Modern C++ in Advent of Code: Day 4
Today is the fourth day of Advent of Code, and we are tasked to detect inefficiencies in cleaning schedules.
I recommend you try to solve the problems yourself: https://adventofcode.com.
Input
Our input is a series of pairs of intervals. To simplify the solution code, we can introduce a custom Interval type and take the input as std::vector<std::pair<Interval,Interval>>
.
Part1: completely overlapping intervals
Our goal for part one is to detect (and count) the situations where the intervals overlap entirely.
One interval is wholly contained within another if its minimum and maximum are within the other interval's minimum and maximum.
Once we have a method that checks whether one interval is contained within another, all we need is to count the number of instances when this is true:
Part2: partial overlap
For part two, we need to detect any overlap.
Interval A overlaps with interval B if the minimum or maximum of B is within the interval of A or when A is fully contained within B.
As with part one, once we have a method that detects this overlap, we can count the number of instances:
Links
The repository with a complete solution (including input parsing) is available here: https://github.com/HappyCerberus/moderncpp-aoc-2022.
I post daily Modern C++ content on Twitter, LinkedIn, Mastodon, Medium and Substack.