about summary refs log tree commit diff
path: root/day17.clj
blob: b2f66fb2b682b5a73cb7fab6ce6c2c3cd75df35b (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
(def data
  (->> (slurp "day17")
       clojure.string/split-lines))

(def cells
  (set
   (remove nil?
           (for [y (range (count data))
                 x (range (count (first data)))]
             (if (= \# (get-in data [y x]))
               (list 0 y x))))))

(def neighbour-offsets
  (let [digits [-1 0 1]]
    (for [x digits
          y digits
          z digits
          :when (not= 0 x y z)]
      [z y x])))

(defn all-neighbours [cell]
  (for [offset neighbour-offsets]
    (map + offset cell)))

(defn alive-neighbours [cells cell]
  (filter cells (all-neighbours cell)))

(defn dead-neighbours [cells cell]
  (filter (complement cells) (all-neighbours cell)))

(defn regulate [cells]
  (filter #(#{2 3} (count (alive-neighbours cells %))) cells))

(defn dead-neighbour-cells [cells]
  (reduce clojure.set/union (map (partial dead-neighbours cells) cells)))

(defn reproduce [cells]
  (filter #(= 3 (count (alive-neighbours cells %))) (dead-neighbour-cells cells)))

(defn tick [cells] 
  (clojure.set/union (set (reproduce cells)) (set (regulate cells))))


(->> cells
     (iterate tick)
     (drop 6)
     first
     count)
;; => 298


(def cells
  (set (map (partial cons 0) cells)))

(def neighbour-offsets
  (let [digits [-1 0 1]]
    (for [x digits
          y digits
          z digits
          w digits
          :when (not= 0 x y z w)] 
      [w z y x])))

(->> cells
     (iterate tick)
     (drop 6)
     first
     count)
;; => 1792