-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.py
executable file
·76 lines (51 loc) · 1.61 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
from run_util import run_puzzle
D_X = [(-1, 0), (1, 0)]
D_Y = [(0, -1), (0, 1)]
D_XY = D_X + D_Y
def part_a(data):
grid = [[int(char) for char in line] for line in data.strip().split('\n')]
x_max = len(grid)
y_max = len(grid[0])
def is_visible(x, y):
for d_x, d_y in D_XY:
n_x, n_y = x + d_x, y + d_y
while 0 <= n_x < x_max and 0 <= n_y < y_max and grid[n_x][n_y] < grid[x][y]:
n_x += d_x
n_y += d_y
if not (0 <= n_x < x_max and 0 <= n_y < y_max):
return True
return False
count = sum([is_visible(x, y) for x in range(x_max) for y in range(y_max)])
return count
def part_b(data):
grid = [[int(char) for char in line] for line in data.strip().split('\n')]
x_max = len(grid)
y_max = len(grid[0])
def get_scenic_score(x, y):
scenic_score = 1
for d_x, d_y in D_XY:
direction_score = 0
n_x, n_y = x + d_x, y + d_y
while 0 <= n_x < x_max and 0 <= n_y < y_max:
direction_score += 1
if grid[n_x][n_y] >= grid[x][y]:
break
n_x += d_x
n_y += d_y
scenic_score *= direction_score
return scenic_score
best_score = max([get_scenic_score(x, y) for x in range(x_max) for y in range(y_max)])
return best_score
def main():
examples = [
("""30373
25512
65332
33549
35390
""", 21, 8)
]
day = int(__file__.split('/')[-1].split('.')[0][-2:])
run_puzzle(day, part_a, part_b, examples)
if __name__ == '__main__':
main()