-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path23.py
55 lines (50 loc) · 1.89 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
"""--- Day 23: Safe Cracking ---"""
def solve(instructions, registers):
# intialise program counter to zero
cnt = 0
while cnt < len(instructions):
inst = instructions[cnt].split()
if inst[0][0] == "c": # cpy
if inst[1] in registers:
registers[inst[2]] = registers[inst[1]]
else:
registers[inst[2]] = int(inst[1])
elif inst[0][0] == "i": # inc
registers[inst[1]] += 1
elif inst[0][0] == "d": # dec
registers[inst[1]] -= 1
elif inst[0][0] == "j": # jnz
if inst[2] in registers:
jmp = registers[inst[2]]
else:
jmp = int(inst[2])
if inst[1] in registers:
if registers[inst[1]] != 0:
cnt += jmp
continue
else:
if int(inst[1]) != 0:
cnt += jmp
continue
else: # tgl
jmp = cnt + registers[inst[1]]
if jmp < len(instructions):
tgl = instructions[jmp]
inst = tgl.split()
if len(inst) == 2:
if inst[0] == "inc":
instructions[jmp] = "dec " + inst[1]
else:
instructions[jmp] = "inc " + inst[1]
else:
if inst[0] == "jnz":
instructions[jmp] = "cpy " + " ".join(inst[1:])
else:
instructions[jmp] = "jnz " + " ".join(inst[1:])
# increment program counter
cnt += 1
return registers["a"]
with open("input/23.txt", "r") as f:
instructions = [x[:-1] for x in f] # drop "\n"
print(solve(instructions, {"a" : 7, "b" : 0, "c" : 0, "d" : 0}))
print(solve(instructions, {"a" : 12, "b" : 0, "c" : 0, "d" : 0}))