aboutsummaryrefslogtreecommitdiffstats
path: root/day14/part2.clj
blob: 4e283ee6016116343958daa8f2d746c7e757960f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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)))
    ))