We’re back to saving Christmas this year! And this time to the depths of the ocean to retrieve the keys to Santa’s sleigh.
The swift-algorithms package comes in handy again, making even lighter of the simpler problems than they originally were. My initial solution turned the input list into a list of sliding windows of size 2 and counting the number of times when the first value in each window was less than the last.
Part two is similar except we need to compare the rolling totals of three values at a time. I used the sliding windows algorithm again using a count of 3, then summing each sequence to find this rolling total, then used the same code as part one to find the amount of times where the prior total was less than the latter total.
What I like about this solution is that it reads very naturally like a set of rules derived from the puzzle itself.
200 A B
208 A B
The above is the example provided for part two and I later realised that the final comparison is between four values, the middle two of which exist in both sides, so can be eliminated. This allows us to use a four-measurement sliding window, and compare the first and last values.