Day 2, part 2
Day 2 challenge of the mighty Advent of Code 2024
Created at:
Last updated:
Table of Contents
Introduction
This article is a continuation of the Day 2, part 1, so if you haven't please read that first as we are going to use the code from there. 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, we are asked to check in each game, what is the smallest amount of cubes of each color, to make the game possible. Lets take a look at the example data once more.
With the new rules, we get the following answers to that question:
That basically mean, that we have to find out the highest amount of cubes of each color in the game rounds. Next we are asked, based on the data we get, calculate the power of each set of cubes, which is calculated by multiplying the minimal amounts of cubes to make the game possible. In our example the powers we get for each game are:
And finally, to get the answer to the second part of this puzzle, we are asked to return the sum of these powers. In our case, the expected answer is .
Solution
Find minimal amount of cubes
Previously we were checking game possibilities by checking if the amounts of cubes in each color are not exceeding the given amounts for each color. This time we are not going to check any possibilities, but instead we need to find out the minimal amounts ourselves based on the cubes in each round in each game.
In the snippet above, we can see a simple function that takes a game, goes through all rounds and replaces the result for each color if it is higher than the previous result. Now we create a new main function which will for now use the above function. We also want to again, use the test data instead of reading the file.
After running this code now, the following data is being logged in the console:
And these are the results we expected. Hooray! We can commit the changes.
Calculating the power factor
Now, when we have the minimal amounts of cubes to make a game possible, we can calculate the power factor for each of them. To do so we simply have to multiply the amount of cubes in each color.
Now lets use this in the main function and see what results do we get.
This is the resounding success, we did it! Again, let's commit the changes.
Summing up the powers
The final step to get the answer to this puzzle, is to sum up all the powers. Fortunately this is pretty easy, simply by using method on the powers array.
This now can be used in the main function.
Now the final check can be done. Fingers crossed!
Success! We get the correct answer. Now is the time for a real test of our new program. We need to run it using the real data. But before that, time to save our progress.
Final result
Now we can replace the test data with the data from . Here is the full code we have which has code for both parts of the day 2 puzzle.
Running this will give us logs with answers for both parts, but what we are interested in is:
And after submitting this answer we now now that we produced a piece of a properly working code, as our answer is correct! Congratulations!
The final step is to commit our final code.
That was a lot of fun! If you are interested in the next puzzle, please check out Day 3, part 1