-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
82 lines (63 loc) · 2.25 KB
/
common.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import time
from abc import ABC, abstractmethod
from dataclasses import dataclass
from importlib import import_module
from pathlib import Path
from typing import Any
class Reader:
def __init__(self, day: int, is_test: bool, input_path: str = None):
self.day = day
self.test = is_test
self.base_dir = Path(__file__).parent
self.filename = f'{day:02}{"_test" if is_test else ""}.txt'
self.path = (self.base_dir / 'input' / self.filename) if (input_path is None) else Path(input_path)
def get_lines(self):
return self.get_data().splitlines()
def get_data(self):
with open(self.path) as f:
return f.read()
def path_exists(self) -> bool:
return self.path.exists()
@dataclass
class Answer:
"""
Dataclass for answers. Contains answers both of the first and of the second parts
"""
day: int
one: Any
two: Any
def __repr__(self):
return f'Answers for day {self.day}: part one = {self.one}, part two = {self.two}'
class BaseSolver(ABC):
"""
Abstract class for solutions
"""
def __init__(self, day: int):
self.day = day
self.data = {}
def solve(self, input_path: str = None, test: bool = False, part: int = None) -> Answer:
reader = Reader(day=self.day, is_test=test, input_path=input_path)
self.data['is_test'] = test
self.data['input'] = reader.get_lines()
self.data['raw'] = reader.get_data()
self._prepare()
one, two = self._solve(part)
return Answer(self.day, one, two)
def _prepare(self):
pass
def _solve(self, part: int):
start_time = time.time()
ans_one = self._solve_one() if part == 1 or part is None else None
first_time = time.time()
print(f' --- Elapsed time for part one = {first_time - start_time:.2f}s ---')
ans_two = self._solve_two() if part == 2 or part is None else None
print(f' --- Elapsed time for part two = {time.time() - first_time:.2f}s ---')
return ans_one, ans_two
@abstractmethod
def _solve_one(self):
...
@abstractmethod
def _solve_two(self):
...
def get_module(_day: int):
return getattr(import_module(f"solutions.day{_day:02}"), 'Solver')