Day 2, part 1
Day 2 challenge of the mighty Advent of Code 2024
Created at:
Last updated:
Table of Contents
Introduction
The second day challenge can be found here. Read the description carefully to understand the problem we want to solve. Basically we are asked to figure out if in the given games, if the games were "possible". A game is possible if in the given game, the amount cubes in each color doesn't exceed the given maximal of cubes of that color. Each game has an which is a number and as a solution to this puzzle, we are asked to return the sum of s of all possible games.
Understanding the problem
Let's take a look at some example data:
and the example maximal amounts for each color:
So we take at look at every round in each game and check if none of the colors in each round exceeds the maximal amount. Doing so we can see that games and are impossible because in game in first round we have red cubes and in game in second round we have blue cubes. All the other games are possible. So now summing up the s of all the possible games we get which is the expected result.
Creating files for the challenge
We are going to start by creating the new directory in our project's folder.
In the directory lets add a new file which will be our main entry for the code for this puzzle.
We can already also place our test data in the variable.
Solution
Preparing the input data
To begin, we first need to read the given data and split it into lines, to separate games from each other. To do so lets reuse the function from Day 1, part 1#Preparing the input data
Running this will result in following log in the terminal:
Parsing the game data
As we now have data for each game stored in a separate string in the array, we can take these strings one by one and parse them so we can easily reuse them later.
The given piece of code return this result:
for now we just split the game into its and . The could be easily used to extract the game , the game data was for now left as the rest of the spited game string. Now we can start thinking how to extract the data from the string and turn it into array of object describing amount of each color in each round of the game.
And running this piece of code now, we are getting:
I think this is the time we can commit our progress before we move forward.
Check if game is possible
Now having the data parsed, lets figure out if a given game was possible (none of the colors in the given rounds exceeds the maximal amount). We can achieve this with these couple of functions:
Let's check this out now.
Now we can do a little bit of clean up so we can check out the results for the whole test data. The whole code so far after the changes looks like this:
and the result we get is:
As we can see the results are correct and we commit the changes and happily move to the last step.
Summing up the possible games ids
Now having the information of which game is possible we can go through that data and add the s of possible games together. We need to update the function and the message.
Lets now run the code and see what do we get.
This is the result we were expecting. Now we can commit what we have so far before we run the code against the real input data.
Real input data
The final step to solve the first part of the day 1 challenge is to use the real input data provided on the day 2 page. I created the file inside of the directory and put the data in it. Now it is time to read the input file. This time again we can reuse the function from day 1.
Final result
Now we can replace the test input data and run our script one last time to get the final answer to the puzzle.
Now we can copy the number we get and paste it in the submit field on the website. Clicking the button will give us a resounding congrats, as we did it right!
We are now also able to move forward to the part two of the puzzle.
The final step is to commit our final code.
Now allow me to invite you to follow along with the second part. Day 2, part 2