-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10.clj
More file actions
38 lines (33 loc) · 1.38 KB
/
Copy pathday10.clj
File metadata and controls
38 lines (33 loc) · 1.38 KB
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
(ns day10
(:require [input :refer [f->lines lines]]))
(defn parse [it]
(->> it (map #(re-seq #"\w+" %))
(reduce
(fn [[m ii] [a b _ _ e f g _ _ _ k l]]
(if (= a "value")
[(update m (str e f) conj (parse-long b)) ii]
[m (conj ii [(str a b) (str f g) (str k l)])])) [{} []])))
(defn inspect [chips instructions part]
(reduce
(fn [m [bot low high]]
(let [[lo hi :as v] (sort (m bot)), out (vals (select-keys m ["output0" "output1" "output2"]))]
(cond
(and (= part 1) (= v (m :compared))) (reduced bot)
(and (= part 2) (= (count out) 3)) (reduced (->> out flatten (apply *)))
(not= (count v) 2) m
:else (-> m (dissoc bot) (update low conj lo) (update high conj hi)))))
chips (cycle instructions)))
(defn -main [day]
(let [[chips instructions] (->> day f->lines parse)
solve (partial inspect (assoc chips :compared [17 61]) instructions)]
{:part1 (solve 1) :part2 (solve 2)}))
(comment
(let [test-input "value 5 goes to bot 2
bot 2 gives low to bot 1 and high to bot 0
value 3 goes to bot 1
bot 1 gives low to output 1 and high to bot 0
bot 0 gives low to output 2 and high to output 0
value 2 goes to bot 2"
[ch ii] (->> test-input lines parse), solve (partial inspect (assoc ch "comp" [2 5]) ii)]
{:1 (= "bot2" (solve 1)) :2 (= (* 5 2 3) (solve 2))})
)