-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.py
52 lines (41 loc) · 1.35 KB
/
day9.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
from aocd import lines, submit
data = [[int(c) for c in line] for line in lines]
def get_adj(row, col, data):
# Get all adjacent points in the cave grid
adj = {}
if row > 0:
adj[(row - 1, col)] = data[row - 1][col]
if row < len(data[0]) - 1:
adj[(row + 1, col)] = data[row + 1][col]
if col > 0:
adj[(row, col - 1)] = data[row][col - 1]
if col < len(data) - 1:
adj[(row, col + 1)] = data[row][col + 1]
return adj
def fill_basin(visited, cur_point, data):
# Recursive function to find all the points in each basin.
# print(f'Current point: {cur_point}')
# print(f'Visited list: {visited}')
adj = [point for point, val in get_adj(*cur_point, data).items() if point not in visited and val != 9]
visited.extend(adj)
for point in adj:
visited = fill_basin(visited, point, data)
return visited
low_count = 0
low_points = []
for row in range(len(data)):
for col in range(len(data[0])):
if all([data[row][col] < adj for adj in list(get_adj(row, col, data).values())]):
low_count += data[row][col] + 1
low_points += [(row, col)]
print(low_count)
submit(low_count, part='a')
basin = []
for low in low_points:
basin += [len(fill_basin([low], low, data))]
basin.sort()
prod = 1
for val in basin[-3:]:
prod *= val
print(prod)
submit(prod, part='b')