2024-12-08 13:06:41 +00:00
|
|
|
from abc import abstractmethod
|
|
|
|
from logging import Logger
|
2024-12-15 09:13:42 +00:00
|
|
|
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-08 13:06:41 +00:00
|
|
|
|
2024-12-14 10:52:32 +00:00
|
|
|
from numpy.typing import NDArray
|
|
|
|
|
2024-12-08 13:06:41 +00:00
|
|
|
_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]):
|
2024-12-15 09:13:42 +00:00
|
|
|
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
|
|
|
|
2024-12-08 13:06:41 +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,
|
2024-12-08 13:06:41 +00:00
|
|
|
):
|
|
|
|
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
|
2024-12-08 13:06:41 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def solve(self, input: str) -> Iterator[Any] | None: ...
|