day14: finished part1, part2 wip

master
Clyne 3 years ago
parent 9b0982ba81
commit 3c7c3e5e7f

@ -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))))

@ -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)))
))
Loading…
Cancel
Save