-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))))) |