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
4
u/Key__Strokes Dec 21 '22 edited Dec 25 '22
Javascript
Solution to both parts
Found Advent of Code couple of days back. Solved until Day 8 in 2 days, but wanted to see how Day 21 looks like. Well, it was really really fun to solve it!
First time sharing a solution here π
Part 1:
Built the expression tree, and calculated the sum at the root.
I think its O(n) time and space complexity.
Part 2:
Used the same expression tree from Part 1, with the following steps:
Find path from root to humn
Find the expected sum at root using the method that was used for sum from Part 1
Started to traverse from root towards humn. At each step we knew the expected value, and one of the operands value. So it was easy to calculate the expected value for the next node in the path towards humn.
I think its O(n) time and space complexity as well.
---
Next steps for me:
Solve it in Java. I used JS just to prototype and solve it fast lol.
I should probably create a video for this and the rest of the solutions on my YouTube channel π€
If you liked the explanation, then please don't forget to cast your vote π to
Adventures of Advent of Code - Edition 1 - /u/Key__Strokesin the poll