Walking Through Advent of Code Day 9 Part 2

Welcome back after a long, much-needed break! Hopefully, you’ve had time to reenergize and are now ready to start 2024 with a bang!

Thankfully, the elves were semi-nice to us for part 2 and only slightly changed their requests and wanted us to find the first number in the solution instead of the last.

If you have my solution for part 1 handy, you’re way better than Jon Bon Jovi – you’re more than halfway there.

I chose to leave most of the solution completely alone – absolutely nothing changed from the while loop or above. We’re still using our #OneNastyRow table and we’re still checking if the summation of the entire row is equal to zero.

But now, instead of being able to quickly add up the far right side of the triangle, we have to loop through the data again to determine the left side of the triangle.

D   3   6   9  12  15   B
  C   3   3   3   3   A
    0   0   0   0   0  

Yes, that’s right. This time, we’re figuring out C and D instead of A and B.

After we have completed the original while loop, I want to add the following code:

set @Loops = @Loops - 1

delete from #OneNastyRow WHERE rn = @RN AND OrderNum = 0

insert into #OneNastyRow
select @loops, 0, 0

We’re now looking to reset our @Loops variable to the correct number – since we would have added 1 to @Loops to finish our while loop, we want to subtract one to set it to the max row of what is in our table.

We also want to delete the row we put in for OrderNum = 0 and RN = @RN that we originally put into the table. This is just to make it so that we won’t have 2 rows with the same RN and OrderNum at the end of the process.

And finally, we insert one row into the #OneNastyRow table where the RN = @Loops while History and OrderNum = 0. This is so that we have the final row setup with all zeroes for History and OrderNums all showing correctly. After this is complete, we can add our loop to go back to the beginning of our row and determine the first number.

while @Loops >= 0
begin
     ;WITH Normal AS (SELECT CONVERT(bigint, History) AS History FROM #OneNastyRow WHERE OrderNum = 1 AND RN = @Loops),
	   Previous AS (SELECT CONVERT(bigint, History) AS History FROM #OneNastyRow WHERE OrderNum = 1 AND RN = @Loops + 1)
     insert into #OneNastyRow
     select @loops, N.History - P.History, 0
     FROM Normal N 
     JOIN Previous P ON 1 = 1

     set @loops = @loops - 1
end

;WITH cte AS 
        (SELECT History FROM #OneNastyRow WHERE RN = 1 AND OrderNum = 0)
UPDATE ac
SET NewNumber = c.History
FROM #AOCDay9 ac
JOIN cte c ON @RowNumber = Ac.RN

With this code, we’re taking the data we’ve found previously, subtracting it from the data found in OrderNum 1 on the row we’re on currently, and pushing that into the table.

Our very last statement then pushes the data back into the #AOCDay9 table so that we can have the data we need for the elves.

With that, we sum all of the NewNumbers up and provide it to our friends the elves.

As a side note, please remember that this summed-up number will be extremely low – the opposite of what you’ve given to them before. So if you come up with a number in the millions, or even thousands, you’ve goofed up.

And so with that, we are ready for Day 10 with our elvish friends. Until tomorrow when we find out what they require this time!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.