From 2c56a9baf17d15ae06bcb0199c02d27700019070 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 8 Dec 2022 09:41:56 +0100 Subject: Add 2022 day 8 solutions --- 2022/08/puzzles.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 2022/08/puzzles.py (limited to '2022/08/puzzles.py') diff --git a/2022/08/puzzles.py b/2022/08/puzzles.py new file mode 100644 index 0000000..9bec4a1 --- /dev/null +++ b/2022/08/puzzles.py @@ -0,0 +1,61 @@ +#!/usr/bin/python3 + +from itertools import product + +from libaoc import matrix, read_int_matrix + +rows: int +cols: int + + +def check( + data: matrix[int], + start: tuple[int, int] +# START PART 1 +) -> bool: +# END PART 1 START PART 2 +) -> int: + p = 1 +# END PART 2 + o = data[start[0]][start[1]] + for di, dj in ((+1, 0), (-1, 0), (0, +1), (0, -1)): + # START PART 2 + c = 0 + # END PART 2 + i, j = start + while 0 < i < cols - 1 and 0 < j < rows - 1: + i += di + j += dj + # START PART 2 + c += 1 + # END PART 2 + if data[i][j] >= o: + break + # START PART 1 + else: + return True + return False + # END PART 1 START PART 2 + p *= c + return p + # END PART 2 + + +def main() -> None: + global rows, cols + + # START PART 1 + sum_or_max = sum + # END PART 1 START PART 2 + sum_or_max = max + # END PART 2 + + with open("input", "r") as f: + data = read_int_matrix(f) + + rows, cols = len(data), len(data[0]) + print(sum_or_max(check(data, (i, j)) for i, j in product(range(rows), range(cols)))) + + +if __name__ == "__main__": + main() -- cgit v1.2.3