Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 toblink 182 and had 19 number10 -> we split the number to1 and0 only -> adding both these numbers to ouroriginal stones for the next blink -> but instead of doing this 19 time, we just update the count for each1 and0 to 19.

Because we're passing / updating theoriginal 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 onTwitter where you can hear more about my other articles on FreeCodeCamp, DevTo along with other tech discussions.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
miles_hitchen_3bc87e32fc5 profile image
miles hitchen
  • Joined

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

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Lvl 11 - Lead Software Engineer & Lego FanaticTwitter link below 👇
  • Location
    Yorkshire, England
  • Education
    BSc(Hons) in Applied Computing
  • Work
    Application Development Specialist
  • Joined

More fromGrant Riordan

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp