-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday18.py
More file actions
49 lines (42 loc) · 1.09 KB
/
Copy pathday18.py
File metadata and controls
49 lines (42 loc) · 1.09 KB
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
#!/usr/bin/env python3
instrs = open("day18.txt", "r").readlines()
ip = 0
freq = None
registers = {}
def get_rhs(s):
try:
b = int(s)
except ValueError:
b = registers.get(s, 0)
return b
while ip < len(instrs):
ins, *op = instrs[ip].strip().split(" ")
print("%u: %s %s" % (ip, ins, op))
if ins == "snd":
freq = get_rhs(op[0])
ip += 1
elif ins == "set":
registers[op[0]] = get_rhs(op[1])
ip += 1
elif ins == "add":
registers[op[0]] = registers.get(op[0], 0) + get_rhs(op[1])
ip += 1
elif ins == "mul":
registers[op[0]] = registers.get(op[0],0) * get_rhs(op[1])
ip += 1
elif ins == "mod":
registers[op[0]] = registers.get(op[0],0) % get_rhs(op[1])
ip += 1
elif ins == "rcv":
if freq != 0:
print("recovered %s" % freq)
break
else:
ip += 1
elif ins == "jgz":
if get_rhs(op[0]) > 0:
ip += get_rhs(op[1])
print("jumping to ip = %u" % ip)
else:
ip += 1
print(registers)