-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21-02.py
77 lines (77 loc) · 2.97 KB
/
21-02.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
from collections import deque
deps = {}
ops = {}
preds = {}
empty_queue = deque()
while True:
inputval = input()
if inputval == "": break
vals = inputval.split()
monkeyId = vals[0][:-1]
if monkeyId == "humn":
ops[monkeyId] = monkeyId
empty_queue.append(monkeyId)
elif len(vals) == 2:
ops[monkeyId] = int(vals[1])
empty_queue.append(monkeyId)
else:
if vals[1] not in preds:
preds[vals[1]] = []
preds[vals[1]].append(monkeyId)
if vals[3] not in preds:
preds[vals[3]] = []
preds[vals[3]].append(monkeyId)
deps[monkeyId] = 2
ops[monkeyId] = vals[1:]
while empty_queue:
monkeyId = empty_queue.popleft()
if type(ops[monkeyId]) == list:
if type(ops[ops[monkeyId][0]]) is int and type(ops[ops[monkeyId][2]]) is int:
if ops[monkeyId][1] == "+":
ops[monkeyId] = ops[ops[monkeyId][0]] + ops[ops[monkeyId][2]]
elif ops[monkeyId][1] == "-":
ops[monkeyId] = ops[ops[monkeyId][0]] - ops[ops[monkeyId][2]]
elif ops[monkeyId][1] == "*":
ops[monkeyId] = ops[ops[monkeyId][0]] * ops[ops[monkeyId][2]]
elif ops[monkeyId][1] == "/":
ops[monkeyId] = ops[ops[monkeyId][0]] // ops[ops[monkeyId][2]]
else:
ops[monkeyId][0] = ops[ops[monkeyId][0]]
ops[monkeyId][2] = ops[ops[monkeyId][2]]
if monkeyId == "root":
target, op_left = None, None
if type(ops[monkeyId][0]) is int:
target, op_left = ops[monkeyId][0], ops[monkeyId][2]
else:
target, op_left = ops[monkeyId][2], ops[monkeyId][0]
assert type(target) is int, "target must be int"
# print(target)
# print(op_left)
while type(op_left) is list:
if op_left[1] == "+":
if type(op_left[0]) is int:
target, op_left = target - op_left[0], op_left[2]
else:
target, op_left = target - op_left[2], op_left[0]
elif op_left[1] == "-":
if type(op_left[0]) is int:
target, op_left = op_left[0] - target, op_left[2]
else:
target, op_left = target + op_left[2], op_left[0]
elif op_left[1] == "*":
if type(op_left[0]) is int:
target, op_left = target // op_left[0], op_left[2]
else:
target, op_left = target // op_left[2], op_left[0]
elif op_left[1] == "/":
if type(op_left[0]) is int:
target, op_left = op_left[0] // target, op_left[2]
else:
target, op_left = target * op_left[2], op_left[0]
print(target)
break
if monkeyId in preds:
for newMonkey in preds[monkeyId]:
deps[newMonkey] -= 1
if deps[newMonkey] == 0:
empty_queue.append(newMonkey)