114 lines
3.4 KiB
Python
114 lines
3.4 KiB
Python
from tools.aoc import AOCDay
|
|
from typing import Any
|
|
|
|
from tools.coordinate import Coordinate
|
|
from tools.grid import Grid
|
|
|
|
|
|
class Day(AOCDay):
|
|
inputs = [
|
|
[
|
|
(21, "input8_test"),
|
|
(1854, "input8_dennis"),
|
|
(1679, "input8"),
|
|
],
|
|
[
|
|
(8, "input8_test"),
|
|
(527340, "input8_dennis"),
|
|
(536625, "input8"),
|
|
]
|
|
]
|
|
|
|
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()
|
|
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:
|
|
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
|
|
|
|
|
|
if __name__ == '__main__':
|
|
day = Day(2022, 8)
|
|
day.run(verbose=True)
|