1
1
advent-of-code/src/holt59/aoc/base.py

53 lines
1.3 KiB
Python
Raw Normal View History

from abc import abstractmethod
from logging import Logger
from typing import Any, Final, Iterable, Iterator, Protocol, Sequence, TypeVar, overload
2024-12-14 10:52:32 +00:00
from numpy.typing import NDArray
_T = TypeVar("_T")
class ProgressHandler(Protocol):
@overload
def wrap(self, values: Sequence[_T]) -> Iterator[_T]: ...
@overload
def wrap(self, values: Iterable[_T], total: int) -> Iterator[_T]: ...
2024-12-14 10:52:32 +00:00
class FileHandler:
@abstractmethod
2024-12-10 14:38:00 +00:00
def create(self, filename: str, content: bytes, text: bool = False): ...
2024-12-14 10:52:32 +00:00
def image(self, filename: str, image: NDArray[Any]):
from io import BytesIO
import matplotlib.pyplot as plt
io = BytesIO()
plt.imsave(io, image) # type: ignore
io.seek(0)
self.create(filename, io.read(), False)
2024-12-10 14:38:00 +00:00
class BaseSolver:
def __init__(
self,
logger: Logger,
verbose: bool,
year: int,
day: int,
progress: ProgressHandler,
2024-12-10 14:38:00 +00:00
files: FileHandler | None = None,
):
self.logger: Final = logger
self.verbose: Final = verbose
self.year: Final = year
self.day: Final = day
self.progress: Final = progress
2024-12-10 14:38:00 +00:00
self.files: Final = files
@abstractmethod
def solve(self, input: str) -> Iterator[Any] | None: ...