Advent of Code 2024 - Day 2
Red-Nosed Reports
Written December 21st, 2024 | Attempted December 4th, 2024
With Day 2, we can see that the problems are starting to get a little more challenging. Not significantly so but I can tell that it's going to get harder.
# Part 1
The sample input we're given is as follows:
Each row is a “report” containing some X number of integers. For the first part, we have to confirm whether or not each report adheres to the following set of conditions:
- From left to right, the integers are either strictly increasing or strictly decreasing.
- The magnitude of the change between two adjacent numbers is at most three (strictly changing implies a minimum of one, since zeros would mean the set is now monotonic).
It is based on these requirements that the problem boils down to the following set of steps:
- Read in the file (arguably the biggest part).
- Parse each report and check if it meets the conditions.
- Count the number of reports that meet the conditions.
Based on these steps, I implemented the following C++ solution (this time opting to use ranges the first time around):
# Part 2
The second part of the problem now asks us to include reports that could be turned “safe” by removing a single integer from the report. Since these input sizes are not monstrously big, we can brute force this by permuting the reports and checking if they meet the conditions. It's admittedly not the most efficient solution but it works.
Again, not terribly hard problems but they're definitely starting to ramp up a bit—whereas the first day was just a simple “read an array in and sort them” kind of problem, this one requires a little more thought into the algorithm used, but still not too bad.