-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaoc09.py
More file actions
executable file
·50 lines (36 loc) · 1.21 KB
/
aoc09.py
File metadata and controls
executable file
·50 lines (36 loc) · 1.21 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
#!/usr/bin/env pypy3
from functools import reduce
def siblings(cave_map, x, y):
if x > 0:
yield (x - 1, y)
if x < len(cave_map[y]) - 1:
yield (x + 1, y)
if y > 0:
yield (x, y - 1)
if y < len(cave_map) - 1:
yield (x, y + 1)
def find_basin(cave_map, seen_points, basin_points, x, y):
if (x, y) in seen_points:
return basin_points
seen_points.add((x, y))
if cave_map[y][x] == 9:
return basin_points
basin_points.add((x, y))
for x, y in list(basin_points):
for x1, y1 in siblings(cave_map, x, y):
find_basin(cave_map, seen_points, basin_points, x1, y1)
return basin_points
def main():
total_risk = 0
cave_map = [[int(i) for i in list(line.strip())] for line in open("/Users/zee/Downloads/input-2.txt").readlines()]
seen_points = set()
basins = []
for y in range(len(cave_map)):
for x in range(len(cave_map[0])):
basin = find_basin(cave_map, seen_points, set(), x, y)
if basin:
basins.append(basin)
result = reduce(lambda a, b: a * len(b), sorted(basins, key=len, reverse=True)[:3], 1)
print(f"result: {result}")
if __name__ == "__main__":
main()