-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday23.py
73 lines (59 loc) · 1.99 KB
/
day23.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
67
68
69
70
71
72
73
from typing import List
class Computer:
def __init__(self, instructions: List, regs: dict):
self.ptr = 0
self.regs = regs
self.instructions = instructions
self.end = len(self.instructions)
def run(self):
while self.ptr < self.end:
# compute instructions
instruction = self.instructions[self.ptr]
op, reg, *_ = instruction
if op == 'inc':
self.regs[reg] += 1
self.ptr += 1
if op == 'hlf':
self.regs[reg] /= 2
self.ptr += 1
elif op == 'tpl':
self.regs[reg] *= 3
self.ptr += 1
elif op == 'jio':
offset = int(instruction[2])
if self.regs[reg] == 1:
self.ptr += offset
else:
self.ptr += 1
elif op == 'jie':
offset = int(instruction[2])
if self.regs[reg] % 2 == 0:
self.ptr += offset
else:
self.ptr += 1
elif op == 'jmp':
offset = int(instruction[1])
self.ptr += offset
def parse_input(filename: str):
lines = [line.strip() for line in open(filename).readlines()]
instructions = []
for line in lines:
op = line[0: 3]
instructions.append([op] + line[4:].split(', '))
return instructions
def part1(instructions: List[List], regs: dict) -> int:
computer = Computer(instructions, regs)
computer.run()
return computer.regs['b']
def part2(instructions: List[List], regs: dict) -> int:
computer = Computer(instructions, regs)
computer.run()
return computer.regs['b']
def main():
instructions = parse_input('input/day23.txt')
regs = {'a': 0, 'b': 0}
print(f'Part 1: {part1(instructions, regs)}')
regs = {'a': 1, 'b': 0}
print(f'Part 2: {part2(instructions, regs)}')
if __name__ == "__main__":
main()