From a277190148d9f0cb6cabbf9eff74eba645b7b6c8 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Fri, 9 Dec 2022 12:21:57 +0100 Subject: [PATCH] day8 - cleanup --- day08.py | 121 +++++++++++++++++-------------------------------------- 1 file changed, 36 insertions(+), 85 deletions(-) diff --git a/day08.py b/day08.py index 335969f..64a215f 100644 --- a/day08.py +++ b/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) + 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 + + 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: - 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) + 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__':