-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.py
120 lines (95 loc) · 2.79 KB
/
day21.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
import util
import numpy as np
test_data: str = \
"""root: pppw + sjmn
dbpl: 5
cczh: sllz + lgvd
zczc: 2
ptdq: humn - dvpt
dvpt: 3
lfqf: 4
humn: 5
ljgn: 2
sjmn: drzm * dbpl
sllz: 4
pppw: cczh / lfqf
lgvd: ljgn * ptdq
drzm: hmdt - zczc
hmdt: 32"""
def calc(monkeys, monkey):
if type(monkeys[monkey]) is int:
return monkeys[monkey]
else:
vals = monkeys[monkey]
a = calc(monkeys, vals[0])
b = calc(monkeys, vals[2])
if vals[1] == "+":
return a + b
elif vals[1] == "-":
return a - b
elif vals[1] == "*":
return a * b
elif vals[1] == "/":
return a // b
def task1(input):
return calc(input, "root")
def reverse_search(monkeys, monkey, val):
if monkey == "humn":
return val
start_val = calc(monkeys, monkeys[monkey][0])
monkeys["humn"] += 100
side = 0 if start_val != calc(monkeys, monkeys[monkey][0]) else 2
vals = monkeys[monkey]
if side == 0:
if vals[1] == "+":
# val = a + b => a = val - b
val = val - calc(monkeys, vals[2])
elif vals[1] == "-":
# val = a - b => a = val + b
val = val + calc(monkeys, vals[2])
elif vals[1] == "*":
# val = a * b => a = val // b
val = val // calc(monkeys, vals[2])
elif vals[1] == "/":
# val = a // b => a = val * b
val = val * calc(monkeys, vals[2])
else:
if vals[1] == "+":
# val = a / b => b = val - a
val = val - calc(monkeys, vals[0])
elif vals[1] == "-":
# val = a - b => b = a - val
val = calc(monkeys, vals[0]) - val
elif vals[1] == "*":
# val = a * b => b = val // a
val = val // calc(monkeys, vals[0])
elif vals[1] == "/":
# val = a // b => b = a // val
val = calc(monkeys, vals[0]) // val
return reverse_search(monkeys, monkeys[monkey][side], val)
def task2(input):
start_val = calc(input, input["root"][0])
input["humn"] = 0
side = 0 if start_val != calc(input, input["root"][0]) else 2
start_val = calc(input, input["root"][2 - side])
input["humn"] = reverse_search(input, input["root"][side], start_val)
return input["humn"]
def parse(data: str):
lines = util.as_lines(data)
monkeys = {}
for line in lines:
halves = line.split(": ")
second_split = halves[1].split(" ")
if len(second_split) == 1:
monkeys[halves[0]] = int(second_split[0])
else:
monkeys[halves[0]] = second_split
return monkeys
def main():
data: str = util.get(21, 2022)
# data = test_data
input = parse(data)
print(task1(input))
print(task2(input))
if __name__ == "__main__":
main()