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

64 lines
1.5 KiB
Python
Raw Normal View History

from abc import abstractmethod
from logging import Logger
from pathlib import Path
2024-12-15 10:30:23 +00:00
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-15 10:30:23 +00:00
def create(
self, filename: str, content: bytes, text: bool = False
) -> Path | None: ...
2024-12-10 14:38:00 +00:00
2024-12-14 10:52:32 +00:00
def image(self, filename: str, image: NDArray[Any]):
import imageio.v3 as iio
2024-12-15 10:30:23 +00:00
from pygifsicle import optimize # type: ignore
2024-12-14 10:52:32 +00:00
2024-12-15 10:21:01 +00:00
data = iio.imwrite("<bytes>", image, extension=Path(filename).suffix) # type: ignore
2024-12-15 10:30:23 +00:00
path = self.create(filename, data, False)
assert path is not None
optimize(path, options=["--no-warnings"])
2024-12-14 10:52:32 +00:00
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: ...