Advent of Code 2024 (Day 2)
Published on: Monday, December 9, 2024 at 7:30 PM PST
Written by Josh Bright
Intro
The puzzle for day 2 was a pretty easy one, where we need to test if a list of numbers is valid based upon some rules provided.
Rules:
- The numbers are either all increasing or decreasing
- The difference between each number is at least one and at most 3
Part 1
How I normally handle these is to loop over each line, and hand the line to a function that returns true or false, depending on if the line is valid or not. Comparing the list of numbers to a sorted list (ascending and descending) can check if the rule 1 applies, and then looping over each number and checking the next one can check for rule 2.
Part 2
For part two, we check the same lists of numbers, but, now we can “ignore” one failing number and the list can still be valid. After thinking a bit on how to best deal with this curve ball, I sort of took the brute force route here. When a list is not valid, I then generate a new set of lists for that line, but each item in the list is the list with one of the numbers removed. I then run the same check but for each of these sub lists. Doing that still let the puzzle finish in under a second, although I think I could have solved it in a bit more clever way. I’ll save that for later though!