-
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.
Added day25 solution. Added python-related entries to .gitignore
- Loading branch information
Showing
4 changed files
with
147 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 |
---|---|---|
|
@@ -3,3 +3,5 @@ debug | |
*.exe | ||
*.o | ||
*.dll | ||
__pycache__ | ||
*.pyc |
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,43 @@ | ||
"""Advent of Code | ||
day25.py | ||
Code by Dylan Sheehy | ||
http://adventofcode.com/2017/day/25 | ||
""" | ||
|
||
from turing import Turing | ||
|
||
STATES = { | ||
'A': [ | ||
{'write': 1, 'move': 1, 'next': 'B'}, # current value is 0 | ||
{'write': 0, 'move': -1, 'next': 'C'} # current value is 1 | ||
], | ||
'B': [ | ||
{'write': 1, 'move': -1, 'next': 'A'}, # current value is 0 | ||
{'write': 1, 'move': 1, 'next': 'D'} # current value is 1 | ||
], | ||
'C': [ | ||
{'write': 1, 'move': 1, 'next': 'A'}, # current value is 0 | ||
{'write': 0, 'move': -1, 'next': 'E'} # current value is 1 | ||
], | ||
'D': [ | ||
{'write': 1, 'move': 1, 'next': 'A'}, # current value is 0 | ||
{'write': 0, 'move': 1, 'next': 'B'} # current value is 1 | ||
], | ||
'E': [ | ||
{'write': 1, 'move': -1, 'next': 'F'}, # current value is 0 | ||
{'write': 1, 'move': -1, 'next': 'C'} # current value is 1 | ||
], | ||
'F': [ | ||
{'write': 1, 'move': 1, 'next': 'D'}, # current value is 0 | ||
{'write': 1, 'move': 1, 'next': 'A'} # current value is 1 | ||
] | ||
} | ||
|
||
TM = Turing('A', STATES) | ||
STEPS = 12919244 | ||
for i in range(STEPS): | ||
if i % 1000000 == 0: | ||
print(i) | ||
TM.run() | ||
|
||
print(TM.checksum()) |
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 12919244 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 D. | ||
|
||
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 E. | ||
|
||
In state D: | ||
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 right. | ||
- Continue with state B. | ||
|
||
In state E: | ||
If the current value is 0: | ||
- Write the value 1. | ||
- Move one slot to the left. | ||
- Continue with state F. | ||
If the current value is 1: | ||
- Write the value 1. | ||
- Move one slot to the left. | ||
- Continue with state C. | ||
|
||
In state F: | ||
If the current value is 0: | ||
- Write the value 1. | ||
- Move one slot to the right. | ||
- Continue with state D. | ||
If the current value is 1: | ||
- Write the value 1. | ||
- Move one slot to the right. | ||
- Continue with state A. |
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,40 @@ | ||
"""Contains a Turing machine class, and not much else.""" | ||
|
||
class Turing: | ||
"Represents a Turing machine." | ||
def __init__(self, start, states): | ||
"""Creates a Turing machine. | ||
Creates a Turing machine with the given starting state and state transitions. | ||
States should look like this: | ||
{ | ||
'State1': [ | ||
{'write': value_to_write, 'move': <1 or -1>, 'next': next_state}, | ||
{'write': value_to_write, 'move': <1 or -1>, 'next': next_state} | ||
], | ||
'State2': [...], | ||
... | ||
} | ||
Arguments: | ||
start {str} -- The Turing Machine's starting state. | ||
steps {int} -- Number of steps to run | ||
states {dict} -- State transitions | ||
""" | ||
self.current = start | ||
self.states = states | ||
self.cursor = 0 | ||
self.tape = {} | ||
|
||
def run(self): | ||
"""Runs the Turing Maching for one step.""" | ||
value = self.tape.get(self.cursor, 0) | ||
state = self.states[self.current][value] | ||
self.tape[self.cursor] = state['write'] # write to the tape | ||
self.cursor += state['move'] # move the cursor | ||
self.current = state['next'] # go to next state | ||
|
||
def checksum(self): | ||
"Returns the number of ones on the tape." | ||
return sum(self.tape.values()) |