All checks were successful
continuous-integration/drone/push Build is passing
70 lines
1.9 KiB
Python
70 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
from pathlib import Path
|
|
|
|
|
|
def part_1(input):
|
|
result = 0
|
|
m = [[int(d) for d in l.rstrip()] for l in input]
|
|
for y, l in enumerate(m):
|
|
for x, d in enumerate(l):
|
|
lowest = True
|
|
if 0 < y and d >= m[y-1][x]:
|
|
lowest = False
|
|
elif (len(m) - 1) > y and d >= m[y+1][x]:
|
|
lowest = False
|
|
elif 0 < x and d >= m[y][x-1]:
|
|
lowest = False
|
|
elif (len(l) - 1) > x and d >= m[y][x+1]:
|
|
lowest = False
|
|
if lowest:
|
|
result += d + 1
|
|
|
|
print("Part 1 result:", result)
|
|
|
|
|
|
def part_2(input):
|
|
result = 0
|
|
m = [[int(d) for d in l.rstrip()] for l in input]
|
|
p_lowest = set()
|
|
p_low = set()
|
|
for y, l in enumerate(m):
|
|
for x, d in enumerate(l):
|
|
low = True
|
|
if d != 9:
|
|
p_low.add((x, y))
|
|
if 0 < y and d >= m[y-1][x]:
|
|
low = False
|
|
elif (len(m) - 1) > y and d >= m[y+1][x]:
|
|
low = False
|
|
elif 0 < x and d >= m[y][x-1]:
|
|
low = False
|
|
elif (len(l) - 1) > x and d >= m[y][x+1]:
|
|
low = False
|
|
if low:
|
|
p_lowest.add((x, y))
|
|
b_size = []
|
|
neighbors = [[-1, 0], [0, -1], [0, 1], [1, 0]]
|
|
for k in p_lowest:
|
|
to_check = [k]
|
|
basin = set()
|
|
while len(to_check):
|
|
p = to_check.pop(0)
|
|
basin.add(p)
|
|
for n in neighbors:
|
|
tup = (p[0] + n[0], p[1] + n[1])
|
|
if tup in p_low and tup not in basin and tup not in to_check:
|
|
to_check.append(tup)
|
|
b_size.append(len(basin))
|
|
|
|
b_size.sort()
|
|
result = b_size[-3] * b_size[-2] * b_size[-1]
|
|
print("Part 2 result:", result)
|
|
|
|
|
|
input = list()
|
|
p = Path(__file__).with_name('input.txt')
|
|
with open(p) as f:
|
|
input = f.readlines()
|
|
part_1(input)
|
|
part_2(input)
|