-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2020-17-conway-cubes.clj
51 lines (45 loc) · 1.51 KB
/
2020-17-conway-cubes.clj
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
(defn parse [n]
(comp (map-indexed (fn [y r] (map-indexed (fn [x v] [(into [x y] (repeat n 0)) v]) r))) cat
(filter (comp #{\#} second))))
(defn neighbors3 [from]
(let [[ax ay az] from]
(for [bx [-1 0 1]
by [-1 0 1]
bz [-1 0 1]
:let [next [(+ ax bx) (+ ay by) (+ az bz)]]
:when (not= from next)]
next)))
(defn neighbors4 [from]
(let [[ax ay az ah] from]
(for [bx [-1 0 1]
by [-1 0 1]
bz [-1 0 1]
bh [-1 0 1]
:let [next [(+ ax bx) (+ ay by) (+ az bz) (+ ah bh)]]
:when (not= from next)]
next)))
(def neighbors
(memoize
(fn [from]
(if (= (count from) 3)
(neighbors3 from)
(neighbors4 from)))))
(defn turn [xs [k v]]
(let [xf (comp (map xs) (filter #{\#}) (take 4))
ct (count (sequence xf (neighbors k)))]
(cond (and (= v \#) (<= 2 ct 3)) \#
(and (= v \.) (= ct 3)) \#
:else \.)))
(defn step [xs]
(let [vs (vec xs)
ks (set (keys xs))
nf (comp (mapcat (comp neighbors key))
(distinct)
(filter (complement ks))
(map (juxt identity (constantly \.))))
xf (comp (map (juxt first (partial turn xs)))
(filter (comp #{\#} second)))]
(into {} xf (into vs nf vs))))
(let [in (line-seq (java.io.BufferedReader. *in*))]
(println "Part A:" (count (nth (iterate step (into {} (parse 1) in)) 6)))
(println "Part B:" (count (nth (iterate step (into {} (parse 2) in)) 6))))