summaryrefslogtreecommitdiffstats
path: root/aoc/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'aoc/__init__.py')
-rw-r--r--aoc/__init__.py43
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 -*-
2import os
3from abc import ABC
4from typing import Generator, Any, Iterator, Dict, TypeVar, Generic
5
6T = TypeVar("T")
7I = TypeVar("I")
8
9
10class 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")