-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday3.py
75 lines (60 loc) · 1.97 KB
/
day3.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
from collections import defaultdict
from typing import List, Tuple
from pydantic.dataclasses import dataclass
from common.aoc import AoCDay
@dataclass
class Number:
value: int
row: int
start: int
length: int
@property
def border(self) -> List[Tuple[int, int]]:
return [
(self.row + y - 1, self.start + x - 1)
for x in range(self.length + 2)
for y in range(3)
]
def extract_numbers(input, row) -> List[Number]:
start = 0
length = 0
result = []
for c, i in enumerate(input):
if i.isnumeric():
length += 1
else:
if length > 0:
result.append(
Number(int(input[start : start + length]), row, start, length),
)
start = c + 1
length = 0
if length > 0:
result.append(Number(int(input[start : start + length]), row, start, length))
return result
class Day(AoCDay):
def __init__(self, test=0):
super().__init__(__name__, test)
def _preprocess_input(self):
self.__input_data = self._input_data[0]
self.__simbols = {}
self.__numbers = []
for c, x in enumerate(self.__input_data):
for d, y in enumerate(x):
if y != "." and not y.isnumeric():
self.__simbols[(c, d)] = y
self.__numbers.extend(extract_numbers(x, c))
def _calculate_1(self) -> int:
result = 0
for i in self.__numbers:
for j in i.border:
if j in self.__simbols:
result += i.value
return result
def _calculate_2(self) -> int:
star_adjacents = defaultdict(list)
for i in self.__numbers:
for j in i.border:
if j in self.__simbols and self.__simbols[j] == "*":
star_adjacents[j].append(i.value)
return sum((v[0] * v[1]) for v in star_adjacents.values() if len(v) == 2)