day8 - cleanup

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

121
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) visible = set()
max_scenic = 0
dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)]
for x in range(len_x):
for y in range(len_y):
scenic_score = 1
for d in dirs:
vx, vy = x + d[0], y + d[1]
view = 0
while 0 <= vx < len_x and 0 <= vy < len_y:
view += 1
if trees[vy][vx] >= trees[y][x]:
break
vx += d[0]
vy += d[1]
else:
visible.add((x, y))
scenic_score *= view
if scenic_score > max_scenic:
max_scenic = scenic_score
if part2:
return max_scenic
else:
return len(visible)
return grid
def part1(self) -> Any: def part1(self) -> Any:
tree_map = self.get_tree_grid() return self.get_vision()
visible = set()
for y in tree_map.rangeY():
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 tree_map.rangeX(reverse=True):
if y == 0 or y == tree_map.maxY:
continue
pos = Coordinate(x, y)
if tree_map.get(pos) > max_height:
visible.add(pos)
max_height = tree_map.get(pos)
for x in tree_map.rangeX():
max_height = 0
for y in tree_map.rangeY():
pos = Coordinate(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
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)
return len(visible)
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__':