-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.janet
58 lines (46 loc) · 1.99 KB
/
day08.janet
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 network-peg
~{:instr ':a+
:node (/ (* ':w+ " = (" ':w+ ", " ':w+ ")\n"), |{:label $0 :left $1 :right $2})
:main (* :instr "\n\n" (some :node))})
(defn network [s]
(def @[instr & nodes] (peg/match network-peg s))
{:instr instr :nodes (tabseq [{:label l :left left :right right} :in nodes] l {:left left :right right})})
(comment
(def input (file/read (file/open "inputs/day08.test") :all))
(network input))
(defn camel [{:instr instr :nodes nodes}]
(var location "AAA")
(sum (generate [_ :iterate true :until (= location "ZZZ")
i :in instr
:until (= location "ZZZ")]
(printf "Now in state %p" location)
(set location (case (string/from-bytes i)
"L" ((nodes location) :left)
"R" ((nodes location) :right)))
1)))
(defn camel2 [loc {:instr instr :nodes nodes}]
(var location loc)
(sum (generate [_ :iterate true :until (= (string/slice location -2) "Z")
i :in instr
:until (= (string/slice location -2) "Z")]
(printf "Now in state %p" location)
(set location (case (string/from-bytes i)
"L" ((nodes location) :left)
"R" ((nodes location) :right)))
1)))
(defn ghost [{:instr instr :nodes nodes}]
(def starting (filter |(= 65 (last $)) (keys nodes)))
(map |(camel2 $ {:instr instr :nodes nodes}) starting))
# (sum (generate [_ :iterate true :until (done)
# i :in instr :until (done)]
# (printf "Now in states %p" locations)
# (set locations (case (string/from-bytes i)
# "L" (map |((nodes $) :left) locations)
# "R" (map |((nodes $) :right) locations)))
# 1)))
(defn main [& args]
(def input (file/read stdin :all))
(def net (network input))
(pp net)
# (prin "Part 1: ") (pp (camel net))
(prin "Part 2: ") (pp (ghost net)))