From 4147da1317c19fa61d6aa265e8370e63231f9207 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Sun, 19 Nov 2023 16:55:03 +0100 Subject: Initial commit --- aoc/__init__.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 aoc/__init__.py (limited to 'aoc/__init__.py') 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 @@ +# -*- coding: utf-8 -*- +import os +from abc import ABC +from typing import Generator, Any, Iterator, Dict, TypeVar, Generic + +T = TypeVar("T") +I = TypeVar("I") + + +class BaseAssignment(Generic[T, I], ABC): + example_result: T = NotImplemented + example_kwargs: Dict = {} + + def __init__(self, path): + self.path = path + + def __str__(self): + return f"{self.__module__}.{self.__class__.__name__}" + + def parse_item(self, item: str) -> I: + return item + + @property + def part(self) -> int: + return 1 if self.__class__.__name__.endswith("One") else 2 + + def read_input(self, example=False) -> Iterator[I]: + file = f"{self.path}/input.txt" + + if example or not os.path.isfile(file): + for file in [ + f"{self.path}/example_part_{self.part}.txt", + f"{self.path}/example.txt", + ]: + if os.path.exists(file): + break + + with open(file, "r") as input_file: + for line in input_file.readlines(): + yield self.parse_item(line.strip("\n")) + + def run(self, input: Iterator[I]) -> T: + raise NotImplementedError("Please implement run") -- cgit v1.2.3