DEV Community

Cover image for Advent of Code 2024 - Day 11: Plutonian Pebbles
Grant Riordan
Grant Riordan

Posted on

6 1 1

Advent of Code 2024 - Day 11: Plutonian Pebbles

Day 11: Plutonian Pebbles

Solution can be found here

Like many others I started with a simple brute force using a List, and replacing the values using a switch, case statement, however I knew I'd be re-writing this in Part2, 'cos from experience it's going to throw a huge number of iterations at us.

Hopefully this will be helpful and offer some guidance on how we need to thnk about solutions going forward, or try and write code which may be suitable for more than one use-case.

 Part 1 - Bash it Out

Brute-forcing in this Challenge happens by checking every stone and converting (within the ruleset). and for a small number of blinks is doable. However, as the number of blinks grows, it quickly becomes a massive computational task.

Part 2

Instead, we can optimise the process by grouping identical stones. For instance, if we have three '10's, we can split one of them and then simply update the count of the resulting stones. This avoids redundant calculations for identical stones.

To achieve this, we can use a dictionary to track the count of each stone value. This way, we only need to process the unique stone values and their corresponding counts. In essence, you're doing the logic once, and then multiplying this logic by 'x' count.

Example:
Say we had gotten to blink 182 and had 19 number 10 -> we split the number to 1 and 0 only -> adding both these numbers to our original stones for the next blink -> but instead of doing this 19 time, we just update the count for each 1 and 0 to 19.

Because we're passing / updating the original stones to the the Blink method, we don't want to loop over this as we're processing and applying logic. We can create a copy of the stones dictionary and iterate over that, making updates to the original dictionary, preventing any concurrency issues.

As always feel free to subscribe here, or follow me on Twitter where you can hear more about my other articles on FreeCodeCamp, DevTo along with other tech discussions.

Billboard image

Monitoring as code

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

Top comments (1)

Collapse
 
miles_hitchen_3bc87e32fc5 profile image
miles hitchen

Yep, I used brute-force for part 1 and tried it for part 2, until my PC ran out of memory! I tried all sorts of recursion, and saving the state to file. Thanks for pointing out a much simpler and crazy fast approach

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay