diff options
author | Leah Neukirchen <leah@vuxu.org> | 2021-12-15 16:05:36 +0100 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2021-12-15 16:05:36 +0100 |
commit | b40cb1f469bbf88641127caee911ec07e7b2e1c4 (patch) | |
tree | a65962d93b957664c6cf96dea1d635afbb3342db /day14.clj | |
parent | df2124d106c44c7a6b4bd4619020c8ffa6a5a719 (diff) | |
download | adventofcode2021-b40cb1f469bbf88641127caee911ec07e7b2e1c4.tar.gz adventofcode2021-b40cb1f469bbf88641127caee911ec07e7b2e1c4.tar.xz adventofcode2021-b40cb1f469bbf88641127caee911ec07e7b2e1c4.zip |
day14
Diffstat (limited to 'day14.clj')
-rw-r--r-- | day14.clj | 45 |
1 files changed, 45 insertions, 0 deletions
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 |