From d18f57876311baf252e5eaa6ece0a86427e03f30 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Thu, 8 Dec 2022 23:07:13 +0100 Subject: Day 8 --- day8/__init__.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- day8/test_init.py | 7 +++++ 2 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 day8/test_init.py (limited to 'day8') diff --git a/day8/__init__.py b/day8/__init__.py index 21e18d2..08f9925 100644 --- a/day8/__init__.py +++ b/day8/__init__.py @@ -11,17 +11,86 @@ class Assignment(BaseAssignment): def row_at(self, y: int) -> List[int]: return [int(i) for i in self.trees[y]] - def col_at(self, x: int): + def col_at(self, x: int) -> List[int]: return [int(i[x]) for i in self.trees] + +class AssignmentOne(Assignment): + example_result = 21 + + def is_visible(self, trees: List[int], index: int) -> bool: + highest_first_half = max(trees[:index]) + highest_second_half = max(trees[index + 1 :]) + + value = trees[index] + + return highest_first_half < value or highest_second_half < value + + def tree_visible(self, x: int, y: int) -> bool: + col = self.col_at(x) + row = self.row_at(y) + + return self.is_visible(col, y) or self.is_visible(row, x) + def run(self, input: Iterator) -> Any: self.trees = list(input) width = len(self.trees[0]) height = len(self.trees) - return ((width - 1) * 2) + ((height - 1) * 2) + visible_inside = 0 + self.is_visible(self.col_at(1), 2) -class AssignmentOne(Assignment): - example_result = 21 + for x in range(1, width - 1): + for y in range(1, height - 1): + if self.tree_visible(x, y): + visible_inside += 1 + + return visible_inside + ((width - 1) * 2) + ((height - 1) * 2) + + +class AssignmentTwo(Assignment): + example_result = 8 + + @staticmethod + def calculate_1d_scenic_score(trees: List[int], index: int) -> int: + first_half = list(reversed(trees[:index])) + second_half = trees[index + 1 :] + + value = trees[index] + + score_first_half = 0 + for score_first_half, tree in enumerate(first_half): + if tree >= value: + break + + score_second_half = 0 + for score_second_half, tree in enumerate(second_half): + if tree >= value: + break + + return (score_first_half + 1) * (score_second_half + 1) + + def calculate_scenic_score(self, x: int, y: int) -> int: + col = self.col_at(x) + row = self.row_at(y) + + return self.calculate_1d_scenic_score(col, y) * self.calculate_1d_scenic_score( + row, x + ) + + def run(self, input: Iterator) -> Any: + self.trees = list(input) + + width = len(self.trees[0]) + height = len(self.trees) + + scenic_scores: List[int] = [] + + for x in range(1, width - 1): + for y in range(1, height - 1): + scenic_score = self.calculate_scenic_score(x, y) + scenic_scores.append(scenic_score) + + return max(scenic_scores) diff --git a/day8/test_init.py b/day8/test_init.py new file mode 100644 index 0000000..6fd0e2b --- /dev/null +++ b/day8/test_init.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +from day8 import AssignmentTwo + + +def test_calculate_1d_scenic_score(): + assert 1 * 2 == AssignmentTwo.calculate_1d_scenic_score([3, 5, 3, 5, 3], 1) + assert 1 * 2 == AssignmentTwo.calculate_1d_scenic_score([2, 5, 5, 1, 2], 2) -- cgit v1.2.3