Day 7, part 1
Day 7 challenge of the mighty Advent of Code 2024
Created at:
Last updated:
Table of Contents
Introduction
Let me welcome you to this article where we are going to crack the advent of Code day 7 puzzle. The whole description can be read here. Please do so before moving forward, to have a good context of what we are going to do. Today we are asked to play some cards. We are presented with a list of hands and each hand has a bid. We need to rank these hands by their strength and then to get the final answer we need to multiply each hand's rank by the bid and add all these numbers together.
Understanding the problem
Let's take a look at the test data:
Here we see the left column with hands containing card symbols and the right column with the bids. A hand consists of five cards labeled one of , , , , , , , , , , , , or . The relative strength of each card follows this order, where is the highest and is the lowest. Then we determine which type is each of the card hands and we compare them with others to rank them. Based on their ranks, we calculate the final answer by summing up * of all cards together.
Creating files
We are going to start by creating the new directory in or project's root directory.
We also create new file inside the folder, which will be our main entry for the code for this puzzle.
We can also put the test input data inside the .
Solution
This is what comes to my mind when I think about the steps we need to perform to get the result:
- Parse the input text to create individual hand objects with properties:
- - array with each card letter/number,
- - the amount of bid.
- Determine what type is the hand (one pair, full house, etc.).
- Rank hands based on hand types and individual cards strengths.
- Multiply each hands and and sum them together to get the final answer.
Parse card hands data
To transform the input data into useful objects we simply split the whole input into lines. For each line we take the left column and we slit it into an array of individual characters and the right column is the bid.
We can run the code to see what do we have.
This seems to work fine and provide the data in easy to use way. Lets save the changes.
Find cards hand type
To handle this task we created a lookup object which for each hand type provide some properties:
- - how strong is the type, based on the strength we can decide which hand of cards is stronger and rank them later on.
- - describes with a string how the cards in the hand should look like to be of particular type. means the type is two pairs and the for this type is which means we have cards of one type, cards of other type and card of yet another type. If we analyse the test data we know that:
- is ,
- and are both ,
- and are both .
As you can see we first take the card hand and count how many card of each kind we have. Then we build this kind of string by taking only the amount of cards, sort them from the highest to lowest and convert into string. Then we compare it with out lookup and this way we can determine which type our hand has. If we run this now, we can see these results:
This matches our analysis of the test data, so we can save our progress and move forward.
Rank card hands
Now we need to rank these hands. To do so we will follow two rules.
- Comparing the types.
- In case of the same type we compare individual card strengths starting from the left one. Applying these two rules to our test data we get the following ranks: - 1, - 2, - 3, - 4 and - 5.
We use method and we compare elements in this array by type strength and/or by individual cards strength. Then we map the array back to objects. This is the output of our function:
This is exactly what we expected so we can commit the changes and go to the final part.
Sum up bids
Now the last thing left is to calculate the final bid by multiplying s by and add them together. For our test data we have: . The below code does that for us.
And when we run it we get the answer we were looking for.
Lets once again save what we did before moving to the final test with real data.
Final result
This is the time to test our code in battle. Once again we will use the function, to get the real input data, which we copy from the Advent of Code website and we put the data inside file.
Now we run the code and we can see in the terminal:
If we submit the answer we can see:
Great! It looks like we are right once again!
We can save the final result.
And I hope to see you in Day 7, part 2!