diff --git a/day14/part1.clj b/day14/part1.clj new file mode 100644 index 0000000..de03bda --- /dev/null +++ b/day14/part1.clj @@ -0,0 +1,27 @@ +(require '[clojure.string :as str]) + +(def input (->> (slurp "./in") + str/split-lines + ((juxt + first + (fn [lines] + (->> lines + (drop 2) + (map #(str/split % #" -> ")) + (flatten) + (apply (partial assoc {})) + )))))) + +(defn grow-polymer [polymer insertion-rules] + (str/join + (cons + (first polymer) + (mapcat (juxt insertion-rules second) + (for [i (range 0 (dec (count polymer)))] + (subs polymer i (+ i 2))))))) + +(def growth-seq (iterate #(grow-polymer % (second input)) (first input))) + +(let [freqs (vals (frequencies (nth growth-seq 10)))] + (println (- (apply max freqs) (apply min freqs)))) + diff --git a/day14/part2.clj b/day14/part2.clj new file mode 100644 index 0000000..4e283ee --- /dev/null +++ b/day14/part2.clj @@ -0,0 +1,40 @@ +(require '[clojure.string :as str]) +(require '[clojure.core.reducers :as r]) + +(def input (->> (slurp "./in") + str/split-lines + ((juxt + first + (fn [lines] + (->> lines + (drop 2) + (map #(str/split % #" -> ")) + (flatten) + (apply (partial assoc {})) + )))))) + +(defn handle-pair [pair steps] + (if (= 0 steps) + (frequencies (rest pair)) + (let [ins ((second input) pair) + p1 (handle-pair (str/join [(first pair) ins]) (dec steps)) + p2 (handle-pair (str/join [ins (second pair)]) (dec steps))] + (reduce + (fn [r p] (update r (key p) #(if (nil? %) (val p) (+ (val p) %)))) + p1 p2) + ))) + +(println + (reduce + (fn [tot pair] + (let [freqs (handle-pair pair 23)] + (println "Finished pair " pair) + (reduce + (fn [r p] (update r (key p) #(if (nil? %) (val p) (+ (val p) %)))) + tot + freqs))) + {} + (for [i (range 0 (dec (count (first input))))] + (subs (first input) i (+ i 2))) + )) +