]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
add day 11
authorClyne Sullivan <clyne@bitgloo.com>
Sat, 11 Dec 2021 22:01:05 +0000 (17:01 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sat, 11 Dec 2021 22:01:05 +0000 (17:01 -0500)
day11/part1.clj [new file with mode: 0644]
day11/part2.clj [new file with mode: 0644]

diff --git a/day11/part1.clj b/day11/part1.clj
new file mode 100644 (file)
index 0000000..82e1a15
--- /dev/null
@@ -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 (file)
index 0000000..e13848d
--- /dev/null
@@ -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)))))
+