aboutsummaryrefslogtreecommitdiffstats
path: root/day14/part2.clj
blob: ede1b32dcd09351d144c79928de6af5ec2c9dec2 (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])

(def input (->> (slurp "./in")
                str/split-lines
                ((juxt
                   #(let [init-polymer (first %)]
                      (for [i (range 0 (dec (count init-polymer)))]
                        (subs init-polymer i (+ i 2))))
                   (fn [lines]
                     (->> lines
                       (drop 2)
                       (map #(str/split % #" -> "))
                       (map (fn [[pair ltr]]
                              {pair (map str/join [[(first pair) ltr] [ltr (second pair)]])}))
                       (reduce into)
                       ))))))

(def blank-map (zipmap (keys (second input)) (repeat 0)))

(defn grow-polymer [polymer insertion-rules]
  (reduce
    #(let [[p1 p2] (insertion-rules (key %2)) v (val %2)]
       (-> %1 (update p1 + v) (update p2 + v)))
    blank-map
    (filter (comp pos? val) polymer)))

(def growth-seq
  (iterate #(grow-polymer % (second input))
           (reduce #(update %1 %2 inc) blank-map (first input))))

(let [polymer (nth growth-seq 40)  
      results (filter pos?
                (map #(Math/ceil (/ % 2))
                  (vals
                    (reduce
                      #(let [k (key %2) v (val %2)] (-> %1 (update (first k) + v) (update (second k) + v)))
                      (zipmap (flatten (map (partial split-at 1) (keys polymer))) (repeat 0))
                      polymer))))]
  (println (- (apply max results) (apply min results))))