-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
48 lines (39 loc) · 1.17 KB
/
script.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
from GhostyUtils import aoc
def process(program, regs):
instr_ptr = 0
while 0 <= instr_ptr < len(program):
instr = program[instr_ptr]
op, arg = instr.split(maxsplit=1)
reg = None
offset = None
if op in ['hlf', 'tpl', 'inc']:
reg = arg
elif op in ['jie', 'jio']:
reg, offset = arg.split(', ')
offset = int(offset)
elif op in ['jmp']:
offset = int(arg)
match op:
case 'hlf':
regs[reg] //= 2
instr_ptr += 1
case 'tpl':
regs[reg] *= 3
instr_ptr += 1
case 'inc':
regs[reg] += 1
instr_ptr += 1
case 'jmp':
instr_ptr += offset
case 'jie':
instr_ptr += offset if regs[reg] % 2 == 0 else 1
case 'jio':
instr_ptr += offset if regs[reg] == 1 else 1
if __name__ == "__main__":
program = aoc.read_lines()
regs = {'a': 0, 'b': 0}
process(program, regs)
print(regs['b'])
regs = {'a': 1, 'b': 0}
process(program, regs)
print(regs['b'])