aoc
/
2022
1
0
Fork 0
2022/day08.py

46 lines
1.1 KiB
Python
Raw Normal View History

2022-12-24 14:59:27 +00:00
import fileinput
from pprint import pprint
inp = [[int(y) for y in x.strip()] for x in fileinput.input()]
inp_ = list(zip(*inp))
part1 = 0
for y in range(1, len(inp) - 1):
for x in range(1, len(inp[0]) - 1):
x_min_max = max(inp_[x][:y])
y_min_max = max(inp[y][:x])
x_max_max = max(inp_[x][y + 1 :])
y_max_max = max(inp[y][x + 1 :])
n = inp[y][x]
if n > x_min_max or n > y_min_max or n > x_max_max or n > y_max_max:
part1 += 1
part1 += (len(inp) - 1) * 4
def viewing_dist(x: int, y: int, dx: int, dy: int) -> int:
dist = 0
n = inp[y][x]
m = 0
while n > m:
x += dx
y += dy
if x < 0 or y < 0 or y >= len(inp) or x >= len(inp[0]):
break
dist += 1
m = inp[y][x]
return dist
scores = []
for y in range(1, len(inp) - 1):
for x in range(1, len(inp[0]) - 1):
a = viewing_dist(x, y, 0, -1)
b = viewing_dist(x, y, -1, 0)
c = viewing_dist(x, y, 1, 0)
d = viewing_dist(x, y, 0, 1)
scores.append(a * b * c * d)
print(part1, max(scores))