diff options
author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2022-12-21 12:10:11 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2022-12-21 12:10:11 +0000 |
commit | 4e8eda742a6a34675232f8e8a3e54768d4efb95c (patch) | |
tree | 11fb1f9356b62c60ad63f970d43c8246e31a1af3 | |
parent | dfcdcf1e5dde06095fb5c3100f115737fb52fc4b (diff) | |
download | aoc2022-4e8eda742a6a34675232f8e8a3e54768d4efb95c.tar.gz aoc2022-4e8eda742a6a34675232f8e8a3e54768d4efb95c.tar.xz aoc2022-4e8eda742a6a34675232f8e8a3e54768d4efb95c.zip |
21
-rw-r--r-- | 21.py | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -0,0 +1,40 @@ +from functools import cache +from utils import open_day + +monkeys = {} +with open_day(21) as f: + for line in f: + target, expr = line.rstrip().split(': ') + expr = expr.split(' ') + if len(expr) == 1: + expr = int(expr[0]) + monkeys[target] = expr + +ops = { + '+': lambda a, b: a + b, + '-': lambda a, b: a - b, + '*': lambda a, b: a * b, + '/': lambda a, b: a // b, +} + +@cache +def eval_monkey(m): + expr = monkeys[m] + if isinstance(expr, int): + return expr + return ops[expr[1]](eval_monkey(expr[0]), eval_monkey(expr[2])) + +print(eval_monkey('root')) + +with open('21.z3', 'w') as f: + f.write(''.join(f'(declare-const {monkey} Int)\n' for monkey in monkeys.keys())) + for k, v in monkeys.items(): + if k == 'humn': continue + if k == 'root': + print(f'(assert (= {v[0]} {v[2]}))', file=f) + continue + if isinstance(v, int): + print(f'(assert (= {k} {v}))', file=f) + else: + print(f'(assert (= {k} ({v[1]} {v[0]} {v[2]})))', file=f) + print('(check-sat)\n(get-model)', file=f) |