30 lines
857 B
Python
30 lines
857 B
Python
from __future__ import annotations
|
|
|
|
import re
|
|
from typing import NamedTuple
|
|
|
|
|
|
class Range(NamedTuple):
|
|
first: int
|
|
last: int
|
|
|
|
def __contains__(self, other: Range) -> bool:
|
|
return self.first <= other.first and self.last >= other.last
|
|
|
|
def __str__(self):
|
|
return f"{self.first}-{self.last}"
|
|
|
|
def overlaps_with(self, other: Range) -> bool:
|
|
return Range(other.first, other.first) in self or Range(other.last, other.last) in self
|
|
|
|
|
|
def parse(filename: str) -> list[tuple[Range, Range]]:
|
|
pairs = []
|
|
with open(filename) as f:
|
|
while line := f.readline():
|
|
if match := re.match(r"(\d+)-(\d+),(\d+)-(\d+)", line):
|
|
pairs.append(
|
|
(Range(int(match.group(1)), int(match.group(2))), Range(int(match.group(3)), int(match.group(4))))
|
|
)
|
|
return pairs
|