From 8bf93934a3246609d185f30d2c6382f5b8d19c86 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 11 Dec 2021 17:01:05 -0500 Subject: [PATCH] add day 11 --- day11/part1.clj | 39 +++++++++++++++++++++++++++++++++++++++ day11/part2.clj | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 day11/part1.clj create mode 100644 day11/part2.clj diff --git a/day11/part1.clj b/day11/part1.clj new file mode 100644 index 0000000..82e1a15 --- /dev/null +++ b/day11/part1.clj @@ -0,0 +1,39 @@ +(defonce input (->> (slurp "./in") + (clojure.string/split-lines) + (map (partial map #(- (int %) 48))) + (atom) + )) +(defonce blinks (atom 0)) + +(defn get-adj [y x] + (filter #(some? (get-in @input %)) + [[(dec y) (dec x)] [(dec y) x] [(dec y) (inc x)] + [y (dec x)] [y (inc x)] + [(inc y) (dec x)] [(inc y) x] [(inc y) (inc x)]] + )) + +(defn apply-incs [] (mapv (partial mapv inc) @input)) + +(defn next-step [] + (reset! input (apply-incs)) + (loop [y 0 x 0] + (cond + (> (get-in @input [y x]) 9) + (do + (doseq [p (get-adj y x)] + (reset! input (update-in @input p #(if (pos? %) (inc %) %)))) + (reset! input (update-in @input [y x] (fn [x] 0))) + (reset! blinks (inc @blinks)) + (recur 0 0)) + (< x (dec (count (first @input)))) + (recur y (inc x)) + (< y (dec (count @input))) + (recur (inc y) 0) + ) + ) + @input + ) + +(dotimes [n 100] (next-step)) +(println @blinks) + diff --git a/day11/part2.clj b/day11/part2.clj new file mode 100644 index 0000000..e13848d --- /dev/null +++ b/day11/part2.clj @@ -0,0 +1,41 @@ +(defonce input (->> (slurp "./in") + (clojure.string/split-lines) + (map (partial map #(- (int %) 48))) + (atom) + )) +(defonce blinks (atom 0)) + +(defn get-adj [y x] + (filter #(some? (get-in @input %)) + [[(dec y) (dec x)] [(dec y) x] [(dec y) (inc x)] + [y (dec x)] [y (inc x)] + [(inc y) (dec x)] [(inc y) x] [(inc y) (inc x)]] + )) + +(defn apply-incs [] (mapv (partial mapv inc) @input)) + +(defn next-step [] + (reset! input (apply-incs)) + (loop [y 0 x 0] + (cond + (> (get-in @input [y x]) 9) + (do + (doseq [p (get-adj y x)] + (reset! input (update-in @input p #(if (pos? %) (inc %) %)))) + (reset! input (update-in @input [y x] (fn [x] 0))) + (reset! blinks (inc @blinks)) + (recur 0 0)) + (< x (dec (count (first @input)))) + (recur y (inc x)) + (< y (dec (count @input))) + (recur (inc y) 0) + ) + ) + @input + ) + +(loop [cnt 0] + (if (= 0 (apply + (flatten @input))) + (println cnt) + (do (next-step) (recur (inc cnt))))) +