-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasins.py
More file actions
58 lines (55 loc) · 1.55 KB
/
Basins.py
File metadata and controls
58 lines (55 loc) · 1.55 KB
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
import math
text = open("input.txt").read().strip()
# text = """
# 2199943210
# 3987894921
# 9856789892
# 8767896789
# 9899965678
# """
lines = text.strip().split('\n')
elevation = [[int(char) for char in line.strip()] for line in lines]
print(elevation)
width = len(elevation[0])
height = len(elevation)
def adjs(x, y):
adj = []
if x < width - 1:
adj.append([y, x+1])
if x > 0:
adj.append([y, x-1])
if y < height - 1:
adj.append([y+1, x])
if y > 0:
adj.append([y-1, x])
return adj
def low(x, y):
adj = adjs(x, y)
if elevation[y][x] < min([elevation[ad[0]][ad[1]] for ad in adj]):
return True
return False
lows = []
for x in range(width):
for y in range(height):
if low(x, y):
lows.append([x, y])
# Enter the seventh circle of index hell
def basin(x, y):
scanned = set([y * 10000 + x])
frontier = set([y * 10000 + x])
nex = {}
while len(frontier) > 0:
nex = set([])
for point in frontier:
for adj in adjs(point % 10000, math.floor(point / 10000)):
i = (adj[0] * 10000 + adj[1])
if not i in scanned:
if elevation[adj[0]][adj[1]] != 9:
nex.add(adj[0] * 10000 + adj[1])
scanned.add(adj[0] * 10000 + adj[1])
frontier = nex
return len(scanned)
print(lows)
basins = [basin(low[0], low[1]) for low in lows]
sort = sorted(basins, reverse=True)
print(sort[0] * sort[1] * sort[2])