-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22.clj
75 lines (63 loc) · 2.54 KB
/
day22.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(ns day22
(:require aoc))
(defn third [[_ _ z]] z)
(def initial-heights
(into {}
(for [x (range 10)
y (range 10)]
[[x y] {:height 0
:brick-id nil}])))
(def initial-state
{:heights initial-heights
:supports {}
:non-removables {}
:on-top-of {}})
(defn part-1 [blocks]
(reduce
(fn [{:keys [heights supports non-removables on-top-of]}
[id [x1 y1 z1 x2 y2 z2]]]
(let [positions (for [x (range x1 (inc x2))
y (range y1 (inc y2))]
[x y])
heights-below (select-keys heights positions)
max-height (apply max (map :height (vals heights-below)))
curr-supports (->> (vals heights-below)
(filter (fn [{:keys [height]}]
(= height max-height)))
(map :brick-id)
(remove nil?)
set)
new-heights (update-vals heights-below
(fn [_] {:height (+ (inc max-height) (- z2 z1))
:brick-id id}))]
{:heights (merge heights new-heights)
:supports (reduce (fn [acc support]
(update acc support conj id))
supports
curr-supports)
:on-top-of (update on-top-of id into curr-supports)
:non-removables (if (= 1 (count curr-supports))
(update non-removables (first curr-supports) conj id)
non-removables)}))
initial-state
(map-indexed vector blocks)))
(defn traverse [supports on-top-of nr]
(loop [stack (list nr)
visited #{}]
(if-not (seq stack)
(dec (count visited))
(let [current (peek stack)
stack' (pop stack)]
(if (visited current)
(recur stack' visited)
(let [candidates (supports current)
vis' (conj visited current)
goods (filter (fn [x] (every? vis' (on-top-of x))) candidates)]
(recur (reduce conj stack' goods) vis')))))))
(defn solve [input-file]
(let [blocks (->> (aoc/parse-input input-file :ints)
(sort-by third))
{:keys [non-removables supports on-top-of]} (part-1 blocks)]
[(- (count blocks) (count non-removables))
(aoc/sum-map #(traverse supports on-top-of %) (keys non-removables))]))
(solve (aoc/read-file 22))