-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18-2.py
101 lines (85 loc) · 2.44 KB
/
18-2.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
a = open('18.txt').read()
a = a.split('\n')
class Program:
queues = {'0': [], '1': []}
states = {'0': 'working', '1': 'working'}
def __init__(self, no):
self._no = str(no)
self._reg = {'p': no}
self._i = 0
self._running = True
self._nb_sent_values = 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._nb_sent_values += 1
if self._no == '0':
Program.queues['1'].append(X)
elif self._no == '1':
Program.queues['0'].append(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):
Program.states[self._no] = 'waiting'
if len(Program.queues[self._no]) == 0:
self._i -= 1
return
X = args[0]
self._reg[X] = Program.queues[self._no].pop(0)
Program.states[self._no] = 'working'
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(0)
p1 = Program(1)
while any([Program.states[x] == 'working' for x in Program.states]):
p0._execute(a[p0._i])
p1._execute(a[p1._i])
p0._i += 1
p1._i += 1
print p1._nb_sent_values