-
Notifications
You must be signed in to change notification settings - Fork 0
/
18-1.py
86 lines (72 loc) · 1.95 KB
/
18-1.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
74
75
76
77
78
79
80
81
82
83
84
85
86
a = open('18.txt').read()
a = a.split('\n')
class Program:
def __init__(self):
self._reg = {}
self._sound = None
self._reveived = []
self._i = 0
def _arg_value(self, arg):
if arg.replace('-', '').isdigit():
return int(arg)
elif arg in self._reg:
return self._reg[arg]
else:
return 0
def _validate_reg(self, reg):
if not reg in self._reg:
self._reg[reg] = 0
def _set(self, args):
X = args[0]
Y = self._arg_value(args[1])
self._reg[X] = Y
def _snd(self, args):
X = self._arg_value(args[0])
self._sound = X
def _add(self, args):
X = args[0]
Y = self._arg_value(args[1])
self._validate_reg(X)
self._reg[X] += Y
def _mul(self, args):
X = args[0]
Y = self._arg_value(args[1])
self._validate_reg(X)
self._reg[X] *= Y
def _mod(self, args):
X = args[0]
Y = self._arg_value(args[1])
self._validate_reg(X)
self._reg[X] %= Y
def _rcv(self, args):
X = self._arg_value(args[0])
if X != 0:
self._reveived.append(self._sound)
print self._sound
exit(0)
def _jgz(self, args):
X = self._arg_value(args[0])
Y = self._arg_value(args[1])
if X > 0:
self._i += (Y - 1)
def _execute(self, line):
cmd = line[:3]
args = line[4:].split()
if cmd == 'set':
self._set(args)
elif cmd == 'snd':
self._snd(args)
elif cmd == 'add':
self._add(args)
elif cmd == 'mul':
self._mul(args)
elif cmd == 'mod':
self._mod(args)
elif cmd == 'rcv':
self._rcv(args)
elif cmd == 'jgz':
self._jgz(args)
p0 = Program()
while p0._i < len(a):
p0._execute(a[p0._i])
p0._i += 1