From 109ddc67b6a1bd18de6d5202a65c63a13623a081 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Thu, 12 Dec 2024 20:22:43 +0100 Subject: [PATCH] add 2022/day08.2 --- 2022/08/main.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/2022/08/main.py b/2022/08/main.py index c5d4fbe..7063369 100644 --- a/2022/08/main.py +++ b/2022/08/main.py @@ -43,17 +43,52 @@ def is_visible(trees: list, tree_y: int, tree_x: int) -> bool: return False +def scenic_score(trees: list, tree_y: int, tree_x: int) -> int: + height = len(trees) + width = len(trees[0]) + + score = 1 + + # check up, right, down, right + directions = [(-1, 0), (0, 1), (1, 0), (0, -1)] + for dy, dx in directions: + nxty = tree_y + dy + nxtx = tree_x + dx + visual_range = 1 + + # continue to check in the direction til border + while 0 < nxty < height - 1 and 0 < nxtx < width - 1: + # check size, stop if bigger tree found + if trees[tree_y][tree_x] <= trees[nxty][nxtx]: + break + + visual_range += 1 + nxtx += dx + nxty += dy + + score *= visual_range + + return score + + def main(): trees = readinput() + # part 1 visible = 0 for y in range(len(trees)): for x in range(len(trees[0])): if is_visible(trees, y, x): visible += 1 - print("Visible trees: %d" % visible) + # part 1 + scenic_scores = [] + for y in range(len(trees)): + for x in range(len(trees[0])): + scenic_scores.append(scenic_score(trees, y, x)) + print("Highest scenic score: %d" % max(scenic_scores)) + if __name__ == "__main__": main()