about summary refs log tree commit diff
path: root/day24.clj
blob: 0e57d2a19a06f00c28f0a255db238e7b1be23cdc (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
(def data
  (->> (slurp "day24")
       (clojure.string/split-lines)))

;     nw ne        1,-1   1,0
;    w  .  e     0,-1  0,0  0,1
;     sw se        -1,0  -1,1

(def dirs
  {"e"  [0 1]
   "se" [-1 1]
   "sw" [-1 0]
   "w"  [0 -1]
   "nw" [1 -1]
   "ne" [1 0]})

(defn pos [l]
  (->> l
       (re-seq #"e|se|sw|w|nw|ne")
       (map dirs)
       (apply (partial mapv +))))

(def black
  (set
   (map first (filter (fn [[k v]] (odd? v)) (frequencies (map pos data))))))

(count black)
;; => 386


(defn all-neighbours [cell]
  (for [offset (vals dirs)]
    (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 #(#{1 2} (count (alive-neighbours cells %))) cells))

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

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

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

(->> black
     (iterate tick)
     (#(nth % 100))
     count)
;; => 4214