Skip to content

Commit

Permalink
Added day25 solution. Added python-related entries to .gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
djsheehy committed Dec 26, 2017
1 parent af782ba commit 525a874
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ debug
*.exe
*.o
*.dll
__pycache__
*.pyc
43 changes: 43 additions & 0 deletions 2017/day25/day25.py
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())
62 changes: 62 additions & 0 deletions 2017/day25/input.txt
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.
40 changes: 40 additions & 0 deletions 2017/day25/turing.py
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())

0 comments on commit 525a874

Please sign in to comment.