|
| 1 | +# https://adventofcode.com/2021/day/9 |
| 2 | + |
| 3 | + |
| 4 | +from collections import namedtuple |
| 5 | +from math import prod |
| 6 | + |
| 7 | + |
| 8 | +SAMPLE_PATH = "../../input/2021-09-sample.txt" |
| 9 | +INPUT_PATH = "../../input/2021-09-input.txt" |
| 10 | +Point = namedtuple("Point", ("x", "y")) |
| 11 | + |
| 12 | + |
| 13 | +def get_data(filename): |
| 14 | + with open(filename) as file: |
| 15 | + data = file.read().splitlines() |
| 16 | + heightmap = tuple(tuple(int(x) for x in list(line)) for line in data) |
| 17 | + low_points = get_low_points(heightmap) |
| 18 | + return heightmap, low_points |
| 19 | + |
| 20 | + |
| 21 | +def get_neighbors(x, y, max_x, max_y): |
| 22 | + neighbors = [] |
| 23 | + if x > 0: |
| 24 | + neighbors.append(Point(x - 1, y)) |
| 25 | + if x < max_x: |
| 26 | + neighbors.append(Point(x + 1, y)) |
| 27 | + if y > 0: |
| 28 | + neighbors.append(Point(x, y - 1)) |
| 29 | + if y < max_y: |
| 30 | + neighbors.append(Point(x, y + 1)) |
| 31 | + return neighbors |
| 32 | + |
| 33 | + |
| 34 | +def get_low_points(heightmap): |
| 35 | + low_points = [] |
| 36 | + max_x = len(heightmap[0]) - 1 |
| 37 | + max_y = len(heightmap) - 1 |
| 38 | + for y, row in enumerate(heightmap): |
| 39 | + for x, height in enumerate(row): |
| 40 | + neighbors = get_neighbors(x, y, max_x, max_y) |
| 41 | + if all(height < heightmap[n.y][n.x] for n in neighbors): |
| 42 | + low_points.append(Point(x, y)) |
| 43 | + return low_points |
| 44 | + |
| 45 | + |
| 46 | +def flood_fill(heightmap, origin, max_x, max_y): |
| 47 | + neighbors = [origin] |
| 48 | + basin_members = {origin} |
| 49 | + while neighbors: |
| 50 | + current = neighbors.pop() |
| 51 | + new_neighbors = get_neighbors(current.x, current.y, max_x, max_y) |
| 52 | + for nn in new_neighbors: |
| 53 | + # Assuming all basins are surrounded by nines, which is true for the sample. |
| 54 | + if nn not in basin_members and heightmap[nn.y][nn.x] < 9: |
| 55 | + neighbors.append(nn) |
| 56 | + basin_members.add(nn) |
| 57 | + return len(basin_members) |
| 58 | + |
| 59 | + |
| 60 | +def part_1(heightmap, low_points): |
| 61 | + return sum(heightmap[lp.y][lp.x] + 1 for lp in low_points) |
| 62 | + |
| 63 | + |
| 64 | +def part_2(heightmap, low_points): |
| 65 | + max_x = len(heightmap[0]) - 1 |
| 66 | + max_y = len(heightmap) - 1 |
| 67 | + basin_sizes = [flood_fill(heightmap, lp, max_x, max_y) for lp in low_points] |
| 68 | + basin_sizes.sort(reverse=True) |
| 69 | + return prod(basin_sizes[:3]) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + sample_data = get_data(SAMPLE_PATH) |
| 74 | + assert part_1(*sample_data) == 15 |
| 75 | + assert part_2(*sample_data) == 1134 |
| 76 | + |
| 77 | + challenge_data = get_data(INPUT_PATH) |
| 78 | + print(part_1(*challenge_data)) # 554 |
| 79 | + print(part_2(*challenge_data)) # 1017792 |
0 commit comments