summaryrefslogtreecommitdiffstats
path: root/day8/__init__.py
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-12-08 23:07:13 +0100
committerGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-12-08 23:07:13 +0100
commitd18f57876311baf252e5eaa6ece0a86427e03f30 (patch)
treec74be4766e2dd29e13521ff0c88e8eee1afc9dba /day8/__init__.py
parent921f795917fd297d1003ef869d1cbf9b8a6bd5db (diff)
download2022-d18f57876311baf252e5eaa6ece0a86427e03f30.tar.gz
2022-d18f57876311baf252e5eaa6ece0a86427e03f30.tar.bz2
2022-d18f57876311baf252e5eaa6ece0a86427e03f30.zip
Day 8
Diffstat (limited to 'day8/__init__.py')
-rw-r--r--day8/__init__.py77
1 files changed, 73 insertions, 4 deletions
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):
11 def row_at(self, y: int) -> List[int]: 11 def row_at(self, y: int) -> List[int]:
12 return [int(i) for i in self.trees[y]] 12 return [int(i) for i in self.trees[y]]
13 13
14 def col_at(self, x: int): 14 def col_at(self, x: int) -> List[int]:
15 return [int(i[x]) for i in self.trees] 15 return [int(i[x]) for i in self.trees]
16 16
17
18class AssignmentOne(Assignment):
19 example_result = 21
20
21 def is_visible(self, trees: List[int], index: int) -> bool:
22 highest_first_half = max(trees[:index])
23 highest_second_half = max(trees[index + 1 :])
24
25 value = trees[index]
26
27 return highest_first_half < value or highest_second_half < value
28
29 def tree_visible(self, x: int, y: int) -> bool:
30 col = self.col_at(x)
31 row = self.row_at(y)
32
33 return self.is_visible(col, y) or self.is_visible(row, x)
34
17 def run(self, input: Iterator) -> Any: 35 def run(self, input: Iterator) -> Any:
18 self.trees = list(input) 36 self.trees = list(input)
19 37
20 width = len(self.trees[0]) 38 width = len(self.trees[0])
21 height = len(self.trees) 39 height = len(self.trees)
22 40
23 return ((width - 1) * 2) + ((height - 1) * 2) 41 visible_inside = 0
24 42
43 self.is_visible(self.col_at(1), 2)
25 44
26class AssignmentOne(Assignment): 45 for x in range(1, width - 1):
27 example_result = 21 46 for y in range(1, height - 1):
47 if self.tree_visible(x, y):
48 visible_inside += 1
49
50 return visible_inside + ((width - 1) * 2) + ((height - 1) * 2)
51
52
53class AssignmentTwo(Assignment):
54 example_result = 8
55
56 @staticmethod
57 def calculate_1d_scenic_score(trees: List[int], index: int) -> int:
58 first_half = list(reversed(trees[:index]))
59 second_half = trees[index + 1 :]
60
61 value = trees[index]
62
63 score_first_half = 0
64 for score_first_half, tree in enumerate(first_half):
65 if tree >= value:
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
75 def calculate_scenic_score(self, x: int, y: int) -> int:
76 col = self.col_at(x)
77 row = self.row_at(y)
78
79 return self.calculate_1d_scenic_score(col, y) * self.calculate_1d_scenic_score(
80 row, x
81 )
82
83 def run(self, input: Iterator) -> Any:
84 self.trees = list(input)
85
86 width = len(self.trees[0])
87 height = len(self.trees)
88
89 scenic_scores: List[int] = []
90
91 for x in range(1, width - 1):
92 for y in range(1, height - 1):
93 scenic_score = self.calculate_scenic_score(x, y)
94 scenic_scores.append(scenic_score)
95
96 return max(scenic_scores)