summaryrefslogtreecommitdiffstats
path: root/aoc
diff options
context:
space:
mode:
Diffstat (limited to 'aoc')
-rw-r--r--aoc/__init__.py18
-rw-r--r--aoc/test_init.py4
2 files changed, 14 insertions, 8 deletions
diff --git a/aoc/__init__.py b/aoc/__init__.py
index 8368f7b..7a089f6 100644
--- a/aoc/__init__.py
+++ b/aoc/__init__.py
@@ -1,11 +1,15 @@
1# -*- coding: utf-8 -*- 1# -*- coding: utf-8 -*-
2import os 2import os
3from abc import ABC 3from abc import ABC
4from typing import Generator, Any, Iterator 4from typing import Generator, Any, Iterator, Dict, TypeVar, Generic
5 5
6T = TypeVar("T")
7I = TypeVar("I")
6 8
7class BaseAssignment(ABC): 9
8 example_result = NotImplemented 10class BaseAssignment(Generic[T, I], ABC):
11 example_result: T = NotImplemented
12 example_kwargs: Dict = {}
9 13
10 def __init__(self, path): 14 def __init__(self, path):
11 self.path = path 15 self.path = path
@@ -13,14 +17,14 @@ class BaseAssignment(ABC):
13 def __str__(self): 17 def __str__(self):
14 return f"{self.__module__}.{self.__class__.__name__}" 18 return f"{self.__module__}.{self.__class__.__name__}"
15 19
16 def parse_item(self, item: str) -> Any: 20 def parse_item(self, item: str) -> I:
17 return item 21 return item
18 22
19 @property 23 @property
20 def part(self): 24 def part(self) -> int:
21 return 1 if self.__class__.__name__.endswith("One") else 2 25 return 1 if self.__class__.__name__.endswith("One") else 2
22 26
23 def read_input(self, example=False) -> Generator: 27 def read_input(self, example=False) -> Iterator[I]:
24 file = f"{self.path}/input.txt" 28 file = f"{self.path}/input.txt"
25 29
26 if example or not os.path.isfile(file): 30 if example or not os.path.isfile(file):
@@ -35,5 +39,5 @@ class BaseAssignment(ABC):
35 for line in input_file.readlines(): 39 for line in input_file.readlines():
36 yield self.parse_item(line.strip("\n")) 40 yield self.parse_item(line.strip("\n"))
37 41
38 def run(self, input: Iterator) -> Any: 42 def run(self, input: Iterator[I]) -> T:
39 raise NotImplementedError("Please implement run") 43 raise NotImplementedError("Please implement run")
diff --git a/aoc/test_init.py b/aoc/test_init.py
index f804c41..e52daf7 100644
--- a/aoc/test_init.py
+++ b/aoc/test_init.py
@@ -1,6 +1,8 @@
1# -*- coding: utf-8 -*- 1# -*- coding: utf-8 -*-
2def test_assignment_examples(assignment): 2def test_assignment_examples(assignment):
3 assert ( 3 assert (
4 assignment.run(input=assignment.read_input(example=True)) 4 assignment.run(
5 input=assignment.read_input(example=True), **assignment.example_kwargs
6 )
5 == assignment.example_result 7 == assignment.example_result
6 ) 8 )