-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03p2.py
119 lines (93 loc) · 3.12 KB
/
03p2.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
input = open("03-input.txt", "r")
lines = input.read().splitlines()
LINES_NUM = len(lines)
LINE_LENGTH = len(lines[0])
# build the 2D data array
data = []
symbols = set()
numbersWithAdjacents = []
class Coordinates:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f"{self.x}:{self.y}"
def __hash__(self):
return hash(self.__repr__())
def __eq__(self, other):
if isinstance(other, Coordinates):
return self.__repr__() == other.__repr__()
else:
return False
class NumberWithAdjacents:
def __init__(self, value, adjacentCoordinates):
self.value = value
self.adjacentCoordinates = adjacentCoordinates
def __repr__(self):
return f"{self.value}"
def isAdjacentToSymbol(self, symbol):
for coordinate in self.adjacentCoordinates:
if coordinate.x < 0 or coordinate.x > LINE_LENGTH - 1:
continue
if coordinate.y < 0 or coordinate.y > LINES_NUM -1:
continue
if data[coordinate.y][coordinate.x] == symbol:
return True
return False
def getCoordinatesForAdjactentWithSymbol(self, symbol) -> Coordinates:
for coordinate in self.adjacentCoordinates:
if coordinate.x < 0 or coordinate.x > LINE_LENGTH - 1:
continue
if coordinate.y < 0 or coordinate.y > LINES_NUM -1:
continue
if data[coordinate.y][coordinate.x] == symbol:
return coordinate
def getAdjacents(x, y):
adjacents = []
# same row
adjacents.append(Coordinates(x - 1, y + 0))
adjacents.append(Coordinates(x + 1, y + 0))
# top row
adjacents.append(Coordinates(x - 1, y + 1))
adjacents.append(Coordinates(x + 0, y + 1))
adjacents.append(Coordinates(x + 1, y + 1))
#bottom row
adjacents.append(Coordinates(x - 1, y - 1))
adjacents.append(Coordinates(x + 0, y - 1))
adjacents.append(Coordinates(x + 1, y - 1))
return adjacents
# prepare data and extract symbols
for line in lines:
lineData = []
for character in line:
lineData.append(character)
if not character.isdigit() and character != ".":
symbols.add(character)
data.append(lineData)
# extract numbers and adjacent coordinates
for y, rowValue in enumerate(data):
digits = ""
adjacents = []
for x, columnValue in enumerate(rowValue):
if columnValue.isdigit():
digits += columnValue
for adjacent in getAdjacents(x, y):
adjacents.append(adjacent)
if digits != "" and ((not columnValue.isdigit()) or x == LINE_LENGTH -1):
numbersWithAdjacents.append(
NumberWithAdjacents(int(digits), adjacents))
digits = ""
adjacents = []
symbolCoordinates = set()
for number in numbersWithAdjacents:
if number.isAdjacentToSymbol("*"):
symbolCoordinates.add(number.getCoordinatesForAdjactentWithSymbol("*"))
sum = 0
for coordinates in symbolCoordinates:
numbersWithSharedSymbol = []
for number in numbersWithAdjacents:
if coordinates in number.adjacentCoordinates:
numbersWithSharedSymbol.append(number)
if len(numbersWithSharedSymbol) == 2:
sum += numbersWithSharedSymbol[0].value * numbersWithSharedSymbol[1].value
print(sum)