about summary refs log tree commit diff
path: root/day11.clj
blob: 73191adba1567a7cf21b38250ff174af2e7b3c49 (plain) (blame)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
(def data
  (->> (slurp "day11")
       (clojure.string/split-lines)
       (map vec)
       vec))

(defn in-bound [[x y]]
  (and (< -1 x (count data))
       (< -1 y (count (first data)))))

(defn fix [l]
  (->> l
       (partition 2 1)
       (drop-while (fn [[x y]] (not= x y)))
       ffirst))

(defn gneighbors [ch? [x y]]
  (->> (for [dir (for [dx [-1 0 1]
                       dy [-1 0 1]
                       :when (not= 0 dx dy)]
                   [dx dy])]
         (->> [x y]
              (iterate (partial mapv + dir))
              (drop 1)
              (take-while in-bound)
              (map (fn [p] [p (get-in data p)]))
              (some (fn [[p c]] (if (ch? c) p)))))
       (remove nil?)))

(defn gstep [neighbors ncnt data]
  (vec (for [x (range (count data))]
         (vec (for [y (range (count (first data)))]
                (let [occ (->> (neighbors [x y])
                               (map (partial get-in data))
                               (filter #{\#})
                               count)]
                  (case (get-in data [x y])
                    \. \.
                    \L (if (zero? occ) \# \L)
                    \# (if (>= occ ncnt) \L \#))))))))

(->> data
     (iterate (partial gstep (partial gneighbors #{\L \# \.}) 4))
     fix
     flatten
     (filter #{\#})
     count) ; => 2324

(->> data
     (iterate (partial gstep (partial gneighbors #{\L \#}) 5))
     fix
     flatten
     (filter #{\#})
     count) ; => 2068