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
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
3
u/flwyd Dec 21 '22
Elixir 2506/3402 (24 minutes, 2 hours), code, thoughts
Today's elixir:
I could see from the problem description that a graph was going to be involved, but I didn't feel like building a full expression tree. I solved the first part by including an anonymous function in each
Opstruct which looks up its left and right sides from a context map. For the second part I thought about pivoting the tree such that thehumnnode became the root and one side of the tree got inverted. That felt like more recursive logic than I had brain power for this evening, so I just addedleftandrightstring fields toOpand replaced the anonymous function with anoperandchar thatevaluatecould match on. I then built a set of node names which havehumnas a descendant. With that set, I evaluate the non-humnside of root, then pass that as thetargetvalue into a recursive function on thehumnside. That function returnstargetwhen it reaches thehumnleaf, returns the literal value for literals, and otherwise evaluates the non-humnside, applies an inverted arithmetic operator to it, and calls thehumnside with the result as the new target.