Skip to content

Commit

Permalink
Solution to day 25
Browse files Browse the repository at this point in the history
  • Loading branch information
dlofstrom committed Dec 25, 2017
1 parent c8f84c8 commit 06c929a
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
62 changes: 62 additions & 0 deletions dlofstrom-python/day25.in
@@ -0,0 +1,62 @@
Begin in state A.
Perform a diagnostic checksum after 12399302 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 right.
- Continue with state C.

In state B:
If the current value is 0:
- Write the value 0.
- Move one slot to the left.
- Continue with state A.
If the current value is 1:
- Write the value 0.
- 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 D.
If the current value is 1:
- Write the value 1.
- Move one slot to the right.
- Continue with state A.

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 0.
- Move one slot to the left.
- Continue with state D.

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 left.
- Continue with state B.

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.
48 changes: 48 additions & 0 deletions dlofstrom-python/day25.py
@@ -0,0 +1,48 @@
import sys
import re

input = sys.stdin.read()
state = re.findall(r'(?<=Begin in state )\w+(?=.)',input)[0]
steps = int(re.findall(r'(?<=Perform a diagnostic checksum after )[0-9]+(?= steps.)',input)[0])
input = input.split('In ')

#Generate turing machine state lookup
lookup = {}
for l in input:
s = re.findall(r'(?<=state )\w+(?=:)',l)
if s:
s = s[0]
i = re.findall(r'(?<=current value is )\w+(?=:)',l)
w = re.findall(r'(?<=Write the value )\w+(?=.)',l)
m = re.findall(r'(?<=Move one slot to the )\w+(?=.)',l)
c = re.findall(r'(?<=Continue with state )\w+(?=.)',l)
m = [1 if x=='right' else -1 for x in m]
lookup[(s,int(i[0]))] = (int(w[0]),m[0],c[0])
lookup[(s,int(i[1]))] = (int(w[1]),m[1],c[1])

#Step through
strip = {}
slot = 0
for i in range(steps):
#Read strip value
value = 0
if strip.has_key(slot):
value = strip[slot]

#State action
w, m, c = lookup[(state, value)]
strip[slot] = w
slot += m
state = c

#Count number of 1's on strip
print "Part 1:", strip.values().count(1)









0 comments on commit 06c929a

Please sign in to comment.