aboutsummaryrefslogtreecommitdiff
path: root/2022/08/puzzles.py
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2022-12-08 09:41:56 +0100
committerThomas Voss <mail@thomasvoss.com> 2022-12-08 09:45:30 +0100
commit2c56a9baf17d15ae06bcb0199c02d27700019070 (patch)
treeeb1ae69ef07a1075a365187d4c122de759d9c8c2 /2022/08/puzzles.py
parent4ab8876daa573ae49be0dda0507d18446cda8dba (diff)
Add 2022 day 8 solutions
Diffstat (limited to '2022/08/puzzles.py')
-rw-r--r--2022/08/puzzles.py61
1 files changed, 61 insertions, 0 deletions
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()