diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2022-11-30 19:55:31 -0500 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2022-11-30 19:55:31 -0500 |
commit | 8d43e37df99f280377bed90284d6ac2428334804 (patch) | |
tree | 3a5042c9af29da52b4bac38fd78b3ccde77a1dbc /day14 | |
parent | 66ed0b9d27850dc653abc8baa75884f3de311bfa (diff) |
move 2021 days to folder; update README
Diffstat (limited to 'day14')
-rw-r--r-- | day14/part1.clj | 27 | ||||
-rw-r--r-- | day14/part2.clj | 38 |
2 files changed, 0 insertions, 65 deletions
diff --git a/day14/part1.clj b/day14/part1.clj deleted file mode 100644 index de03bda..0000000 --- a/day14/part1.clj +++ /dev/null @@ -1,27 +0,0 @@ -(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 deleted file mode 100644 index f5de5fe..0000000 --- a/day14/part2.clj +++ /dev/null @@ -1,38 +0,0 @@ -(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 [[[a b] c]] - {(str/join [a b]) (map str/join [[a c] [c b]])})) - (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 [final-polymer (nth growth-seq 40) - letter-counts (reduce - (fn [r [k v]] (-> r (update (first k) + v) (update (second k) + v))) - (zipmap (str/join (keys final-polymer)) (repeat 0)) - final-polymer) - unique-counts (filter pos? (map #(Math/ceil (/ (val %) 2)) letter-counts))] - (println (- (apply max unique-counts) (apply min unique-counts)))) - |