Day 9, part 1
Day 9 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 9 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 given some sensor reading and our job is to make predictions of what the next value might be.
Understanding the problem
Let's consider the test data:
And more precisely, we take the first row which is our first reading. To make a prediction of what the next value might be we need to calculate differences between each pair of number until all the differences are s, like so:
As soon as we get here we have to start now from the bottom to make predictions for values in the lines above, and repeat the process until we get the prediction for the first line.
Now if we know that the difference between and is 0 we know that = . Now we can use this value to calculate the same way: - = , thus equals .
Following the same algorithm we get for the second reading and for the third one.
The solution to this puzzle is to sum all this numbers together, so in case of our test data the result is .
Now we can talk about the solution, but first let's create necessary files.
Solution
The solution can be handled in steps:
- Parse input data - of course, we can work on text straight but why would we?
- Calculate differences between numbers as long as you end up with zeros only
- Starting from the bottom, calculate next number until we get the final result
Parse input data
Let's put the test data into a variable.
Parsing the data in this case is as easy as separating lines and then collecting numbers in each line into an array.
With the piece of code above we get following results:
Looks good to me! We can now commit the changes before moving forward.
Find the differences
We will do it in steps:
- Store the results in the , initializing with the array passed as an input.
- Look at the last element in the array, if it isn't all zeros, then calculate the new differences and push them to the .
- When we get to the point when the array we are currently checking is all zeros, we return the .
If we run this piece of code we can see in the console the following results:
If we compare this to the test data analysis on the puzzle's page and to what we have calculated earlier, we can see that this is what we need. We can save that.
Predict next number
Now when we have all the diffs, we can use them to find our predicted value. To do so we start from the last array, and we use its last value to calculate the prediction for the previous diffs. We repeat the process until we get back to the first array (with the initial numbers). All of this can be done with the following:
If we run this piece of code, it spits in the console:
And these are the values we previously calculated by hand. We can now commit the changes before feeding our code with the real input data.
Final result
Now we replace the test data with input coming from the file containing the puzzles' data. We also have to add a bit of logic to sum all the predictions up. This time I did it inline.
Finally, we can run the code to get the final answer.
If we submitted the result to the form on the website, this appeared before our eyes:
This means we are home once again, and we can celebrate this beautiful moment, but before, let's commit the final code we made.
That's all folks! See you in the Day 9, part 2!