day11: remove atoms, clean up execution

master
Clyne 3 years ago
parent 8bf93934a3
commit bf8d987494

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

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

Loading…
Cancel
Save