aoc2017/day25.py
2022-12-04 13:11:47 +01:00

70 lines
2.0 KiB
Python

from tools.aoc import AOCDay
from typing import Any
class Day(AOCDay):
inputs = [
[
(3, "input25_test"),
(5593, "input25")
],
[
(None, "input25")
]
]
def get_blueprint(self) -> (str, int, dict):
i = self.getInput()
begin_state = i[0][-2]
step_count = int(i[1].split(" ")[-2])
blueprint = {}
current_state = ''
current_value_order = {}
for line in i[3:]:
if not line:
blueprint[current_state].append(current_value_order)
elif line.startswith("In state "):
current_state = line[-2]
blueprint[current_state] = []
elif line.startswith(" If the current value is"):
if line[-2] == '1':
blueprint[current_state].append(current_value_order)
current_value_order = {}
elif line.startswith(" - Write"):
current_value_order['write'] = int(line.split(" ")[-1][:-1])
elif line.startswith(" - Move"):
current_value_order['move'] = -1 if line.split(" ")[-1] == 'left.' else 1
elif line.startswith(" - Continue"):
current_value_order['continue'] = line[-2]
blueprint[current_state].append(current_value_order)
return begin_state, step_count, blueprint
def part1(self) -> Any:
current_state, step_count, blueprint = self.get_blueprint()
tape = [0]
index = 0
for _ in range(step_count):
order = blueprint[current_state][tape[index]]
tape[index] = order['write']
index += order['move']
current_state = order['continue']
if index < 0:
tape.insert(0, 0)
index = 0
if index == len(tape):
tape.append(0)
return sum(tape)
def part2(self) -> Any:
return ""
if __name__ == '__main__':
day = Day(2017, 25)
day.run(verbose=True)