-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.py
73 lines (54 loc) · 1.86 KB
/
day8.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
import util
import numpy as np
test_data: str = \
"""30373
25512
65332
33549
35390"""
def task1(input):
visible: np.ndarray = np.zeros((len(input) - 2, len(input) - 2), dtype=int)
for x in range(len(visible)):
for y in range(len(visible[0])):
dirs = [[0, 1], [0, -1], [1, 0], [-1, 0]]
for dir in dirs:
pos = [x + 1, y + 1]
shorter = True
while 0 < pos[0] < len(input) - 1 and 0 < pos[1] < len(input) - 1:
pos = [pos[0] + dir[0], pos[1] + dir[1]]
if input[pos[0], pos[1]] >= input[x + 1, y + 1]:
shorter = False
break
if shorter:
visible[x, y] = 1
return visible.sum() + 4 * (len(input) - 1)
def task2(input):
visible: np.ndarray = np.ones((len(input) - 2, len(input) - 2), dtype=int)
for x in range(len(visible)):
for y in range(len(visible[0])):
dirs = [[0, 1], [0, -1], [1, 0], [-1, 0]]
for dir in dirs:
pos = [x + 1, y + 1]
vis = 0
while 0 < pos[0] < len(input) - 1 and 0 < pos[1] < len(input) - 1:
pos = [pos[0] + dir[0], pos[1] + dir[1]]
vis += 1
if input[pos[0], pos[1]] >= input[x + 1, y + 1]:
break
visible[x, y] *= vis
return visible.max()
def parse(input) -> np.ndarray:
input = util.as_lines(input)
grid = np.zeros((len(input), len(input)), dtype=int)
for i, line in enumerate(input):
for j, c in enumerate(line):
grid[i, j] = int(c)
return grid
def main():
data: str = util.get(8, 2022)
# data = test_data
input = parse(data)
print(task1(input))
print(task2(input))
if __name__ == "__main__":
main()