mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement day 25, in python
This commit is contained in:
56
2017/day-25/solution.py
Executable file
56
2017/day-25/solution.py
Executable file
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/python
|
||||
import fileinput
|
||||
import re
|
||||
|
||||
def read_action(it):
|
||||
to_write = 1 if "1" in next(it) else 0
|
||||
offset = 1 if "right" in next(it) else -1
|
||||
goto = next(it).strip()[-2]
|
||||
|
||||
return (to_write, offset, goto)
|
||||
|
||||
def read_input():
|
||||
in_iter = fileinput.input()
|
||||
|
||||
initial = next(in_iter).strip()[-2]
|
||||
runs = int(next(in_iter).split(" ")[-2])
|
||||
states = {}
|
||||
|
||||
for line in in_iter:
|
||||
line = line.strip()
|
||||
|
||||
if not line:
|
||||
continue
|
||||
|
||||
state = line[-2]
|
||||
next(in_iter)
|
||||
|
||||
if_0 = read_action(in_iter)
|
||||
next(in_iter)
|
||||
|
||||
if_1 = read_action(in_iter)
|
||||
|
||||
states[state] = (if_0, if_1)
|
||||
|
||||
return initial, runs, states
|
||||
|
||||
state, steps, states = read_input()
|
||||
|
||||
ones = set()
|
||||
pos = 0
|
||||
|
||||
for _ in range(steps):
|
||||
if pos in ones:
|
||||
instr = states[state][1]
|
||||
else:
|
||||
instr = states[state][0]
|
||||
|
||||
if instr[0] == 1:
|
||||
ones.add(pos)
|
||||
else:
|
||||
ones.discard(pos)
|
||||
|
||||
pos += instr[1]
|
||||
state = instr[2]
|
||||
|
||||
print(len(ones))
|
||||
Reference in New Issue
Block a user