-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve.py
46 lines (37 loc) · 1.19 KB
/
solve.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
import re
import os
one = 0
two = 0
with open(os.path.dirname(__file__) + "/input.txt") as f:
field = f.read().strip().splitlines()
# numbers per line
numbers = []
for line in field:
numbers.append([m for m in re.finditer(r"\d+", line)])
for y, line in enumerate(field):
for x, c in enumerate(line):
if re.match(r"[\.\d]", c):
# skip numbers and dots
continue
parts_two = []
def parts(row):
global one
for mn in numbers[row]:
if mn.span()[0] >= x + 2 or mn.span()[1] <= max(x - 1, 0):
# check if window relevant for us
continue
no = int(mn.group(0))
if c == "*":
parts_two.append(no)
one += no
if y > 0:
# not first row
parts(y-1)
parts(y)
if y < len(field) - 1:
# not last row
parts(y+1)
if len(parts_two) == 2:
two += parts_two[0] * parts_two[1]
print("Part One", one)
print("Part Two", two)