-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_21_2.py
29 lines (24 loc) · 1.06 KB
/
day_21_2.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
# Copyright Hector Bravo <[email protected]>
# Code for Day 21 excercise 2 https://adventofcode.com/2022/day/21
import adv_common as common
import day_21_1
from sympy import solve
def get_monkey_expr(_monkey, _monkey_dict):
if _monkey_dict[_monkey].isnumeric():
return _monkey if _monkey == 'humn' else _monkey_dict[_monkey]
monkey1, operation, monkey2 = _monkey_dict[_monkey].split()
_monkey_dict[monkey1] = get_monkey_expr(monkey1, _monkey_dict)
_monkey_dict[monkey2] = get_monkey_expr(monkey2, _monkey_dict)
if _monkey == 'root':
# Assume root operation will always be sum
operation = '-'
return '(' + _monkey_dict[monkey1] + operation + _monkey_dict[monkey2] + ')'
@common.elapsed_time_factory()
def process_solution(_contents):
# print('Contents:', _contents)
monkey_dict = day_21_1.generate_monkey_dict(_contents)
return solve(get_monkey_expr('root', monkey_dict))[0]
if __name__ == "__main__":
contents = common.read_input()
result = process_solution(contents)
common.print_result(result, 3330805295850)