-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path202417.py
73 lines (64 loc) · 1.79 KB
/
202417.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
from utils import input_as_strings_iter
regs = [0, 0, 0]
instrs = []
regidx = 0
input_part_one = True
for line in input_as_strings_iter("202417.txt"):
if line == "":
input_part_one = False
continue
if input_part_one:
regs[regidx] = int(line.split(": ")[1])
regidx += 1
else:
instrs = list(map(int, line.split(": ")[1].split(",")))
def combo(reg: List[int], val: int) -> int:
if val < 4:
return val
elif val == 4:
return reg[0]
elif val == 5:
return reg[1]
elif val == 6:
return reg[2]
else:
raise ValueError(f"Invalid combo: {val}")
def part_one(regs: List[int], instrs: List[int]):
regs = regs.copy()
out = []
ip = 0
while ip >= 0 and ip < len(instrs) - 1:
op = instrs[ip]
# print(ip, regs, op, instrs[ip + 1])
if op == 0:
regs[0] >>= combo(regs, instrs[ip + 1])
ip += 2
elif op == 1:
regs[1] = regs[1] ^ instrs[ip + 1]
ip += 2
elif op == 2:
regs[1] = combo(regs, instrs[ip + 1]) % 8
ip += 2
elif op == 3:
if regs[0] == 0:
ip += 2
else:
ip = instrs[ip + 1]
elif op == 4:
regs[1] = regs[1] ^ regs[2]
ip += 2
elif op == 5:
out.append(combo(regs, instrs[ip + 1]) % 8)
ip += 2
elif op == 6:
regs[1] = regs[0] >> combo(regs, instrs[ip + 1])
ip += 2
elif op == 7:
regs[2] = regs[0] >> combo(regs, instrs[ip + 1])
ip += 2
else:
raise ValueError(f"Invalid op: {op}")
return out
res = part_one(regs, instrs)
print(f"Part one: {','.join(map(str, res))}")