about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2021-12-15 16:05:36 +0100
committerLeah Neukirchen <leah@vuxu.org>2021-12-15 16:05:36 +0100
commitb40cb1f469bbf88641127caee911ec07e7b2e1c4 (patch)
treea65962d93b957664c6cf96dea1d635afbb3342db
parentdf2124d106c44c7a6b4bd4619020c8ffa6a5a719 (diff)
downloadadventofcode2021-b40cb1f469bbf88641127caee911ec07e7b2e1c4.tar.gz
adventofcode2021-b40cb1f469bbf88641127caee911ec07e7b2e1c4.tar.xz
adventofcode2021-b40cb1f469bbf88641127caee911ec07e7b2e1c4.zip
day14
-rw-r--r--day14102
-rw-r--r--day14.bqn14
-rw-r--r--day14.clj45
3 files changed, 161 insertions, 0 deletions
diff --git a/day14 b/day14
new file mode 100644
index 0000000..6c6996b
--- /dev/null
+++ b/day14
@@ -0,0 +1,102 @@
+CVKKFSSNNHNPSPPKBHPB
+
+OF -> S
+VO -> F
+BP -> S
+FC -> S
+PN -> K
+HC -> P
+PP -> N
+FK -> V
+KN -> C
+BO -> O
+KS -> B
+FF -> S
+KC -> B
+FV -> C
+VF -> N
+HS -> H
+OS -> F
+VC -> S
+VP -> P
+BC -> O
+HF -> F
+HO -> F
+PC -> B
+CC -> K
+NB -> N
+KK -> N
+KP -> V
+BH -> H
+BF -> O
+OB -> F
+VK -> P
+FB -> O
+NP -> B
+CB -> C
+PS -> S
+KO -> V
+SP -> C
+BK -> O
+NN -> O
+OC -> F
+VB -> B
+ON -> K
+NK -> B
+CK -> H
+NH -> N
+CV -> C
+PF -> P
+PV -> V
+CP -> N
+FP -> N
+SB -> B
+SN -> N
+KF -> F
+HP -> S
+BN -> V
+NF -> B
+PO -> O
+CH -> O
+VV -> S
+OV -> V
+SF -> P
+BV -> S
+FH -> V
+CN -> H
+VH -> V
+HB -> B
+FN -> P
+OH -> S
+SK -> H
+OP -> H
+VN -> V
+HN -> P
+BS -> S
+CF -> B
+PB -> H
+SS -> K
+NV -> P
+FS -> N
+CS -> O
+OK -> B
+CO -> O
+VS -> F
+OO -> B
+NO -> H
+SO -> F
+HH -> K
+FO -> H
+SH -> O
+HV -> B
+SV -> N
+PH -> F
+BB -> P
+KV -> B
+KB -> H
+KH -> N
+NC -> P
+SC -> S
+PK -> B
+NS -> V
+HK -> B
diff --git a/day14.bqn b/day14.bqn
new file mode 100644
index 0000000..a54b14c
--- /dev/null
+++ b/day14.bqn
@@ -0,0 +1,14 @@
+str←•Import"bqn-libs/strings.bqn"
+template‿rules ← (⊑⋈2⊸↓) str.lf str.Split ¯1↓ •FChars"day14"
+rules ↩ >(" -> " ⊸ str.Split)¨ rules
+
+pairs ← ⊏˘rules
+letters ← ⊑¨pairs
+
+f ← +´˘ pairs ≡⌜ <˘ 2↕template
+t ← ¬ (¯1 ↑ template) ⊒ letters
+
+m ← ((<˘⊑¨rules) ≡⌜ pairs) + ((<˘⊑∘⌽¨⌽˘rules) ≡⌜ pairs)
+
+•Show (⌈´-⌊´) +´¨ (⊐ letters) ⊔ t + m⊸(+˝∘×)⍟10 f
+•Show (⌈´-⌊´) +´¨ (⊐ letters) ⊔ t + m⊸(+˝∘×)⍟40 f
diff --git a/day14.clj b/day14.clj
new file mode 100644
index 0000000..e16337a
--- /dev/null
+++ b/day14.clj
@@ -0,0 +1,45 @@
+(ns org.vuxu.aoc2021.day14
+  (:require [clojure.string :as str]))
+
+(let [[[template _] rules] (split-at 2 (str/split-lines (slurp "day14")))]
+  (def template template)
+  (def rules (into {} (for [rule rules]
+                        (str/split rule #" -> ")))))
+
+(defn my-interleave [even odd]
+  (drop-last (interleave even (concat odd [nil]))))
+
+(defn step [p]
+  (->> (partition 2 1 p)
+       (map (partial apply str))
+       (map rules)
+       (my-interleave p)
+       (apply str)))
+
+(def part1
+  (apply - (apply (juxt max min)
+                  (vals (frequencies (nth (iterate step template) 10))))))
+;; => 2375
+
+
+(def freq-template
+  (frequencies (map (partial apply str) (partition 2 1 template))))
+
+(defn freq-step [f]
+  (apply merge-with + {}
+         (mapcat (fn [[k f]]
+                   [{(str (first k) (rules k)) f}
+                    {(str (rules k) (second k)) f}])
+                 f)))
+
+(defn element-count [final pairs]
+  (apply merge-with + {final 1}
+         (map (fn [[k f]]
+                {(first k) f})
+              pairs)))
+
+(def part2
+  (apply - (apply (juxt max min)
+                  (vals (element-count (last template)
+                         (nth (iterate freq-step freq-template) 40))))))
+;; => 1976896901756