-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday12.py
66 lines (51 loc) · 2 KB
/
day12.py
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
from typing import List
from collections import defaultdict
import progressbar
class Computer:
def __init__(self, instructions: List[List[str]], registers: dict):
self.instructions = instructions
self.registers = registers
self.ptr = 0
def run(self):
end = len(self.instructions)
with progressbar.ProgressBar() as p:
while self.ptr != end:
p.update(self.ptr)
instruction = self.instructions[self.ptr]
op = instruction[0]
if op == 'cpy':
_, value, reg = instruction
self.registers[reg] = self.registers[value] if value in ['a', 'b', 'c', 'd'] else int(value)
self.ptr += 1
elif op == 'inc':
self.registers[instruction[1]] += 1
self.ptr += 1
elif op == 'dec':
self.registers[instruction[1]] -= 1
self.ptr += 1
elif op == 'jnz':
_, reg, offset = instruction
value = self.registers[reg] if reg in ['a', 'b', 'c', 'd'] else int(reg)
self.ptr += int(offset) if value != 0 else 1
else:
print('operation not recognized:', op)
break
def parse_input(filename: str):
return [line.strip().split() for line in open(filename).readlines()]
def part1(instructions: List[List[str]]) -> int:
registers = defaultdict(int)
computer = Computer(instructions, registers)
computer.run()
return computer.registers['a']
def part2(instructions: List[List[str]]) -> int:
registers = defaultdict(int)
registers['c'] = 1
computer = Computer(instructions, registers)
computer.run()
return computer.registers['a']
def main():
instructions = parse_input('input/day12.txt')
print(f'Part 1: {part1(instructions)}')
print(f'Part 2: {part2(instructions)}')
if __name__ == "__main__":
main()