summaryrefslogtreecommitdiffstats
path: root/8.py
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2022-12-10 12:32:32 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2022-12-10 12:32:32 +0000
commit803e8d3e3812f72471c35d90a25b86d05263ed83 (patch)
tree875e7e98893b817af515d5ed0a8a378de409fa0a /8.py
parentd9a97e9bf28cf91f1a7e57f1298b7c742e25ed56 (diff)
downloadaoc2022-803e8d3e3812f72471c35d90a25b86d05263ed83.tar.gz
aoc2022-803e8d3e3812f72471c35d90a25b86d05263ed83.tar.xz
aoc2022-803e8d3e3812f72471c35d90a25b86d05263ed83.zip
8, 10
Diffstat (limited to '8.py')
-rw-r--r--8.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/8.py b/8.py
new file mode 100644
index 0000000..152517c
--- /dev/null
+++ b/8.py
@@ -0,0 +1,64 @@
+from functools import reduce
+
+with open('8.in') as f:
+ grid = [[int(c) for c in line.rstrip()] for line in f]
+
+visible = set()
+
+for y, row in enumerate(grid):
+ height = -1
+ for x, t in enumerate(row):
+ if t > height:
+ visible.add((x, y))
+ height = t
+ height = -1
+ for x, t in reversed(list(enumerate(row))):
+ if t > height:
+ visible.add((x, y))
+ height = t
+
+for x in range(len(grid[0])):
+ height = -1
+ for y, row in enumerate(grid):
+ t = row[x]
+ if t > height:
+ visible.add((x, y))
+ height = t
+ height = -1
+ for y, row in reversed(list(enumerate(grid))):
+ t = row[x]
+ if t > height:
+ visible.add((x, y))
+ height = t
+
+print(len(visible))
+
+def calc_scenic(grid, x, y):
+ height = grid[y][x]
+ scores = [0 for _ in range(4)]
+ for nx in range(x+1, len(grid[y])):
+ # print('right', nx, y)
+ scores[0] += 1
+ if grid[y][nx] >= height:
+ break
+ for nx in range(x-1, -1, -1):
+ # print('left', nx, y)
+ scores[1] += 1
+ if grid[y][nx] >= height:
+ break
+ for ny in range(y+1, len(grid)):
+ # print('down', x, ny)
+ scores[2] += 1
+ if grid[ny][x] >= height:
+ break
+ for ny in range(y-1, -1, -1):
+ # print('up', x, ny)
+ scores[3] += 1
+ if grid[ny][x] >= height:
+ break
+ # print(scores)
+ return reduce(lambda x, y: x * y, scores, 1)
+
+maxscore = 0
+# print(calc_scenic(grid, 2, 3))
+print(max(calc_scenic(grid, x, y) for x in range(len(grid[0])) for y in range(len(grid))))