-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
25 lines (18 loc) · 754 Bytes
/
main.py
File metadata and controls
25 lines (18 loc) · 754 Bytes
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
with open('input.txt') as file:
lines = {monkey: op for monkey, op in [line.split(': ') for line in file.read().rstrip().splitlines()]}
root1, _, root2 = lines['root'].split(' ')
FIND = 'humn'
def solve_equation(eq):
z = eval(eq.replace(FIND, 'j').replace('=', '-(') + ')', {'j': 1j})
return round(z.real / -z.imag)
def solve(monkey, part2=False):
expr = lines[monkey]
if (monkey == FIND) and part2:
return FIND
if expr.isdigit():
return int(expr)
else:
p1, op, p2 = expr.split(' ')
return f'({solve(p1, part2=part2)}{op}{solve(p2, part2=part2)})'
print(int(eval(solve('root'))))
print(solve_equation(f'{solve(root1, part2=True)} = {solve(root2, part2=True)}'))