You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
815 B
Clojure
28 lines
815 B
Clojure
3 years ago
|
(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))))
|
||
|
|