retyped day2, completed days 3 and 4, modified template
This commit is contained in:
29
day4/common.py
Normal file
29
day4/common.py
Normal file
@@ -0,0 +1,29 @@
|
||||
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
|
||||
Reference in New Issue
Block a user