aboutsummaryrefslogtreecommitdiff
path: root/2021/09/puzzle-2.py
blob: d435f7cdc86aba9b2593ca4c301799966d797df6 (plain) (blame)
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
59
60
61
62
63
64
65
66
#!/usr/bin/env python3

"""
Most of the code taken from this guys floodfill, I just adapted it for this problem
https://playandlearntocode.com/article/flood-fill-algorithm-in-python
"""

from functools import reduce
from itertools import product
from operator import mul

matrix = list[list[int]]


def is_basin(grid: matrix, row: int, col: int) -> bool:
	return (
		not ((row < 0 or row > len(grid) - 1) or (col < 0 or col > len(grid[0]) - 1))
		and grid[row][col] != 9
	)


def floodfill(grid: matrix, row: int, col: int) -> int:
	if row < 0 or row > len(grid) - 1 or col < 0 or col > len(grid[0]) - 1 or grid[row][col] == 9:
		return 0

	q: list[tuple[int, int]] = []
	grid[row][col] = 9
	q.append((row, col))
	size = 1

	while len(q):
		row, col = q.pop(0)

		for x, y in ((-1, 0), (1, 0), (0, -1), (0, 1)):
			c_row = row + x
			c_col = col + y

			if is_basin(grid, c_row, c_col):
				grid[c_row][c_col] = 9
				q.append((c_row, c_col))
				size += 1

	return size


def solve(grid: matrix) -> int:
	return reduce(
		mul,
		sorted(
			map(
				lambda t: floodfill(grid, t[0], t[1]),
				filter(
					lambda t: grid[t[0]][t[1]] != 9, product(range(len(grid)), range(len(grid[0]))),
				),
			)
		)[-3:],
	)


def main() -> None:
	with open("input", "r", encoding="utf-8") as f:
		print(solve(list(map(lambda l: [int(n) for n in l.strip()], f.readlines()))))


if __name__ == "__main__":
	main()