Victor Kropp

Advent of Code Day 2

The second puzzle is there, and with its second part I got my first wrong answer. Of course, because I wanted to trick the game and get tricked myself instead.

Checking if a report is safe is actually quite easy:

val diffs = levels.zipWithNext { a, b -> b - a }
diffs.all { it.absoluteValue in 1..3 } && diffs.map { it.sign }.distinct().size == 1

We first calculate all differences and then check if they are between 1 and 3 and have the same sign, which means that the sequence is either increasing or decreasing.

In the second puzzle I wanted to replace the all condition with all but one, but it obviously didn’t work, as single outlier may spoil two differences. I ended up trying all possible combinations. Since there are not many of them, it works pretty fast, albeit it isn’t an optimal solution.

As usual, my code is on GitHub: kropp/aoc24. See you tomorrow!



This is post 22 of #100DaysToOffload

Advent of Code100DaysToOffload

Subscribe to all blog posts via RSS