1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
| #!/usr/bin/env python3
#Advent of Code
#2017 Day 25, Part 1
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
import collections
def solve(puzzle_input):
state = puzzle_input[0].split()[-1][:-1]
steps = int(puzzle_input[1].split()[-2])
states = {}
for i, line in enumerate(puzzle_input):
if line[:8] == "In state":
states[line.split()[-1][:-1]] = [
[
int(puzzle_input[i+2].split()[-1][:-1]),
int(puzzle_input[i+3].split()[-1][:-1] == "right"),
puzzle_input[i+4].split()[-1][:-1]
],
[
int(puzzle_input[i+6].split()[-1][:-1]),
int(puzzle_input[i+7].split()[-1][:-1] == "right"),
puzzle_input[i+8].split()[-1][:-1]
]
]
if states[line.split()[-1][:-1]][0][1] == 0:
states[line.split()[-1][:-1]][0][1] = -1
if states[line.split()[-1][:-1]][1][1] == 0:
states[line.split()[-1][:-1]][1][1] = -1
tape = collections.defaultdict(int)
pos = 0
for i in range(steps):
tape[pos], direction, state = states[state][tape[pos]]
pos += direction
return sum(tape.values())
def main():
puzzle_input = util.read.as_lines()
checksum = solve(puzzle_input)
print("The diagnostic checksum is " + str(checksum) + ".")
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
self.assertEqual(solve(["Begin in state A.",
"Perform a diagnostic checksum after 6 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 B.",
"",
"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 A."]), 3)
if __name__ == "__main__":
run(main)
|