
Day 9, part 2
Day 9 challenge of the mighty Advent of Code 2024
Introduction
Welcome to the par two of day 8 puzzle. First part is described in Day 9, part 1. Please get familiar with the part 1 before jumping into this article. This part of the challenge is available only when you successfully finished the part 1. The second part of the challenge is described under -- Part Two -- section here.
New problem
So, as you remember, in the first part we were predicting the next value for a given sequence of numbers. Now we are asked to calculate the preceding number instead and give the sum of those number as a final result. Let's do the analysis on the test data once more.
1B 0 3 6 9 12 15
2 A 3 3 3 3 3
3 0 0 0 0 0From here we know that A is 3 and B is -3. For the remaining two lines of test data the predictions are 0 and 5 respectively. This gives us a sum of 2.
Solution
Basically we have to do exactly what we did before, not for the last value in the array this time, but for the first one.
Refactor predictNextSteps
Before we start looking for the prediction of the previous values let's clean up a little. That's how the updated predictNextSteps function looks now:
1function predictNextSteps(diffs) {
2 const result = [...diffs];
3 for (let i = result.length - 1; i > 0; i--) {
4 if (i === result.length - 1) {
5 result[i].push(0);
6 }
7 const thisLastIndex = result[i].length - 1;
8 const prevLastIndex = result[i - 1].length - 1;
9 result[i - 1].push(result[i][thisLastIndex] + result[i - 1][prevLastIndex]);
10 }
11 return result;
12}
13function getNextValueFromPredictions(predictions) {
14 return predictions[0][predictions[0].length - 1];
15}
16const predictedNumbers = diffs.map(predictNextSteps).map(getNextValueFromPredictions);
17console.log('Predicted numbers: ', predictedNumbers);If we run this we still get the same results for our test data as before, so we can safely commit the changes.
1git add .
2git commit -m "refactor predictNextSteps"Predict previous steps
As said before, to get the previous prediction instead on the next one we just have to look at the beginning of each diffs array and predict the previous number. Here how the function can look like:
1function predictPrevSteps(diffs) {
2 const result = [...diffs];
3 for (let i = result.length - 1; i > 0; i--) {
4 if (i === result.length - 1) {
5 result[i].unshift(0);
6 }
7 result[i - 1].unshift(result[i - 1][0] - result[i][0]);
8 }
9 return result;
10}
11function getPrevValueFromPredictions(predictions) {
12 return predictions[0][0];
13}
14const predictedNumbers2 = diffs.map(predictPrevSteps).map(getPrevValueFromPredictions);
15console.log('Predicted numbers: ', predictedNumbers2);Running this gives us:
1Predicted numbers: [-3, 0, 5]If you now scroll up to the calculation we did manually, you can see that these are the expected results. Let's commit the changes.
1git add .
2git commit -m "predict prev values"Sum them together
Lets sum the numbers from previous step together.
1console.log(
2 'Final sum: ',
3 predictedNumbers2.reduce((acc, curr) => (acc += curr), 0)
4);After running this code we get:
1Final sum: 2This is the result we were expecting from our analysis, so we can save the changes.
1git add .
2git commit -m "sum predicted values"Final result
Now it is time to replace the test data with the puzzles' real input stored in the text file. When we now run our code, the final result we get is:
1Final sum: 990Uploading this number to the form on the website shows us that we did everything the proper way. Congrats y'all!
We can now commit the final result, and call it a day.
1git add .
2git commit -m "day 9 part 2 final result"