Day 7, part 2
Day 7 challenge of the mighty Advent of Code 2024
Created at:
Last updated:
Table of Contents
Introduction
Welcome to the par two of day 7 puzzle. First part is described in Day 7, part 1. Please get familiar with the part 1 before jumping into this article. This part of the challenge is available only when you successfully finished the part 1. The second part of the challenge is described under section here.
What is the new problem
Now some rules have changed. The card is now treated as a wildcard and it pretends to be the card that gives the best type so is treated as . The second rule for is that even tho it is pretending to be some other card, when it comes to comparing individual card strengths, it is now the weakest card, weaker even than . Our goal is again to find the sum of bids, but now we have to incorporate those new rules.
Solution
The first thing we can easily do is to change of card in object so in the card comparison step when we have two hands with the same type and we are comparing the cards themselves one by one, the card will have the lowest . This will fulfill one of the new rules. The second thing is to somehow find the best card in the hand to replace the with and use this to properly sort and rank each hand.
Find and replace
This is a pretty complex task. It has to be broken into steps:
- We need to get the card counts using the function.
- We replace the number of s in the count with so it's not taken into consideration in the next step.
- Based on the card counts we find the best card to replace the with by finding the keys with the biggest number of cards.
- From those keys, we pick one by checking card strengths.
- Finally we replace all the cards s with our best key we found. Looking at our test data we can expect the following results:
In code this can be done like so:
And if we run the code this is what will be printed:
Look closely, what we get here is what we were looking for! Lets save it.
Updating the type getting
Now we have the new hands with all the s replaced by the card that makes the hand the strongest possible. Thanks to that we can find a type for every hand. If we analyse the data from the previous step, this are the new types (I assign them to the initial hands):
For updating the hand types we will again use the function which I decided to rename to cause it was more clear to me.
As you can see now it takes as a parameter the hands with replaced . These are used to determine the hand type now. We can run our program to now see in the terminal all our hands but with the new types.
This is the expected result. We can commit the changes.
The new final result
Actually running the whole program as we did a few seconds before, we got also the new final answer printed. If we check the expected result on the puzzles website we can find that it is now. And if we take a look at the terminal we see:
so this is again what we were looking for. Hooray!
Final result
Now we can switch back to the input being read from the file and we can make the final run of our code.
We now submit this number on the page and we can see our favorite message.
Lets commit all the changes.
And we nailed it once again! Hope you enjoyed the puzzle and see you in Day 8, part 1