summaryrefslogtreecommitdiffstats
path: root/aoc
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-11-28 13:52:27 +0100
committerGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-11-28 13:52:27 +0100
commita3de698aa6b7e15e9d0974d32dc566676383bd28 (patch)
treed8d372b8adf7c7d2ab9b8d8b7d434945a88e7e9b /aoc
download2022-a3de698aa6b7e15e9d0974d32dc566676383bd28.tar.gz
2022-a3de698aa6b7e15e9d0974d32dc566676383bd28.tar.bz2
2022-a3de698aa6b7e15e9d0974d32dc566676383bd28.zip
Initial code
Diffstat (limited to 'aoc')
-rw-r--r--aoc/__init__.py27
-rw-r--r--aoc/__main__.py47
-rw-r--r--aoc/test_init.py2
-rw-r--r--aoc/utils.py5
4 files changed, 81 insertions, 0 deletions
diff --git a/aoc/__init__.py b/aoc/__init__.py
new file mode 100644
index 0000000..b13489c
--- /dev/null
+++ b/aoc/__init__.py
@@ -0,0 +1,27 @@
1import os
2from abc import ABC
3from typing import Generator, Any, Iterator
4
5
6class BaseAssignment(ABC):
7 example_result = NotImplemented
8 def __init__(self, path):
9 self.path = path
10
11 def __str__(self):
12 return f'{self.__module__}.{self.__class__.__name__}'
13
14 def parse_item(self, item: str) -> Any:
15 return item
16
17 def read_input(self, example = False) -> Generator:
18 file = f'{self.path}/input.txt'
19 if example or not os.path.isfile(file):
20 file = f'{self.path}/example.txt'
21
22 with open(file, 'r') as input_file:
23 for line in input_file.readlines():
24 yield self.parse_item(line.strip())
25
26 def run(self, input: Iterator) -> Any:
27 raise NotImplementedError('Please implement run') \ No newline at end of file
diff --git a/aoc/__main__.py b/aoc/__main__.py
new file mode 100644
index 0000000..25a7026
--- /dev/null
+++ b/aoc/__main__.py
@@ -0,0 +1,47 @@
1import argparse
2import importlib
3import os
4from time import perf_counter
5from typing import List
6
7
8def day(assignment_part: str) -> str:
9 return {
10 '1': 'One',
11 '2': 'Two',
12 }[assignment_part]
13
14def kwargs(kwarg: str) -> List:
15 return kwarg.split('=')
16
17parser = argparse.ArgumentParser(description='Advent of Code')
18
19parser.add_argument('day', type=str, help='Assignment day')
20parser.add_argument(
21 '-p', '--part', type=day, nargs='?', default='1',
22 help='Assingment part. Defaults to one.'
23)
24parser.add_argument('--example', default=False, action='store_true')
25parser.add_argument('kwargs', type=kwargs, nargs='*')
26
27
28if __name__ == '__main__':
29 args = parser.parse_args()
30 assignment_day = importlib.import_module(args.day)
31
32 Assignment = getattr(assignment_day, f'Assignment{args.part}')
33 assignment = Assignment(
34 path=os.path.dirname(assignment_day.__file__),
35 **dict(args.kwargs)
36 )
37
38 start = perf_counter()
39 print(
40 assignment.run(
41 input=assignment.read_input(example=args.example),
42 )
43 )
44 end = perf_counter()
45 delta = end - start
46 print()
47 print(f'{(delta if delta > 1 else delta * 1000):.3f}{"s" if delta > 1 else "ms"}')
diff --git a/aoc/test_init.py b/aoc/test_init.py
new file mode 100644
index 0000000..8524086
--- /dev/null
+++ b/aoc/test_init.py
@@ -0,0 +1,2 @@
1def test_assingment_examples(assignment):
2 assert assignment.run(input=assignment.read_input(example=True)) == assignment.example_result
diff --git a/aoc/utils.py b/aoc/utils.py
new file mode 100644
index 0000000..6d794f4
--- /dev/null
+++ b/aoc/utils.py
@@ -0,0 +1,5 @@
1from typing import Callable
2
3
4def bold(item: str, condition: Callable[[], bool]):
5 return f'\033[36m{item}\033[0m' if condition() else item