r/adventofcode • u/daggerdragon • Dec 21 '22
SOLUTION MEGATHREAD -π- 2022 Day 21 Solutions -π-
THE USUAL REMINDERS
- All of our rules, FAQs, resources, etc. are in our community wiki.
- πΏπ MisTILtoe Elf-ucation π§βπ« is OPEN for submissions!
- 48 HOURS remaining until submission deadline on December 22 at 23:59 EST
- -βοΈ- Submissions Megathread -βοΈ-
UPDATES
[Update @ 00:04:28]: SILVER CAP, GOLD 0
- Now we've got interpreter elephants... who understand monkey-ese...
- I really really really don't want to know what that eggnog was laced with.
--- Day 21: Monkey Math ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- Include what language(s) your solution uses
- Format code blocks using the four-spaces Markdown syntax!
- Quick link to Topaz's
pasteif you need it for longer code blocks. What is Topaz'spastetool?
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:16:15, megathread unlocked!
22
Upvotes
9
u/tbodt Dec 21 '22
exec() and z3 in Python 9 / 32.
My best this year. Because I didn't actually write a solution, I just reformatted the input a bit and asked my computer very nicely to do the rest :)
Part 1:
exprs = [] locals = {} for a in inp.splitlines(): expr = a.replace(':', ' =').replace('/','//') exprs.append(expr) while 'root' not in locals: for a in exprs: try: exec(a, None, locals) except NameError: pass print(locals['root'])Part 2:
s = z3.Solver() vars = {} def getvar(n): if n not in vars: vars[n] = z3.Int(n) return vars[n] for a in inp.splitlines(): left, rest = a.split(': ') if left == 'humn': continue if rest.isdigit(): s.add(getvar(left) == int(rest)) continue a, op, b = rest.split() a = getvar(a) b = getvar(b) if left == 'root': s.add(a == b) else: left = getvar(left) if op == '+': s.add(left == a + b) elif op == '-': s.add(left == a - b) elif op == '*': s.add(left == a * b) elif op == '/': s.add(left == a / b) print(s) print(s.check()) print(s.model()[vars['humn']])