Skip to content

Commit 07fd7c4

Browse files
committed
Day 25, part 1
1 parent a66ffe2 commit 07fd7c4

2 files changed

Lines changed: 148 additions & 0 deletions

File tree

resources/twenty_five.txt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Begin in state A.
2+
Perform a diagnostic checksum after 12134527 steps.
3+
4+
In state A:
5+
If the current value is 0:
6+
- Write the value 1.
7+
- Move one slot to the right.
8+
- Continue with state B.
9+
If the current value is 1:
10+
- Write the value 0.
11+
- Move one slot to the left.
12+
- Continue with state C.
13+
14+
In state B:
15+
If the current value is 0:
16+
- Write the value 1.
17+
- Move one slot to the left.
18+
- Continue with state A.
19+
If the current value is 1:
20+
- Write the value 1.
21+
- Move one slot to the right.
22+
- Continue with state C.
23+
24+
In state C:
25+
If the current value is 0:
26+
- Write the value 1.
27+
- Move one slot to the right.
28+
- Continue with state A.
29+
If the current value is 1:
30+
- Write the value 0.
31+
- Move one slot to the left.
32+
- Continue with state D.
33+
34+
In state D:
35+
If the current value is 0:
36+
- Write the value 1.
37+
- Move one slot to the left.
38+
- Continue with state E.
39+
If the current value is 1:
40+
- Write the value 1.
41+
- Move one slot to the left.
42+
- Continue with state C.
43+
44+
In state E:
45+
If the current value is 0:
46+
- Write the value 1.
47+
- Move one slot to the right.
48+
- Continue with state F.
49+
If the current value is 1:
50+
- Write the value 1.
51+
- Move one slot to the right.
52+
- Continue with state A.
53+
54+
In state F:
55+
If the current value is 0:
56+
- Write the value 1.
57+
- Move one slot to the right.
58+
- Continue with state A.
59+
If the current value is 1:
60+
- Write the value 1.
61+
- Move one slot to the right.
62+
- Continue with state E.

src/advent/twenty_five.clj

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
(ns advent.twenty-five)
2+
3+
(def blueprints {:A {0 {:write 1
4+
:move :right
5+
:next :B}
6+
1 {:write 0
7+
:move :left
8+
:next :C}}
9+
:B {0 {:write 1
10+
:move :left
11+
:next :A}
12+
1 {:write 1
13+
:move :right
14+
:next :C}}
15+
:C {0 {:write 1
16+
:move :right
17+
:next :A}
18+
1 {:write 0
19+
:move :left
20+
:next :D}}
21+
:D {0 {:write 1
22+
:move :left
23+
:next :E}
24+
1 {:write 1
25+
:move :left
26+
:next :C}}
27+
:E {0 {:write 1
28+
:move :right
29+
:next :F}
30+
1 {:write 1
31+
:move :right
32+
:next :A}}
33+
:F {0 {:write 1
34+
:move :right
35+
:next :A}
36+
1 {:write 1
37+
:move :right
38+
:next :E}}})
39+
40+
(defn initial-machine [initial-state]
41+
{:state initial-state
42+
:position 0
43+
:on #{}})
44+
45+
(defmulti write (fn [_ value] value))
46+
47+
(defmethod write 0 [{:keys [position] :as state} value]
48+
(update state :on disj position))
49+
50+
(defmethod write 1 [{:keys [position] :as state} value]
51+
(update state :on conj position))
52+
53+
(defmulti move (fn [_ value] value))
54+
55+
(defmethod move :left [{:keys [position] :as state} value]
56+
(update state :position dec))
57+
58+
(defmethod move :right [{:keys [position] :as state} value]
59+
(update state :position inc))
60+
61+
(defn current-value [{:keys [on position]}]
62+
(if (contains? on position)
63+
1
64+
0))
65+
66+
(defn move-once [{:keys [state] :as machine}]
67+
(let [value (current-value machine)
68+
{to-write :write
69+
to-move :move
70+
next-state :next} (get-in blueprints [state value])]
71+
(-> machine
72+
(write to-write)
73+
(move to-move)
74+
(assoc :state next-state))))
75+
76+
(defn diagnostic-checksum [{:keys [on]}]
77+
(count on))
78+
79+
(defn run []
80+
(let [steps-needed 12134527]
81+
(loop [machine (initial-machine :A)
82+
steps-taken 0]
83+
(if (= steps-taken steps-needed)
84+
(diagnostic-checksum machine)
85+
(recur (move-once machine)
86+
(inc steps-taken))))))

0 commit comments

Comments
 (0)