Skip to content

Commit 2ee616c

Browse files
committed
2017/25
1 parent 1320fd6 commit 2ee616c

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

2017/25/input.txt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Begin in state A.
2+
Perform a diagnostic checksum after 12173597 steps.
3+
4+
In state A:
5+
If the current value is 0:
6+
- Write the value 1.
7+
- Move one slot to the right.
8+
- Continue with state B.
9+
If the current value is 1:
10+
- Write the value 0.
11+
- Move one slot to the left.
12+
- Continue with state C.
13+
14+
In state B:
15+
If the current value is 0:
16+
- Write the value 1.
17+
- Move one slot to the left.
18+
- Continue with state A.
19+
If the current value is 1:
20+
- Write the value 1.
21+
- Move one slot to the right.
22+
- Continue with state D.
23+
24+
In state C:
25+
If the current value is 0:
26+
- Write the value 1.
27+
- Move one slot to the right.
28+
- Continue with state A.
29+
If the current value is 1:
30+
- Write the value 0.
31+
- Move one slot to the left.
32+
- Continue with state E.
33+
34+
In state D:
35+
If the current value is 0:
36+
- Write the value 1.
37+
- Move one slot to the right.
38+
- Continue with state A.
39+
If the current value is 1:
40+
- Write the value 0.
41+
- Move one slot to the right.
42+
- Continue with state B.
43+
44+
In state E:
45+
If the current value is 0:
46+
- Write the value 1.
47+
- Move one slot to the left.
48+
- Continue with state F.
49+
If the current value is 1:
50+
- Write the value 1.
51+
- Move one slot to the left.
52+
- Continue with state C.
53+
54+
In state F:
55+
If the current value is 0:
56+
- Write the value 1.
57+
- Move one slot to the right.
58+
- Continue with state D.
59+
If the current value is 1:
60+
- Write the value 1.
61+
- Move one slot to the right.
62+
- Continue with state A.
63+

2017/25/run.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class State:
2+
def __init__(self):
3+
self.writ = [0,0]
4+
self.move = [0,0]
5+
self.cont = [0,0]
6+
def run(self, tape, cursor):
7+
val = tape.get(cursor, 0)
8+
tape[cursor] = self.writ[val]
9+
cursor += self.move[val]
10+
return cursor, self.cont[val]
11+
12+
def parse():
13+
states = []
14+
start = 0
15+
diag = 0
16+
state = None
17+
condition = 0
18+
for l in open("input.txt").read().split("\n"):
19+
if not l:
20+
continue
21+
l=l[:-1]
22+
s = l.split(" ")
23+
if l[0] == "B":
24+
start = ord(l[-1]) - 65
25+
elif l[0] == "P":
26+
diag = int(s[-2])
27+
elif l[0] == "I":
28+
state = State()
29+
states.append(state)
30+
condition = 0
31+
elif l[6] == "W":
32+
state.writ[condition] = int(l[-1])
33+
elif l[6] == "M":
34+
state.move[condition] = (-1,1)[l[-2]=="h"]
35+
elif l[6] == "C":
36+
state.cont[condition] = ord(l[-1]) - 65
37+
condition = 1
38+
return states, start, diag
39+
40+
tape = {}
41+
cursor = 2
42+
states, current, diag = parse()
43+
for _ in range(diag):
44+
cursor, current = states[current].run(tape, cursor)
45+
print(sum(tape.values()))

0 commit comments

Comments
 (0)