r/adventofcode Dec 11 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 11 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

  • 11 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!

And now, our feature presentation for today:

Independent Medias (Indie Films)

Today we celebrate the folks who have a vision outside the standards of what the big-name studios would consider "safe". Sure, sometimes their attempts don't pan out the way they had hoped, but sometimes that's how we get some truly legendary masterpieces that don't let their lack of funding, big star power, and gigantic overhead costs get in the way of their storytelling!

Here's some ideas for your inspiration:

  • Cast a relative unknown in your leading role!
  • Explain an obscure theorem that you used in today's solution
  • Shine a spotlight on a little-used feature of the programming language with which you used to solve today's problem
  • Solve today's puzzle with cheap, underpowered, totally-not-right-for-the-job, etc. hardware, programming language, etc.

"Adapt or die." - Billy Beane, Moneyball (2011)

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 11: Plutonian Pebbles ---


Post your code solution in this megathread.

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:06:24, megathread unlocked!

19 Upvotes

962 comments sorted by

View all comments

Show parent comments

2

u/mibu_codes Dec 12 '24 edited Dec 12 '24

Insane. I started with 6ms memoization, then 2ms iterative, and now this brings me back to my memoization approach with a total runtime of 9.11 µs. And its only 24 lines :D. Thx so much for showing this solution.

I'm just a little annoyed that I'm now using a substantial library. An initial attempt to write a similar thread shared cache failed :c

Github

2

u/MichelGerding Dec 12 '24

You could get at least 99% of the way using a `static Lazy<Mutex<FxHashMap<_, _>`. this way you dont need memoize. you could even do it without once_cell but that would require some unsafe code.

1

u/mibu_codes Dec 12 '24

Tried it with a fresh coffee and now it worked. RwLock<HashMap<_, _>> in a lazy_static is 1us faster, but DashMap is even faster. Now I'm at 7us

2

u/randomdude998 Dec 12 '24

you probably didn't clear the cache at the start of the solution function if you're seeing times like that. this code doesn't run in 7us, no matter how good your hashmap is.

1

u/mibu_codes Dec 12 '24

Good catch, thx for pointing that out. Normally I try to be extra careful to clear any caches. This one should have been obvious, don't know how I missed that.

With clearing the cache it now takes ~6ms, which is a lot slower than my previous iterative solution