summaryrefslogtreecommitdiffstats
path: root/aoc/__init__.py
blob: f490bf85b778d24f373e2c9f2c980e1501b081e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- 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__}"

    @classmethod
    def parse_item(cls, item: str) -> Iterator[I]:
        yield 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 from self.parse_item(line.strip("\n"))

    def run(self, input: Iterator[I]) -> T:
        raise NotImplementedError("Please implement run")