diff options
Diffstat (limited to 'day8')
| -rw-r--r-- | day8/__init__.py | 55 |
1 files changed, 29 insertions, 26 deletions
diff --git a/day8/__init__.py b/day8/__init__.py index 08f9925..0620e7c 100644 --- a/day8/__init__.py +++ b/day8/__init__.py | |||
| @@ -1,6 +1,5 @@ | |||
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | from abc import ABC | 2 | from typing import Iterator, Any, List, Tuple |
| 3 | from typing import Iterator, Any, List | ||
| 4 | 3 | ||
| 5 | from aoc import BaseAssignment | 4 | from aoc import BaseAssignment |
| 6 | 5 | ||
| @@ -14,6 +13,14 @@ class Assignment(BaseAssignment): | |||
| 14 | def col_at(self, x: int) -> List[int]: | 13 | def col_at(self, x: int) -> List[int]: |
| 15 | return [int(i[x]) for i in self.trees] | 14 | return [int(i[x]) for i in self.trees] |
| 16 | 15 | ||
| 16 | def inner_trees(self) -> Iterator[Tuple[int, int]]: | ||
| 17 | width = len(self.trees[0]) | ||
| 18 | height = len(self.trees) | ||
| 19 | |||
| 20 | for x in range(1, width - 1): | ||
| 21 | for y in range(1, height - 1): | ||
| 22 | yield x, y | ||
| 23 | |||
| 17 | 24 | ||
| 18 | class AssignmentOne(Assignment): | 25 | class AssignmentOne(Assignment): |
| 19 | example_result = 21 | 26 | example_result = 21 |
| @@ -42,10 +49,9 @@ class AssignmentOne(Assignment): | |||
| 42 | 49 | ||
| 43 | self.is_visible(self.col_at(1), 2) | 50 | self.is_visible(self.col_at(1), 2) |
| 44 | 51 | ||
| 45 | for x in range(1, width - 1): | 52 | for x, y in self.inner_trees(): |
| 46 | for y in range(1, height - 1): | 53 | if self.tree_visible(x, y): |
| 47 | if self.tree_visible(x, y): | 54 | visible_inside += 1 |
| 48 | visible_inside += 1 | ||
| 49 | 55 | ||
| 50 | return visible_inside + ((width - 1) * 2) + ((height - 1) * 2) | 56 | return visible_inside + ((width - 1) * 2) + ((height - 1) * 2) |
| 51 | 57 | ||
| @@ -53,24 +59,25 @@ class AssignmentOne(Assignment): | |||
| 53 | class AssignmentTwo(Assignment): | 59 | class AssignmentTwo(Assignment): |
| 54 | example_result = 8 | 60 | example_result = 8 |
| 55 | 61 | ||
| 56 | @staticmethod | 62 | @classmethod |
| 57 | def calculate_1d_scenic_score(trees: List[int], index: int) -> int: | 63 | def calculate_score(cls, value: int, values: List[int]) -> int: |
| 64 | score = 0 | ||
| 65 | for score, v in enumerate(values): | ||
| 66 | if v >= value: | ||
| 67 | break | ||
| 68 | |||
| 69 | return score + 1 | ||
| 70 | |||
| 71 | @classmethod | ||
| 72 | def calculate_1d_scenic_score(cls, trees: List[int], index: int) -> int: | ||
| 58 | first_half = list(reversed(trees[:index])) | 73 | first_half = list(reversed(trees[:index])) |
| 59 | second_half = trees[index + 1 :] | 74 | second_half = trees[index + 1 :] |
| 60 | 75 | ||
| 61 | value = trees[index] | 76 | value = trees[index] |
| 62 | 77 | ||
| 63 | score_first_half = 0 | 78 | return cls.calculate_score(value, first_half) * cls.calculate_score( |
| 64 | for score_first_half, tree in enumerate(first_half): | 79 | value, second_half |
| 65 | if tree >= value: | 80 | ) |
| 66 | break | ||
| 67 | |||
| 68 | score_second_half = 0 | ||
| 69 | for score_second_half, tree in enumerate(second_half): | ||
| 70 | if tree >= value: | ||
| 71 | break | ||
| 72 | |||
| 73 | return (score_first_half + 1) * (score_second_half + 1) | ||
| 74 | 81 | ||
| 75 | def calculate_scenic_score(self, x: int, y: int) -> int: | 82 | def calculate_scenic_score(self, x: int, y: int) -> int: |
| 76 | col = self.col_at(x) | 83 | col = self.col_at(x) |
| @@ -83,14 +90,10 @@ class AssignmentTwo(Assignment): | |||
| 83 | def run(self, input: Iterator) -> Any: | 90 | def run(self, input: Iterator) -> Any: |
| 84 | self.trees = list(input) | 91 | self.trees = list(input) |
| 85 | 92 | ||
| 86 | width = len(self.trees[0]) | ||
| 87 | height = len(self.trees) | ||
| 88 | |||
| 89 | scenic_scores: List[int] = [] | 93 | scenic_scores: List[int] = [] |
| 90 | 94 | ||
| 91 | for x in range(1, width - 1): | 95 | for x, y in self.inner_trees(): |
| 92 | for y in range(1, height - 1): | 96 | scenic_score = self.calculate_scenic_score(x, y) |
| 93 | scenic_score = self.calculate_scenic_score(x, y) | 97 | scenic_scores.append(scenic_score) |
| 94 | scenic_scores.append(scenic_score) | ||
| 95 | 98 | ||
| 96 | return max(scenic_scores) | 99 | return max(scenic_scores) |
