1
0
mirror of https://github.com/chylex/Advent-of-Code.git synced 2025-05-28 18:34:11 +02:00

Add 2022 - Day 4 - Part 2

This commit is contained in:
chylex 2022-12-12 22:31:47 +01:00
parent 4304efd023
commit ca7b829db3
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548

View File

@ -12,6 +12,9 @@ class Assignment:
def contains(self, other: "Assignment") -> bool:
return self.first_section <= other.first_section and self.last_section >= other.last_section
def overlaps(self, other: "Assignment") -> bool:
return self.first_section in range(other.first_section, other.last_section + 1) or self.last_section in range(other.first_section, other.last_section + 1)
@staticmethod
def from_str(line: str) -> "Assignment":
(first_section, last_section) = line.split("-", maxsplit = 1)
@ -26,9 +29,12 @@ class Pair:
def __repr__(self) -> str:
return f"{self.first}, {self.second}"
def has_overlap(self) -> bool:
def has_full_overlap(self) -> bool:
return self.first.contains(self.second) or self.second.contains(self.first)
def has_any_overlap(self) -> bool:
return self.first.overlaps(self.second) or self.second.overlaps(self.first)
@staticmethod
def from_line(line: str) -> "Pair":
(first, second) = line.split(",", maxsplit = 1)
@ -36,7 +42,10 @@ class Pair:
lines = read_input_lines()
pairs = map(lambda line: Pair.from_line(line), lines)
pairs = list(map(lambda line: Pair.from_line(line), lines))
overlaps = sum(1 for pair in pairs if pair.has_overlap())
print(f"Amount of assignments with overlap: {overlaps}")
assignments_with_full_overlap = sum(1 for pair in pairs if pair.has_full_overlap())
print(f"Amount of assignments with full overlap: {assignments_with_full_overlap}")
assignments_with_any_overlap = sum(1 for pair in pairs if pair.has_any_overlap())
print(f"Amount of assignments with any overlap: {assignments_with_any_overlap}")