Walking Through Advent of Code Day 6 Part 2

Yet again those little elves have been devilish and messed up our input format. This time, they put spaces in our data.

You put Chocolate in my Peanut Butter!

Sadly, this does make our numbers rather large, so we’re back to using the big ole Tally table we created for Walking Through Advent of Code Day 5.

This time I made it a little simpler on myself and just removed all of the spaces myself and placed the data in variables (one for time and one for distance). I thought this was an excellent idea since only one number would come out of all of this work.

DECLARE @TimeAllowed bigint = <Insert Time Here>,
        @CurrentDistance bigint = <Insert Distance Here>;

SELECT COUNT(1)
FROM Tally
WHERE n < @TimeAllowed
AND (@TimeAllowed - n) * n > @Distance; 

Definitely nothing spectacular here code-wise. We leave all of the goodness to SQL Server since it’s the real star here.

With that, I hope you’ve found a new appreciation for tally tables – big and small – and will continue to use them in the future (maybe even tomorrow’s exercise!) Until then my friends!

Leave a comment

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