-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-08.py
141 lines (119 loc) · 3.1 KB
/
day-08.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# import all the utils I might use
from __future__ import annotations
from aocd import get_data
from aocd import submit
import re
from dataclasses import dataclass, field
import typing as T
from collections import defaultdict
data = get_data(day=8, year=2022)
####### part 1
# test
test_data = \
"""30373
25512
65332
33549
35390"""
# un-comment below when ready to use real data
# data = test_data
lines = data.split('\n')
lines = [
[int(t) for t in line]
for line in lines
]
# width is X
# height is Y
Y = len(lines)
X = len(lines[0])
visible_on_edges = 2 * (Y + X) - 4
# visible, inside the edges
visible = set()
# scan in 4 directions
# scanning to the right, from top-left
for y in range(1, Y - 1):
tallest = lines[y][0]
for x in range(1, X - 1):
tree = lines[y][x]
if tree > tallest:
visible.add((x, y))
tallest = max(tree, tallest)
# scanning to the left, from top-right
for y in range(1, Y - 1):
tallest = lines[y][X - 1]
for x in range(X - 2, 0, -1):
tree = lines[y][x]
if tree > tallest:
visible.add((x, y))
tallest = max(tree, tallest)
# scanning to the bottom, from top-left
for x in range(1, X - 1):
tallest = lines[0][x]
for y in range(1, Y - 1):
tree = lines[y][x]
if tree > tallest:
visible.add((x, y))
tallest = max(tree, tallest)
# scanning to the top, from bottom-left
for x in range(1, X - 1):
tallest = lines[Y - 1][x]
for y in range(Y - 2, 0, -1):
tree = lines[y][x]
if tree > tallest:
visible.add((x, y))
tallest = max(tree, tallest)
visible_inside = len(list(visible))
print(visible_on_edges)
print(visible_inside)
ans = visible_inside + visible_on_edges
print(ans)
####### part 2
best_score = 1
# scan in 4 directions
# outer loop: iterate through all the trees
for y_t in range(1, Y - 1):
for x_t in range(1, X - 1):
# scanning to the right
tree_score = 1
score = 0
for x in range(x_t + 1, X):
score += 1
tree = lines[y_t][x]
if tree >= lines[y_t][x_t]:
break
tree_score *= score
if not tree_score:
break
# scanning to the left
score = 0
for x in range(x_t - 1, -1, -1):
score += 1
tree = lines[y_t][x]
if tree >= lines[y_t][x_t]:
break
tree_score *= score
if not tree_score:
break
# scanning to the bottom
score = 0
for y in range(y_t + 1, Y):
score += 1
tree = lines[y][x_t]
if tree >= lines[y_t][x_t]:
break
tree_score *= score
if not tree_score:
break
# scanning to the top
score = 0
for y in range(y_t - 1, -1, -1):
score += 1
tree = lines[y][x_t]
if tree >= lines[y_t][x_t]:
break
tree_score *= score
if not tree_score:
break
best_score = max(best_score, tree_score)
print(best_score)
ans = best_score