Skip to content
advent of code 2022

day 04

camp cleanup

approach

each line becomes two Interval dataclasses with a start and an end. part 1 counts the pairs where either interval contains the other, and part 2 counts the pairs that overlap at all.

overlaps only checks whether the other interval starts inside this one, so it's called both ways round; between the two calls every overlap is caught. the rust version is a port.

the trick

two one-line interval tests, each tried in both directions.

python
    # other is contained in self
    def contains(self, other):
        return self.start <= other.start and other.end <= self.end

    def overlaps(self, other):
        return self.start <= other.start <= self.end