-
Notifications
You must be signed in to change notification settings - Fork 0
/
23.py
73 lines (46 loc) · 1.46 KB
/
23.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 lib import *
input = read_input(2016, 23)
program = input.splitlines()
registers = {k: 0 for k in "abcd"}
def simulate():
pc = 0
while pc < len(program):
cmd, x, y, *_ = program[pc].split() + [None]
read = lambda a: registers[a] if a in registers else int(a)
if cmd == "cpy":
if y in registers:
registers[y] = read(x)
pc += 1
elif cmd == "inc":
if x in registers:
registers[x] += 1
pc += 1
elif cmd == "dec":
if x in registers:
registers[x] -= 1
pc += 1
elif cmd == "jnz":
if read(x) and y is not None:
pc += read(y) or 1
else:
pc += 1
elif cmd == "tgl":
if pc + (x := read(x)) in range(len(program)):
cmd, args = program[pc + x].split(maxsplit=1)
if cmd == "inc":
cmd = "dec"
elif cmd in ["dec", "tgl"]:
cmd = "inc"
elif cmd == "jnz":
cmd = "cpy"
elif cmd in ["cpy"]:
cmd = "jnz"
program[pc + x] = f"{cmd} {args}"
pc += 1
return registers["a"]
registers["a"] = 7
print(simulate())
program = input.splitlines()
registers = {k: 0 for k in "abcd"}
registers["a"] = 7
print(math.factorial(12) + simulate() - math.factorial(7))