Day 5, part 1
Day 5 challenge of the mighty Advent of Code 2024
Created at:
Last updated:
Table of Contents
Introduction
Hello there! I would like to kindly invite you to the day 5 of Advent of Code. As always please read and understand the puzzle before following the rest of the article. The whole description of today's puzzle can be found here. Today we are asked to do some data transformations based on mapping data. We are given the input text file which includes the list of seeds numbers which are our initial inputs and the rest of the file is describing number of transformations to be done, where each transformation takes as the input, the output of the previous one. Finally, from the list of the transformations outputs, we need to find the lowest number, which is the answer to the puzzle.
Creating files for the challenge
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.
Solution
To solve the puzzle we need to do the following steps:
- Parse the input data to get and .
- Perform mapping on the inputs to get the outputs.
- Chain these transformations starting with the as an initial input for the first transformation and then passing the output as an input to the next transformation.
- In the transformations results, find the lowest .
Parsing the data
Before we can do any magic to get the expected results, we first have to transform the input text data into something more useful. We need to extract the seeds data and data for each map. Here is the piece of code that handles parsing the .
Running this will give us the following:
As you can see we have nice array of parsed digits. Now it's time for parsing the maps data. For each map the interesting data is of the map, as the arrays with numbers and what I think might be useful, the first part of the next maps name. As we see in the test data, the name of each map is built of three words connected with dashes. The last piece of every map name is the starting piece of the next map.
The above pieces of code added to that we had, gives us these results:
As you can see the output in terminal is truncated so I added to get the insights how the data really looks like.
As you can see we have all the data we might need. We can now start to solve the puzzle.
Calculating map output
All the operations we need to do, are basically transformations of the input, into the output data based on the numbers in the map. Each transformation is divided to couple of operations:
- We first change the representation of the map data from array of numbers to objects with descriptive properties (this is totally optional, I did this only to make things easier to follow).
- We check if the input is in any of the map ranges:
- if it is not, the output of the mapping is just the input itself
- if the input is in one of the ranges, we take this range we do the calculations
- We calculate the output by measuring distance from the source range start and we add this distance to the destination range start. From the puzzle description we can take the example outputs for the map:
And this is the code which does that:
I have declared the with the example numbers and run the first map transformation on each of them.
Seems to work as expected. Good job! It is a great time to save our progress.
Chaining the maps
We now have a way to perform transformations for each single map, but now we have to figure out how to order these transformations and run them one by one feeding the output of the previous transformation, to the next one. We can either store these maps in an object which will have methods like and properties like which allow you to iterate through the elements. We can also just sort the array of maps based on the property.
And the results are as follow:
Now the second part is to go through these maps and perform their transformations to get the final results for each seed. Before I show you the code and its output, here are the expected results for the seeds we are getting from the test data (this comes straight from the website):
And the code which does that is:
Look's that we get the right results, so we can now commit the changes.
Finding the lowest location
The final step is to find the lowest and return it as a final result. The is a last element in each transformations outputs.
We can see in the puzzle description that for our test data the lowest is . We can run our code and find out if we did everything correctly.
Success! Let's save the changes and try our code on the real input 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:
After submitting the result, we can see the message that we were RIGHT! Great work everyone! We can now save the final result.
Now its time to beat the part two., so see you all in Day 5, part 2!