retyped day2, completed days 3 and 4, modified template

This commit is contained in:
2024-12-01 16:19:37 +01:00
parent 42bab0cb9e
commit 9e21d8d9c8
12 changed files with 134 additions and 26 deletions

29
day4/common.py Normal file
View 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