Skip to content

Commit

Permalink
Day 25, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
bcobb committed Dec 28, 2017
1 parent a66ffe2 commit 07fd7c4
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
62 changes: 62 additions & 0 deletions resources/twenty_five.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
Begin in state A.
Perform a diagnostic checksum after 12134527 steps.

In state A:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state B.
If the current value is 1:
- Write the value 0.
- Move one slot to the left.
- Continue with state C.

In state B:
If the current value is 0:
- Write the value 1.
- Move one slot to the left.
- Continue with state A.
If the current value is 1:
- Write the value 1.
- Move one slot to the right.
- Continue with state C.

In state C:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state A.
If the current value is 1:
- Write the value 0.
- Move one slot to the left.
- Continue with state D.

In state D:
If the current value is 0:
- Write the value 1.
- Move one slot to the left.
- Continue with state E.
If the current value is 1:
- Write the value 1.
- Move one slot to the left.
- Continue with state C.

In state E:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state F.
If the current value is 1:
- Write the value 1.
- Move one slot to the right.
- Continue with state A.

In state F:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state A.
If the current value is 1:
- Write the value 1.
- Move one slot to the right.
- Continue with state E.
86 changes: 86 additions & 0 deletions src/advent/twenty_five.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
(ns advent.twenty-five)

(def blueprints {:A {0 {:write 1
:move :right
:next :B}
1 {:write 0
:move :left
:next :C}}
:B {0 {:write 1
:move :left
:next :A}
1 {:write 1
:move :right
:next :C}}
:C {0 {:write 1
:move :right
:next :A}
1 {:write 0
:move :left
:next :D}}
:D {0 {:write 1
:move :left
:next :E}
1 {:write 1
:move :left
:next :C}}
:E {0 {:write 1
:move :right
:next :F}
1 {:write 1
:move :right
:next :A}}
:F {0 {:write 1
:move :right
:next :A}
1 {:write 1
:move :right
:next :E}}})

(defn initial-machine [initial-state]
{:state initial-state
:position 0
:on #{}})

(defmulti write (fn [_ value] value))

(defmethod write 0 [{:keys [position] :as state} value]
(update state :on disj position))

(defmethod write 1 [{:keys [position] :as state} value]
(update state :on conj position))

(defmulti move (fn [_ value] value))

(defmethod move :left [{:keys [position] :as state} value]
(update state :position dec))

(defmethod move :right [{:keys [position] :as state} value]
(update state :position inc))

(defn current-value [{:keys [on position]}]
(if (contains? on position)
1
0))

(defn move-once [{:keys [state] :as machine}]
(let [value (current-value machine)
{to-write :write
to-move :move
next-state :next} (get-in blueprints [state value])]
(-> machine
(write to-write)
(move to-move)
(assoc :state next-state))))

(defn diagnostic-checksum [{:keys [on]}]
(count on))

(defn run []
(let [steps-needed 12134527]
(loop [machine (initial-machine :A)
steps-taken 0]
(if (= steps-taken steps-needed)
(diagnostic-checksum machine)
(recur (move-once machine)
(inc steps-taken))))))

0 comments on commit 07fd7c4

Please sign in to comment.