From bf8d9874947868de154b21244d7e6fb974b56e3f Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 11 Dec 2021 19:21:39 -0500 Subject: [PATCH] day11: remove atoms, clean up execution --- day11/part1.clj | 62 +++++++++++++++++++++++----------------------- day11/part2.clj | 65 +++++++++++++++++++++++++------------------------ 2 files changed, 65 insertions(+), 62 deletions(-) diff --git a/day11/part1.clj b/day11/part1.clj index 82e1a15..5113fd4 100644 --- a/day11/part1.clj +++ b/day11/part1.clj @@ -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)))) diff --git a/day11/part2.clj b/day11/part2.clj index e13848d..4c4663c 100644 --- a/day11/part2.clj +++ b/day11/part2.clj @@ -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))