-
Notifications
You must be signed in to change notification settings - Fork 0
/
18.py
143 lines (122 loc) · 3.85 KB
/
18.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
from collections import Counter, defaultdict, deque
from itertools import permutations, combinations, product
from functools import reduce
def val(R, c):
try:
return int(c)
except ValueError:
return R[c]
def main(t):
R = defaultdict(int)
i = 0
last_sound = 0
while i < len(t):
line = t[i].split()
cmd, args = line[0], line[1:]
if cmd == 'snd':
last_sound = val(R, args[0])
elif cmd == 'set': R[args[0]] = val(R, args[1])
elif cmd == 'add': R[args[0]] += val(R, args[1])
elif cmd == 'mul': R[args[0]] *= val(R, args[1])
elif cmd == 'mod': R[args[0]] %= val(R, args[1])
elif cmd == 'rcv':
if val(R, args[0]) != 0:
print('last_sound:', last_sound)
return
elif cmd == 'jgz':
if val(R, args[0]) > 0:
i += val(R, args[1])
continue
else:
assert False, "unknown cmd %s" % cmd
i += 1
print('exiting main without rcv')
def main2(t):
R1 = defaultdict(int)
R2 = defaultdict(int)
i1 = 0
i2 = 0
msg1 = deque() # sent to 1
msg2 = deque() # sent to 2
finished1 = False
finished2 = False
values_sent = 0
# initialize p variable
R1['p'] = 0
R2['p'] = 1
# check for termination
lol = 0
while not(finished1 and finished2):
#print('entering main loop')
if not msg1 and not msg2:
lol += 1
if lol > 10:
print('breaking on probable deadlock')
break
# execute program 1
while i1 < len(t):
line = t[i1].split()
cmd, args = line[0], line[1:]
if cmd == 'snd':
msg2.append(val(R1, args[0]))
elif cmd == 'set': R1[args[0]] = val(R1, args[1])
elif cmd == 'add': R1[args[0]] += val(R1, args[1])
elif cmd == 'mul': R1[args[0]] *= val(R1, args[1])
elif cmd == 'mod': R1[args[0]] %= val(R1, args[1])
elif cmd == 'rcv':
if msg1: R1[args[0]] = msg1.popleft()
else: break
elif cmd == 'jgz':
if val(R1, args[0]) > 0:
i1 += val(R1, args[1])
continue
else:
assert False, "unknown cmd %s" % cmd
i1 += 1
else:
print('finished 1')
finished1 = True
# execute program 2
while i2 < len(t):
line = t[i2].split()
cmd, args = line[0], line[1:]
if cmd == 'snd':
msg1.append(val(R2, args[0]))
values_sent += 1
elif cmd == 'set': R2[args[0]] = val(R2, args[1])
elif cmd == 'add': R2[args[0]] += val(R2, args[1])
elif cmd == 'mul': R2[args[0]] *= val(R2, args[1])
elif cmd == 'mod': R2[args[0]] %= val(R2, args[1])
elif cmd == 'rcv':
if msg2: R2[args[0]] = msg2.popleft()
else: break
elif cmd == 'jgz':
if val(R2, args[0]) > 0:
i2 += val(R2, args[1])
continue
else:
assert False, "unknown cmd %s" % cmd
i2 += 1
else:
print('finished 2')
finished2 = True
print('exited loop, finished:', finished1, finished2)
print('values_sent:', values_sent)
if __name__ == '__main__':
# sample input
print('Sample input:')
t = open('sample.txt').read().rstrip()
t = t.splitlines()
print('> Star 1:')
main(t)
print('> Star 2:')
main2(t)
# actual input
print('\nActual input:')
t = open('input.txt').read().rstrip()
t = t.splitlines()
print('> Star 1:')
main(t)
print('> Star 2:')
main2(t)