-
Notifications
You must be signed in to change notification settings - Fork 0
/
21.py
74 lines (60 loc) · 1.86 KB
/
21.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
from tqdm import tqdm
from utils.read_txt_data import txt_to_str
class MonkeyYelling:
def __init__(self, second=False):
data_str = txt_to_str("data/21.txt").split("\n")
self.data = {}
for line in data_str:
words = line.split(" ")
name = words[0][0:4]
if len(words) == 2:
number = int(words[1])
self.data[name] = number
else:
m1 = words[1]
op = words[2]
m2 = words[3]
self.data[name] = (m1, op, m2)
self.second = second
def reset_with_human(self, human_number):
self.data["humn"] = human_number
def decode(self, name, interior=False):
if type(self.data[name]) == int:
return self.data[name]
m1, op, m2 = self.data[name]
if self.second and interior and (m1 == "humn" or m2 == "humn"):
return "human"
v1 = self.decode(m1, interior=True)
v2 = self.decode(m2, interior=True)
if v1 == "human" or v2 == "human":
return "human"
if name == "root" and self.second:
result = v1 == v2
elif op == "+":
result = v1 + v2
elif op == "-":
result = v1 - v2
elif op == "*":
result = v1 * v2
else:
result = v1 / v2
return result
def find_human(self):
active_node = self.data["root"]
m1, op, m2 = active_node
if self.decode(m1) == "human":
relevant = m1
v = self.decode(m2)
assert v != "human"
else:
relevant = m2
v = self.decode(m2)
print("stop")
def first():
m = MonkeyYelling()
print(m.decode("root"))
def second():
m = MonkeyYelling(second=True)
m.find_human()
if __name__ == "__main__":
second()