Day 4, part 2
Day 4 challenge of the mighty Advent of Code 2024
Created at:
Last updated:
Table of Contents
Introduction
Welcome to part two of Advent of code day 4 puzzle. This is a continuation of what we have done in Day 4, part 1. I recommend reading that one first before you jump into this one. 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
After successfully counting points for each scratchcard in part 1, now the rules has changed. Now we are not counting points based on the amount of matching numbers, but instead we are making copies of the cards that are below the current card. We copy as many cards as many matches the current card has. For instance, lets take a look at the test data again.
- Card 1 has four matching numbers, so you win one copy each of the next four cards: cards 2, 3, 4, and 5.
- Your original card 2 has two matching numbers, so you win one copy each of cards 3 and 4.
- Your copy of card 2 also wins one copy each of cards 3 and 4.
- Your four instances of card 3 (one original and three copies) have two matching numbers, so you win four copies each of cards 4 and 5.
- Your eight instances of card 4 (one original and seven copies) have one matching number, so you win eight copies of card 5.
- Your fourteen instances of card 5 (one original and thirteen copies) have no matching numbers and win no more cards.
- Your one instance of card 6 (one original) has no matching numbers and wins no more cards. With all of these, we end up having copy of , copies of , copies of , copies of , copies of and copy of . To get the final result we are asked to count all the card that we end up having.
Solution
To keep track of all the copies, we need to keep track of how many instances of a card we have. We can start by reusing the data parsing part but for each card we can introduce new property and initialize it with . Then we can move through the cards, reuse the functions responsible for counting how many matches we have and based on that, we can increment the number of copies below the current card. At the end we can count all the copies we have to get the answer to this puzzle.
Keep track of copies
Before we start, lets once more use the test data instead of reading the input file.
Here I list some changes we have to make to our functions to keep track of number.
I also found one bug in our function:
One of the console logs we had, now shows us this cards data:
We have here all card , initial amount of which we will use in the next steps and the amount of matching numbers for each card.
Copying the cards
How do we handle the copying? The tells us how many copies we need to add to to the cards below. The tells us how many cards below the current card have to be copied. We will be modifying the card objects directly, because in this case it seems to be a little easier.
The main is simply taking all the cards and calls the main updating function on each of them. This function finds the last possible of a card and then goes through the cards below the current card. Each of these cars has its parameter updated. We are using the variable as a parameter when we call the function. If we run this now, that's what we will see:
Looks about right. It means we can commit the changes and move to the last step.
Count how many cards we ended up with
This is pretty simple task right now and can be simply done using method on the array of cards after successful copies update.
If we run this, the message will be printed in the terminal:
If we now check the expected number of cards we calculated manually in the introduction, we can see that this is the answer we were looking for. We can save the changes.
Final result
It is time, to finally try our code out on the real input data. Once again we will make a use of function.
The moment of truth has come.
After submitting the result, we are greeted once again! That's the right answer!
The final step is to commit our final code.
And hopefully see you all in Day 5, part 1!