-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path18a_aoc.py
59 lines (54 loc) · 1.26 KB
/
18a_aoc.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
import sys
regs = {}
lastPlayed = 0
program = []
def main():
for x in range(26):
regs[chr(ord('a')+x)] = 0
try:
while True:
getinputs(raw_input())
except EOFError:
solve()
def getinputs(arg):
program.append(arg)
def process(arg):
global lastPlayed
arg = arg.split()
print arg
if arg[0]=='snd':
lastPlayed = regs[arg[1]]
elif arg[0]=='set':
if arg[2] in regs:
regs[arg[1]] = regs[arg[2]]
else:
regs[arg[1]] = int(arg[2])
elif arg[0]=='add':
if arg[2] in regs:
regs[arg[1]] += regs[arg[2]]
else:
regs[arg[1]] += int(arg[2])
elif arg[0]=='mul':
if arg[2] in regs:
regs[arg[1]] *= regs[arg[2]]
else:
regs[arg[1]] *= int(arg[2])
elif arg[0]=='mod':
if arg[2] in regs:
regs[arg[1]] %= regs[arg[2]]
else:
regs[arg[1]] %= int(arg[2])
elif arg[0]=='rcv':
if lastPlayed != 0:
print lastPlayed
sys.exit(0)
elif arg[0]=='jgz':
if regs[arg[1]]>0:
return int(arg[2])
return 1
def solve():
i = 0
while i<len(program):
line = program[i]
i += process(line)
main()