day8 - cleanup
This commit is contained in:
parent
36b8b4aa9d
commit
a277190148
113
day08.py
113
day08.py
@ -1,9 +1,6 @@
|
||||
from tools.aoc import AOCDay
|
||||
from typing import Any
|
||||
|
||||
from tools.coordinate import Coordinate
|
||||
from tools.grid import Grid
|
||||
|
||||
|
||||
class Day(AOCDay):
|
||||
inputs = [
|
||||
@ -19,93 +16,47 @@ class Day(AOCDay):
|
||||
]
|
||||
]
|
||||
|
||||
def get_tree_grid(self) -> Grid:
|
||||
grid = Grid(0)
|
||||
for y, l in enumerate(self.getInput()):
|
||||
for x, v in enumerate(map(int, l)):
|
||||
grid.set(Coordinate(x, y), v)
|
||||
|
||||
return grid
|
||||
|
||||
def part1(self) -> Any:
|
||||
tree_map = self.get_tree_grid()
|
||||
def get_vision(self, part2: bool = False):
|
||||
trees = [list(map(int, l)) for l in self.getInput()]
|
||||
len_x = len(trees[0])
|
||||
len_y = len(trees)
|
||||
visible = set()
|
||||
max_scenic = 0
|
||||
|
||||
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)
|
||||
dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)]
|
||||
|
||||
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 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
|
||||
|
||||
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)
|
||||
vx += d[0]
|
||||
vy += d[1]
|
||||
else:
|
||||
visible.add((x, y))
|
||||
|
||||
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)
|
||||
scenic_score *= view
|
||||
|
||||
if scenic_score > max_scenic:
|
||||
max_scenic = scenic_score
|
||||
|
||||
if part2:
|
||||
return max_scenic
|
||||
else:
|
||||
return len(visible)
|
||||
|
||||
|
||||
def part1(self) -> Any:
|
||||
return self.get_vision()
|
||||
|
||||
def part2(self) -> Any:
|
||||
tree_map = self.get_tree_grid()
|
||||
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
|
||||
return self.get_vision(True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Loading…
Reference in New Issue
Block a user