-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.py
249 lines (184 loc) · 5.85 KB
/
day08.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import numpy.testing as npt
import pytest
import numpy as np
from typing import *
from functools import reduce
def read_grid(path):
with open(path, "rb") as f:
return np.array(
[[c - ord("0") for c in line.rstrip()] for line in f.readlines()]
)
@pytest.fixture(name="example")
def fixture_example():
yield read_grid("./day08_example.txt")
@pytest.fixture(name="big_example")
def fixture_big_example():
yield read_grid("./day08.txt")
def running_max_2d_lr(matrix):
return np.apply_along_axis(np.maximum.accumulate, 1, matrix)
def test_running_max_2d(example):
actual = running_max_2d_lr(example)
expected = np.array(
[
[3, 3, 3, 7, 7],
[2, 5, 5, 5, 5],
[6, 6, 6, 6, 6],
[3, 3, 5, 5, 9],
[3, 5, 5, 9, 9],
]
)
npt.assert_array_equal(actual, expected)
def running_max_2d_rl(matrix):
return np.fliplr(running_max_2d_lr(np.fliplr(matrix)))
def test_running_max_2d_rl(example):
actual = running_max_2d_rl(example)
expected = np.array(
[
[7, 7, 7, 7, 3],
[5, 5, 5, 2, 2],
[6, 5, 3, 3, 2],
[9, 9, 9, 9, 9],
[9, 9, 9, 9, 0],
]
)
npt.assert_array_equal(actual, expected)
def running_max_2d_tb(matrix):
return np.rot90(running_max_2d_lr(np.rot90(matrix)), 3)
def test_running_max_2d_tb(example):
actual = running_max_2d_tb(example)
expected = np.array(
[
[3, 0, 3, 7, 3],
[3, 5, 5, 7, 3],
[6, 5, 5, 7, 3],
[6, 5, 5, 7, 9],
[6, 5, 5, 9, 9],
]
)
npt.assert_array_equal(actual, expected)
def running_max_2d_bt(matrix):
return np.rot90(running_max_2d_lr(np.rot90(matrix, 3)))
def test_running_max_2d_bt(example):
actual = running_max_2d_bt(example)
expected = np.array(
[
[6, 5, 5, 9, 9],
[6, 5, 5, 9, 9],
[6, 5, 5, 9, 9],
[3, 5, 5, 9, 9],
[3, 5, 3, 9, 0],
]
)
npt.assert_array_equal(actual, expected)
def visible_from_left(col, row, grid, running_max) -> bool:
return col == 0 or running_max[row, col - 1] < grid[row, col]
def visible_from_right(col, row, grid, running_max) -> bool:
return col == len(grid) - 1 or running_max[row, col + 1] < grid[row, col]
def visible_from_top(col, row, grid, running_max) -> bool:
return row == 0 or running_max[row - 1, col] < grid[row, col]
def visible_from_bottom(col, row, grid, running_max) -> bool:
return row == len(grid) - 1 or running_max[row + 1, col] < grid[row, col]
def count_visible(grid) -> int:
left_right = running_max_2d_lr(grid)
right_left = running_max_2d_rl(grid)
top_bottom = running_max_2d_tb(grid)
bottom_top = running_max_2d_bt(grid)
visible = (
lambda col, row: visible_from_top(col, row, grid, top_bottom)
or visible_from_bottom(col, row, grid, bottom_top)
or visible_from_left(col, row, grid, left_right)
or visible_from_right(col, row, grid, right_left)
)
return sum(
(1 for row in range(len(grid)) for col in range(len(grid)) if visible(col, row))
)
def test_count_visible(example):
assert count_visible(example) == 21
def sightlines_lr(matrix):
running_max = running_max_2d_lr(matrix)
result = np.ones_like(matrix)
for row in range(len(matrix)):
result[row, 0] = 0
for col in range(1, len(matrix)):
if running_max[row, col - 1] < matrix[row, col]:
result[row, col] = col
elif matrix[row, col - 1] < matrix[row, col]:
col0 = col - 1
result[row, col] = 0
# How to get rid of this loop?
while 0 <= col0:
result[row, col] += 1
if matrix[row, col] <= matrix[row, col0]:
break
col0 -= 1
return result
def test_sightlines_lr(example):
actual = sightlines_lr(example)
expected = np.array(
[
[0, 1, 2, 3, 1],
[0, 1, 1, 1, 2],
[0, 1, 1, 1, 1],
[0, 1, 2, 1, 4],
[0, 1, 1, 3, 1],
]
)
npt.assert_array_equal(actual, expected)
def test_sightlines_lr2():
example = np.array(
[
[5, 1, 1, 5, 9],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
]
)
actual = sightlines_lr(example)
expected = np.array(
[
[0, 1, 1, 3, 4],
[0, 1, 1, 1, 1],
[0, 1, 1, 1, 1],
[0, 1, 1, 1, 1],
[0, 1, 1, 1, 1],
]
)
npt.assert_array_equal(actual, expected)
def sightlines_rl(matrix):
return np.fliplr(sightlines_lr(np.fliplr(matrix)))
def test_sightlines_rl(example):
actual = sightlines_rl(example)
expected = np.array(
[
[2, 1, 1, 1, 0],
[1, 1, 2, 1, 0],
[4, 3, 1, 1, 0],
[1, 1, 2, 1, 0],
[1, 2, 1, 1, 0],
]
)
npt.assert_array_equal(
actual,
expected,
)
def sightlines_tb(matrix):
return np.rot90(sightlines_lr(np.rot90(matrix)), 3)
def sightlines_bt(matrix):
return np.rot90(sightlines_lr(np.rot90(matrix, 3)))
def highest_scenic_score(grid) -> int:
matrices = [
sightlines_lr(grid),
sightlines_rl(grid),
sightlines_tb(grid),
sightlines_bt(grid),
]
return np.max(reduce(np.multiply, matrices))
def test_highest_scenic_score(example):
assert highest_scenic_score(example) == 8
def test_highest_scenic_score_big_example(big_example):
assert highest_scenic_score(big_example) == 287040
if __name__ == "__main__":
grid = read_grid("./day08.txt")
print(count_visible(grid))
print(highest_scenic_score(grid))