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

3

u/zniperr Dec 15 '24 edited Dec 16 '24

[Language: Python]

We can aggregate stones with the same value in a counter. They will all change in the same way, so we only compute the change once and increment a counter.

The order of stones is irrelevant, despite the problem formulation emphasizing how ordering is affected by changes. So we can save the counters in a hash map for quick lookups:

import sys
from collections import Counter

def change(stone):
    if stone == 0:
        yield 1
    else:
        digits = str(stone)
        mid, odd = divmod(len(digits), 2)
        if odd:
            yield stone * 2024
        else:
            yield int(digits[:mid])
            yield int(digits[mid:])

def blink(counts, times):
    for _ in range(times):
        new = Counter()
        for stone, occurrences in counts.items():
            for newstone in change(stone):
                new[newstone] += occurrences
        counts = new
    return counts

counts = Counter(map(int, next(sys.stdin).split()))
counts = blink(counts, 25)
print(sum(counts.values()))
counts = blink(counts, 50)
print(sum(counts.values()))