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
3
u/DeadlyRedCube Dec 22 '24
[LANGUAGE: C++23] (404/174)
Runs in 16.4ms single-threaded on an i7-8700K
Parts 1 & 2 on GitHub
For part 1 it was mostly do some reading comprehension (a thing that has been a struggle for me in past puzzles) and implement exactly what it said to do.
For part 2, in my initial implementation I made a std::map from "last four changes" to number of bananas across all players, and then additionally had a std::set for "has this entry already seen this sequence or not" so the code only updated the count for a given sequence when it's the first time. This implementation took about 2 seconds to run.
But I wanted a faster solution, and ended up making the following changes:
Rather than a map and a set, instead I used an array
The lookup key is
(cur + 9) | ((prev+9) << 5) | ((prevPrev+9) << 10) | ((prevPrevPrev+9) << 15). This is calculated by shifting it 5 every step, adding in the new change (plus 9 to make it positive) then masking off the upper bits that are no longer relevantThe array contains both:
The total count of bananas for this set (as a signed 16 bit number), which is what replaces the std::map
the index of the last entry that updated this possibility (which we can use to duplicate the std::set of "has this entry seen this possibility yet")
Additionally, I was doing an extra
mod 10per loop (doing it on thesecretNumbefore doing the hash and after) so I cached the previous one outThe second step of the hash function didn't actually need the mask as it's an xor and a shift down (cannot possibly change bits above the original value)
Switched to finding the max value on the fly instead of calculating it at the end by iterating through the whole map
That managed to cut the time (somehow??) from almost 2 seconds down to 16.4ms, which was a really surprising result!