day8 - cleanup

This commit is contained in:
Stefan Harmuth 2022-12-09 12:21:57 +01:00
parent 36b8b4aa9d
commit a277190148

113
day08.py
View File

@ -1,9 +1,6 @@
from tools.aoc import AOCDay from tools.aoc import AOCDay
from typing import Any from typing import Any
from tools.coordinate import Coordinate
from tools.grid import Grid
class Day(AOCDay): class Day(AOCDay):
inputs = [ inputs = [
@ -19,93 +16,47 @@ class Day(AOCDay):
] ]
] ]
def get_tree_grid(self) -> Grid: def get_vision(self, part2: bool = False):
grid = Grid(0) trees = [list(map(int, l)) for l in self.getInput()]
for y, l in enumerate(self.getInput()): len_x = len(trees[0])
for x, v in enumerate(map(int, l)): len_y = len(trees)
grid.set(Coordinate(x, y), v)
return grid
def part1(self) -> Any:
tree_map = self.get_tree_grid()
visible = set() visible = set()
max_scenic = 0
for y in tree_map.rangeY(): dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)]
max_height = 0
for x in tree_map.rangeX():
pos = Coordinate(x, y)
if y == 0 or y == tree_map.maxY:
visible.add(pos)
continue
if tree_map.get(pos) > max_height:
visible.add(pos)
max_height = tree_map.get(pos)
max_height = 0 for x in range(len_x):
for x in tree_map.rangeX(reverse=True): for y in range(len_y):
if y == 0 or y == tree_map.maxY: scenic_score = 1
continue for d in dirs:
pos = Coordinate(x, y) vx, vy = x + d[0], y + d[1]
if tree_map.get(pos) > max_height: view = 0
visible.add(pos) while 0 <= vx < len_x and 0 <= vy < len_y:
max_height = tree_map.get(pos) view += 1
if trees[vy][vx] >= trees[y][x]:
break
for x in tree_map.rangeX(): vx += d[0]
max_height = 0 vy += d[1]
for y in tree_map.rangeY(): else:
pos = Coordinate(x, y) visible.add((x, y))
if x == 0 or x == tree_map.maxX:
visible.add(pos)
continue
if tree_map.get(pos) > max_height:
visible.add(pos)
max_height = tree_map.get(pos)
max_height = 0 scenic_score *= view
for y in tree_map.rangeY(reverse=True):
if x == 0 or x == tree_map.maxX:
continue
pos = Coordinate(x, y)
if tree_map.get(pos) > max_height:
visible.add(pos)
max_height = tree_map.get(pos)
if scenic_score > max_scenic:
max_scenic = scenic_score
if part2:
return max_scenic
else:
return len(visible) return len(visible)
def part1(self) -> Any:
return self.get_vision()
def part2(self) -> Any: def part2(self) -> Any:
tree_map = self.get_tree_grid() return self.get_vision(True)
max_score = 0
for x in tree_map.rangeX(-1):
for y in tree_map.rangeY(-1):
up, down, left, right = 0, 0, 0, 0
this_tree = Coordinate(x, y)
this_height = tree_map.get(this_tree)
for vx in range(x - 1, -1, -1):
left += 1
if tree_map.get(Coordinate(vx, y)) >= this_height:
break
for vx in range(x + 1, tree_map.maxX + 1):
right += 1
if tree_map.get(Coordinate(vx, y)) >= this_height:
break
for vy in range(y - 1, -1, -1):
up += 1
if tree_map.get(Coordinate(x, vy)) >= this_height:
break
for vy in range(y + 1, tree_map.maxY + 1):
down += 1
if tree_map.get(Coordinate(x, vy)) >= this_height:
break
score = up * down * left * right
if score > max_score:
max_score = score
return max_score
if __name__ == '__main__': if __name__ == '__main__':