diff options
| author | 2023-11-19 16:55:03 +0100 | |
|---|---|---|
| committer | 2025-12-01 09:53:01 +0100 | |
| commit | d7e30321ae6ae4c82a8ab7455f6ce33afd719c67 (patch) | |
| tree | e873d640f909ae3e247adc7661b7d954c8af3a26 /aoc/__init__.py | |
| download | 2025-d7e30321ae6ae4c82a8ab7455f6ce33afd719c67.tar.gz 2025-d7e30321ae6ae4c82a8ab7455f6ce33afd719c67.tar.bz2 2025-d7e30321ae6ae4c82a8ab7455f6ce33afd719c67.zip | |
Initial commit
Diffstat (limited to 'aoc/__init__.py')
| -rw-r--r-- | aoc/__init__.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/aoc/__init__.py b/aoc/__init__.py new file mode 100644 index 0000000..7a089f6 --- /dev/null +++ b/aoc/__init__.py | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | import os | ||
| 3 | from abc import ABC | ||
| 4 | from typing import Generator, Any, Iterator, Dict, TypeVar, Generic | ||
| 5 | |||
| 6 | T = TypeVar("T") | ||
| 7 | I = TypeVar("I") | ||
| 8 | |||
| 9 | |||
| 10 | class BaseAssignment(Generic[T, I], ABC): | ||
| 11 | example_result: T = NotImplemented | ||
| 12 | example_kwargs: Dict = {} | ||
| 13 | |||
| 14 | def __init__(self, path): | ||
| 15 | self.path = path | ||
| 16 | |||
| 17 | def __str__(self): | ||
| 18 | return f"{self.__module__}.{self.__class__.__name__}" | ||
| 19 | |||
| 20 | def parse_item(self, item: str) -> I: | ||
| 21 | return item | ||
| 22 | |||
| 23 | @property | ||
| 24 | def part(self) -> int: | ||
| 25 | return 1 if self.__class__.__name__.endswith("One") else 2 | ||
| 26 | |||
| 27 | def read_input(self, example=False) -> Iterator[I]: | ||
| 28 | file = f"{self.path}/input.txt" | ||
| 29 | |||
| 30 | if example or not os.path.isfile(file): | ||
| 31 | for file in [ | ||
| 32 | f"{self.path}/example_part_{self.part}.txt", | ||
| 33 | f"{self.path}/example.txt", | ||
| 34 | ]: | ||
| 35 | if os.path.exists(file): | ||
| 36 | break | ||
| 37 | |||
| 38 | with open(file, "r") as input_file: | ||
| 39 | for line in input_file.readlines(): | ||
| 40 | yield self.parse_item(line.strip("\n")) | ||
| 41 | |||
| 42 | def run(self, input: Iterator[I]) -> T: | ||
| 43 | raise NotImplementedError("Please implement run") | ||
