| MAP = [] | |||||
| DEBUG = False | |||||
| class Tree: | |||||
| def __init__(self, x_pos, y_pos, height): | |||||
| self.x = x_pos | |||||
| self.y = y_pos | |||||
| self.height = height | |||||
| self.view_up = 0 | |||||
| self.view_down = 0 | |||||
| self.view_left = 0 | |||||
| self.view_right = 0 | |||||
| def check_left(self): | |||||
| if DEBUG: | |||||
| print("### Checking left ###") | |||||
| trees = MAP[self.y][:self.x] | |||||
| tallest = 0 | |||||
| distance = 0 | |||||
| tallest_distance = 0 | |||||
| for tree in trees[::-1]: | |||||
| distance += 1 | |||||
| if DEBUG: | |||||
| print(f"{self.height=}, {tree=}, {distance=}, {tallest=}, {tallest_distance=}") | |||||
| if tree >= self.height: | |||||
| tallest_distance = distance | |||||
| break | |||||
| if tree > tallest: | |||||
| tallest = tree | |||||
| tallest_distance = distance | |||||
| self.view_left = tallest_distance | |||||
| def check_right(self): | |||||
| if DEBUG: | |||||
| print("### Checking right ###") | |||||
| trees = MAP[self.y][self.x+1:] | |||||
| tallest, distance, tallest_distance = 0, 0, 0 | |||||
| for tree in trees: | |||||
| distance += 1 | |||||
| if DEBUG: | |||||
| print(f"{self.height=}, {tree=}, {distance=}, {tallest=}, {tallest_distance=}") | |||||
| if tree >= self.height: | |||||
| tallest_distance = distance | |||||
| break | |||||
| if tree > tallest: | |||||
| tallest = tree | |||||
| tallest_distance = distance | |||||
| self.view_right = tallest_distance | |||||
| def check_up(self): | |||||
| if DEBUG: | |||||
| print("### Checking up ###") | |||||
| trees = [row[self.x] for row in MAP[:self.y]] | |||||
| tallest, distance, tallest_distance = 0, 0, 0 | |||||
| for tree in trees[::-1]: | |||||
| distance += 1 | |||||
| if DEBUG: | |||||
| print(f"{self.height=}, {tree=}, {distance=}, {tallest=}, {tallest_distance=}") | |||||
| if tree >= self.height: | |||||
| tallest_distance = distance | |||||
| break | |||||
| if tree > tallest: | |||||
| tallest = tree | |||||
| tallest_distance = distance | |||||
| self.view_up = tallest_distance | |||||
| def check_down(self): | |||||
| if DEBUG: | |||||
| print("### Checking down ###") | |||||
| trees = [row[self.x] for row in MAP[self.y+1:]] | |||||
| tallest, distance, tallest_distance = 0, 0, 0 | |||||
| for tree in trees: | |||||
| distance += 1 | |||||
| if DEBUG: | |||||
| print(f"{self.height=}, {tree=}, {distance=}, {tallest=}, {tallest_distance=}") | |||||
| if tree >= self.height: | |||||
| tallest_distance = distance | |||||
| break | |||||
| if tree > tallest: | |||||
| tallest = tree | |||||
| tallest_distance = distance | |||||
| self.view_down = tallest_distance | |||||
| @property | |||||
| def scenic_score(self): | |||||
| self.check_down() | |||||
| self.check_up() | |||||
| self.check_left() | |||||
| self.check_right() | |||||
| return self.view_down + self.view_up + self.view_left + self.view_right | |||||
| def main(): | |||||
| global MAP, DEBUG | |||||
| with open("day08.input", "r") as file: | |||||
| lines = [line.strip() for line in file.readlines()] | |||||
| MAP = [[int(char) for char in line] for line in lines] | |||||
| trees = [] | |||||
| for y, line in enumerate(MAP): | |||||
| for x, tree in enumerate(line): | |||||
| trees.append(Tree(x, y, tree)) | |||||
| max_score = max(trees, key=lambda t: t.scenic_score) | |||||
| # max_score = Tree(67, 38, MAP[38][67]) | |||||
| # DEBUG = True | |||||
| print(max_score.scenic_score) | |||||
| print(max_score.__dict__) | |||||
| if __name__ == "__main__": | |||||
| main() |