-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.py
105 lines (90 loc) · 4.07 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
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
import re
class Day3:
def __init__(self, data):
self.data = data
def day3_part1(self):
with open(self.data) as fp:
doc = [] # 140 x 140
digits = []
result = 0
for line in fp:
doc.append(('.' + line + '.'))
for line in doc:
pattern = r'\d+'
digit_line = []
for match in re.finditer(pattern, line):
r = match.span()
nr = match.group()
# !! match counting from 0 shows position start+1 till end+2 !!
# therefor correcting output to [start-index, end-index, found-number]
digit_line.append([r[0] - 1, r[1] - 2, int(nr)])
digits.append(digit_line)
for i in range(1, len(doc) - 1):
for j in range(len(digits[i])):
# checking for symbols before and after the digit
if self.is_special(doc[i][digits[i][j][0]]) \
or self.is_special(doc[i][digits[i][j][1] + 2]):
res = digits[i][j][2]
print(f'Digit:{res}, doc: {doc[i][digits[i][j][0] - 1]}, Zeile:{i}, Digit_ID:{j}')
result += res
# checking for symbols on lines above of digit
for s in doc[i - 1][digits[i][j][0]:digits[i][j][1] + 3]:
if self.is_special(s):
res = digits[i][j][2]
print(
f'Digit:{res}, doc: {doc[i - 1][digits[i][j][0]:digits[i][j][1] + 3]}, Zeile:{i}, Digit_ID:{j}')
result += res
# checking for symbols on lines below of digit
for s in doc[i + 1][digits[i][j][0]:digits[i][j][1] + 3]:
if self.is_special(s):
res = digits[i][j][2]
print(
f'Digit:{res}, doc: {doc[i + 1][digits[i][j][0]:digits[i][j][1] + 3]}, Zeile:{i}, Digit_ID:{j}')
result += res
'''ToDo checking first and last line corner case doesn't work
# checking corner cases separately to avoid index out of range error for line 0 and -1
for jj in range(len(digits[0])):
if self.is_special(doc[0][digits[0][jj][0]]) or self.is_special(doc[0][digits[0][jj][1] + 2]):
res = digits[0][jj][2]
print(
f'Digit:{res}, doc: {doc[0][digits[0][jj][0] - 1]}, Zeile:{i}, Digit_ID:{jj}')
result += res
# checking below
for s in doc[1][digits[0][jj][0]:digits[0][jj][1]+3]:
if self.is_special(s):
res = digits[0][jj][2]
print(
f'Digit:{res}, doc: {s}, Zeile:{0}, Digit_ID:{jj}')
result += res
for l in range(len(digits[-1])):
if self.is_special(doc[-1][digits[-1][l][0]]) or self.is_special(doc[-1][digits[-1][l][1] + 2]):
res = digits[-1][l][2]
print(
f'Digit:{res}, doc: {doc[-1][digits[-1][l][0] - 1]}, Zeile:{i}, Digit_ID:{l}')
result += res
# checking above
for s in doc[-2][digits[-1][l][0]:digits[-1][l][1]+3]:
if self.is_special(s):
res = digits[-1][l][2]
print(
f'Digit:{res}, doc: {s}, Zeile:{0}, Digit_ID:{l}')
result += res
'''
return result
def is_special(self, sym) -> bool:
zeichen = r'=+-@_!#$%^&*?/|~:'
if sym in zeichen:
return True
else:
return False
def get_results(self) -> [int, int]:
result = self.day3_part1()
# adding first & last row numbers by hand:
result += (296 + 45 + 902 + 269 + 720 + 571)
return [result, 00] # 512794 , not finished
if __name__ == '__main__':
data_test = 'data/day3_input.txt'
d3 = Day3(data_test)
r = d3.day3_part1()
# adding first & last row numbers by hand:
print(r + 296 + 45 + 902 + 269 + 720 + 571) # 512794