diff options
Diffstat (limited to '2021/09')
-rwxr-xr-x | 2021/09/puzzle-1.py | 42 | ||||
-rwxr-xr-x | 2021/09/puzzle-2.py | 10 |
2 files changed, 28 insertions, 24 deletions
diff --git a/2021/09/puzzle-1.py b/2021/09/puzzle-1.py index 5ab2f45..be333a5 100755 --- a/2021/09/puzzle-1.py +++ b/2021/09/puzzle-1.py @@ -1,29 +1,33 @@ #!/usr/bin/env python3 +from itertools import product + +from libaoc import read_int_matrix + + def main() -> None: with open("input", "r", encoding="utf-8") as f: - data = list(map(lambda l: [int(n) for n in l.strip()], f.readlines())) + data = read_int_matrix(f) acc = 0 - rows = len(data[0]) - cols = len(data) - - for i in range(cols): - for j in range(rows): - if ( - i != 0 - and data[i - 1][j] <= data[i][j] - or i != cols - 1 - and data[i + 1][j] <= data[i][j] - or j != 0 - and data[i][j - 1] <= data[i][j] - or j != rows - 1 - and data[i][j + 1] <= data[i][j] - ): - continue - - acc += data[i][j] + 1 + rows = len(data) + cols = len(data[0]) + + for i, j in product(range(rows), range(cols)): + if ( + i != 0 + and data[i - 1][j] <= data[i][j] + or i != cols - 1 + and data[i + 1][j] <= data[i][j] + or j != 0 + and data[i][j - 1] <= data[i][j] + or j != rows - 1 + and data[i][j + 1] <= data[i][j] + ): + continue + + acc += data[i][j] + 1 print(acc) diff --git a/2021/09/puzzle-2.py b/2021/09/puzzle-2.py index d435f7c..72e0752 100755 --- a/2021/09/puzzle-2.py +++ b/2021/09/puzzle-2.py @@ -9,17 +9,17 @@ from functools import reduce from itertools import product from operator import mul -matrix = list[list[int]] +from libaoc import matrix, read_int_matrix -def is_basin(grid: matrix, row: int, col: int) -> bool: +def is_basin(grid: matrix[int], 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: +def floodfill(grid: matrix[int], 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 @@ -43,7 +43,7 @@ def floodfill(grid: matrix, row: int, col: int) -> int: return size -def solve(grid: matrix) -> int: +def solve(grid: matrix[int]) -> int: return reduce( mul, sorted( @@ -59,7 +59,7 @@ def solve(grid: matrix) -> int: 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())))) + print(solve(read_int_matrix(f))) if __name__ == "__main__": |