]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
day11: remove atoms, clean up execution
authorClyne Sullivan <clyne@bitgloo.com>
Sun, 12 Dec 2021 00:21:39 +0000 (19:21 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sun, 12 Dec 2021 00:21:39 +0000 (19:21 -0500)
day11/part1.clj
day11/part2.clj

index 82e1a154f1764e3265d62aecba8ce4c5990fb529..5113fd4a1a35b4e088801f97806600f81c68f751 100644 (file)
@@ -1,39 +1,41 @@
-(defonce input (->> (slurp "./in")
-                    (clojure.string/split-lines)
-                    (map (partial map #(- (int %) 48)))
-                    (atom)
-                    ))
-(defonce blinks (atom 0))
+(defn get-adj [in y x]
+  (filter
+    #(some? (get-in in %))
+    [[(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 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 [in] (mapv (partial mapv inc) in))
 
-(defn apply-incs [] (mapv (partial mapv inc) @input))
-
-(defn next-step []
-  (reset! input (apply-incs))
-  (loop [y 0 x 0]
+(defn next-step [indata]
+  (loop [in (apply-incs (indata :grid)) bl (indata :blinks) y 0 x 0]
     (cond
-      (> (get-in @input [y x]) 9)
+      (> (get-in in [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)
+        (recur
+          (-> (reduce
+                (fn [i n] (update-in i n #(cond-> % (pos? %) inc)))
+                in
+                (get-adj in y x))
+              (update-in [y x] #(do % 0)))
+          (inc bl)
+          0 0))
+      (< x (dec (count (first in))))
+      (recur in bl y (inc x))
+      (< y (dec (count in)))
+      (recur in bl (inc y) 0)
+      :else
+      {:grid in :blinks bl}
       )
     )
-  @input
   )
 
-(dotimes [n 100] (next-step))
-(println @blinks)
+(->> (slurp "./in")
+     (clojure.string/split-lines)
+     (map (partial map #(- (int %) 48)))
+     (assoc {} :blinks 0 :grid)
+     (iterate next-step)
+     (#(nth % 100))
+     (#(println (% :blinks))))
 
index e13848d2a3fef1c194860eec18db5ad4cdbae44a..4c4663c3b61ce98b45c2789681e99f8ef2f234ac 100644 (file)
@@ -1,41 +1,42 @@
-(defonce input (->> (slurp "./in")
-                    (clojure.string/split-lines)
-                    (map (partial map #(- (int %) 48)))
-                    (atom)
-                    ))
-(defonce blinks (atom 0))
+(defn get-adj [in y x]
+  (filter
+    #(some? (get-in in %))
+    [[(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 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 [in] (mapv (partial mapv inc) in))
 
-(defn apply-incs [] (mapv (partial mapv inc) @input))
-
-(defn next-step []
-  (reset! input (apply-incs))
-  (loop [y 0 x 0]
+(defn next-step [indata]
+  (loop [in (apply-incs (indata :grid)) bl (indata :blinks) y 0 x 0]
     (cond
-      (> (get-in @input [y x]) 9)
+      (> (get-in in [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)
+        (recur
+          (-> (reduce
+                (fn [i n] (update-in i n #(cond-> % (pos? %) inc)))
+                in
+                (get-adj in y x))
+              (update-in [y x] #(do % 0)))
+          (inc bl)
+          0 0))
+      (< x (dec (count (first in))))
+      (recur in bl y (inc x))
+      (< y (dec (count in)))
+      (recur in bl (inc y) 0)
+      :else
+      {:grid in :blinks bl}
       )
     )
-  @input
   )
 
-(loop [cnt 0]
-  (if (= 0 (apply + (flatten @input)))
-    (println cnt)
-    (do (next-step) (recur (inc cnt)))))
+(->> (slurp "./in")
+     (clojure.string/split-lines)
+     (map (partial map #(- (int %) 48)))
+     (assoc {} :blinks 0 :grid)
+     (iterate next-step)
+     (take-while #(pos? (apply + (flatten (% :grid)))))
+     (count)
+     (println))