DEV Community

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

Posted on

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.

Top comments (0)