aboutsummaryrefslogtreecommitdiffstats
path: root/year2021/day11/part1.clj
blob: 5113fd4a1a35b4e088801f97806600f81c68f751 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
(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 apply-incs [in] (mapv (partial mapv inc) in))

(defn next-step [indata]
  (loop [in (apply-incs (indata :grid)) bl (indata :blinks) y 0 x 0]
    (cond
      (> (get-in in [y x]) 9)
      (do
        (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}
      )
    )
  )

(->> (slurp "./in")
     (clojure.string/split-lines)
     (map (partial map #(- (int %) 48)))
     (assoc {} :blinks 0 :grid)
     (iterate next-step)
     (#(nth % 100))
     (#(println (% :blinks))))