Day 3, part 2
Day 3 challenge of the mighty Advent of Code 2024
Created at:
Last updated:
Table of Contents
Introduction
This article is a continuation of Day 3, part 1. We will be using what we have done in part 1, so before you jump into this article, please get familiar with the previous 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
Now we are asked to search all that are adjacent to exactly two numbers and then multiply those numbers for each and to get gear ratio add them together to get the final result. Lets take a look at the test data again.
In this grid there are two that are adjacent to exactly two numbers. Those numbers are and for the first and the gear ratio is . The second pair of numbers are and and the gear ratio is , so the result we are looking for here is .
Solution
As you remember from the part 1, when we were looking for numbers, I was considering two options:
- looking for symbols and then for numbers adjacent to it
- looking for numbers and then for adjacent symbols For the reasons described there, I decided that its easier to follow the second solution. This time I think I can do it like so:
- For the numbers we have found, lets check their neighborhoods and gather all the asterisks with the reference to the number they were found next to.
- Group the found asterisks based on their position, so we have the count of how many times they have appeared and which numbers were next to it.
- Filter only these asterisks which were counted twice.
- Calculate the gear ratio for each pair of numbers.
- Sum the numbers to get final result.
Finding the asterisks adjacent to numbers
We can take a look at how did we find the numbers adjacent to symbols before using the and especially the functions. Now instead of filtering, we have to gather each asterisk data in an array.
After running this script, we can see this output in the terminal:
As we can see we found 5 asterisks where two of those are duplicated. This means we have total of 3 asterisks, two of which are adjacent to exactly two numbers. We also have a reference to the numbers, so it will be easier to use these numbers later. Time to save our progress.
Grouping asterisks
Now we can tweak the data a little and create groups for each individual asterisk with the array of its numbers.
In this simple function, we use method to build an object with key as a combination of and position in the grid, which is unique to each asterisk. Then we gather numbers for each asterisk in an array. If the object does not exist we create a new one, and if it does we append the new number to the array. The result is:
We can commit the changes.
Get gear ratio number pairs
Now we can simply filter the asterisks with two numbers in the array and return only the array of those pairs. The is not needed anymore.
Looks good to me! Great! We can commit the changes.
Calculating the gear ratio
To get the gear ratio for each pair of numbers, we can simply through each pair and return the product of multiplication for each.
This is exactly what we were looking for. Lets save the changes once more.
Summing up the gear ratios
There is not much to say here. We did this already many times so lets do it once more.
And we get the correct result as expected. Lets commit the changes.
Final result
Lets run what we have using the real input data.
The following result is showed in the terminal.
We can now paste it in the answer input field and click . Drum roll please!
And...
Aha! We did it!
The final step is to commit our final code.
We can be proud of ourselves as we successfully completed the day 3 puzzle. Allow me to invite you to Day 4, part 1 and see you there!