-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.clj
38 lines (30 loc) · 911 Bytes
/
day08.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
(ns day08
(:require aoc
[clojure.string :as str]))
(defn parse-network [network]
(reduce
(fn [acc line]
(let [[start left right] (map keyword (re-seq #"\w+" line))]
(assoc acc start {:L left :R right})))
{}
network))
(defn steps [instrs network start]
(loop [n 0
[hd & tl] (cycle instrs)
node start]
(if (str/ends-with? node "Z")
n
(recur (inc n) tl ((network node) hd)))))
(defn ghost-steps [instrs network starts]
(transduce
(map #(steps instrs network %))
aoc/lcm
starts))
(defn solve [input]
(let [[[instrs'] network'] (aoc/parse-input-paragraphs input)
network (parse-network network')
instrs (mapv (comp keyword str) instrs')
starts (filter #(str/ends-with? % "A") (keys network))]
[(steps instrs network :AAA)
(ghost-steps instrs network starts)]))
(solve (aoc/read-file 8))