···11+import fileinput
22+from collections import defaultdict
33+44+from utils import parse_line
55+66+PROG = {}
77+88+# Parse puzzle input
99+for i, line in enumerate(fileinput.input()):
1010+ if i == 0:
1111+ STATE = parse_line(r'Begin in state (\w+).', line)[0]
1212+ elif i == 1:
1313+ STEPS = int(parse_line(r'Perform a diagnostic checksum after (\d+) steps.', line)[0])
1414+1515+ if i <= 2:
1616+ continue
1717+1818+ if i % 10 == 3:
1919+ start = parse_line(r'In state (\w+):', line)[0]
2020+ elif i % 10 == 5:
2121+ write_0 = parse_line(r' - Write the value (\d+).', line)[0]
2222+ elif i % 10 == 6:
2323+ move_0 = parse_line(r' - Move one slot to the (\w+).', line)[0]
2424+ move_0 = 1 if move_0 == 'right' else -1
2525+ elif i % 10 == 7:
2626+ next_0 = parse_line(r' - Continue with state (\w+).', line)[0]
2727+ elif i % 10 == 9:
2828+ write_1 = parse_line(r' - Write the value (\d+).', line)[0]
2929+ elif i % 10 == 0:
3030+ move_1 = parse_line(r' - Move one slot to the (\w+).', line)[0]
3131+ move_1 = 1 if move_1 == 'right' else -1
3232+ elif i % 10 == 1:
3333+ next_1 = parse_line(r' - Continue with state (\w+).', line)[0]
3434+ PROG[start] = ((write_0, move_0, next_0), (write_1, move_1, next_1))
3535+3636+3737+TAPE = defaultdict(int)
3838+CURSOR = 0
3939+4040+for _ in range(STEPS):
4141+ write, dx, ns = PROG[STATE][TAPE[CURSOR]]
4242+ TAPE[CURSOR] = write
4343+ CURSOR += dx
4444+ STATE = ns
4545+4646+print "Diagnostic checksum:", sum(x for x in TAPE.values())
+62
2017/inputs/25.txt
···11+Begin in state A.
22+Perform a diagnostic checksum after 12667664 steps.
33+44+In state A:
55+ If the current value is 0:
66+ - Write the value 1.
77+ - Move one slot to the right.
88+ - Continue with state B.
99+ If the current value is 1:
1010+ - Write the value 0.
1111+ - Move one slot to the left.
1212+ - Continue with state C.
1313+1414+In state B:
1515+ If the current value is 0:
1616+ - Write the value 1.
1717+ - Move one slot to the left.
1818+ - Continue with state A.
1919+ If the current value is 1:
2020+ - Write the value 1.
2121+ - Move one slot to the right.
2222+ - Continue with state D.
2323+2424+In state C:
2525+ If the current value is 0:
2626+ - Write the value 0.
2727+ - Move one slot to the left.
2828+ - Continue with state B.
2929+ If the current value is 1:
3030+ - Write the value 0.
3131+ - Move one slot to the left.
3232+ - Continue with state E.
3333+3434+In state D:
3535+ If the current value is 0:
3636+ - Write the value 1.
3737+ - Move one slot to the right.
3838+ - Continue with state A.
3939+ If the current value is 1:
4040+ - Write the value 0.
4141+ - Move one slot to the right.
4242+ - Continue with state B.
4343+4444+In state E:
4545+ If the current value is 0:
4646+ - Write the value 1.
4747+ - Move one slot to the left.
4848+ - Continue with state F.
4949+ If the current value is 1:
5050+ - Write the value 1.
5151+ - Move one slot to the left.
5252+ - Continue with state C.
5353+5454+In state F:
5555+ If the current value is 0:
5656+ - Write the value 1.
5757+ - Move one slot to the right.
5858+ - Continue with state D.
5959+ If the current value is 1:
6060+ - Write the value 1.
6161+ - Move one slot to the right.
6262+ - Continue with state A.