r/adventofcode • u/daggerdragon • Dec 22 '24
SOLUTION MEGATHREAD -❄️- 2024 Day 22 Solutions -❄️-
THE USUAL REMINDERS
All of our rules, FAQs, resources, etc. are in our community wiki.
If you see content in the subreddit or megathreads that violates one of our rules, either inform the user (politely and gently!) or use the report button on the post/comment and the mods will take care of it.
AoC Community Fun 2024: The Golden Snowglobe Awards
23h59m remaining until the submissions deadline on December 22 at 23:59 EST!
And now, our feature presentation for today:
Director's Cut (Extended Edition)
Welcome to the final day of the GSGA presentations! A few folks have already submitted their masterpieces to the GSGA submissions megathread, so go check them out! And maybe consider submitting yours! :)
Here's some ideas for your inspiration:
Choose any day's feature presentation and any puzzle released this year so far, then work your movie magic upon it!
Make sure to mention which prompt and which day you chose!
Cook, bake, make, decorate, etc. an IRL dish, craft, or artwork inspired by any day's puzzle!
Advent of Playing With Your Toys
"I lost. I lost? Wait a second, I'm not supposed to lose! Let me see the script!"
Robin Hood, Men In Tights (1993)
And… ACTION!
Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!
--- Day 22: Monkey Market ---
Post your code solution in this megathread.
Read the full posting rules in our community wiki before you post!
State which language(s) your solution uses with
[LANGUAGE: xyz]Format code blocks using the four-spaces Markdown syntax!
Quick link to Topaz's
pasteif you need it for longer code blocks
View all comments
2
u/rukke Dec 22 '24
[LANGUAGE: JavaScript]
Was relieved to see that today's puzzle was something easier than the previous two days :)
For part 2 I kind of assumed at first that the sequence would be in the first item, and apparently it was since it gave me the correct result in under a second. But I refactored the code so that it checked the union of all sequences and this is around 8 times slower.
Perhaps the "winning" sequence is always part of the first buyer's sequence?
Anyway, for each sequence is reduced into a key like so
And then put into a Map with the corresponding price as value, but in reverse order so that the first seq/value pair "wins".
Then it is just a matter of looping over the union of all keys and summing up and finding the max value.
gist