-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8.py
More file actions
77 lines (59 loc) · 2.09 KB
/
day8.py
File metadata and controls
77 lines (59 loc) · 2.09 KB
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
import itertools
def read_input():
with open("inputs/day8.txt") as fin:
return [list(map(int, [*l.strip()])) for l in fin]
def horiz_visible(trees, left):
row_range = range(len(trees[0])) if left else range(len(trees[0]) - 1, -1, -1)
visible = [[False] * len(trees[0]) for _ in trees]
for i, line in enumerate(trees):
curr_height = -1
for j in row_range:
elem = line[j]
if elem > curr_height:
visible[i][j] = True
curr_height = elem
return visible
def vert_visible(trees, top):
visible = [[False] * len(trees[0]) for _ in trees]
col_range = range(len(trees)) if top else range(len(trees) - 1, -1, -1)
for i in range(len(trees[0])):
curr_height = -1
for j in col_range:
elem = trees[j][i]
if elem > curr_height:
visible[j][i] = True
curr_height = elem
return visible
def problem_1():
trees = read_input()
left, right = horiz_visible(trees, True), horiz_visible(trees, False)
top, bottom = vert_visible(trees, True), vert_visible(trees, False)
acc = 0
for a, b, c, d in zip(left, right, top, bottom):
for a1, b1, c1, d1 in zip(a, b, c, d):
acc += 1 if (a1 or b1 or c1 or d1) else 0
return acc
def scenic_score(trees, i, j):
def visual(rng, horizontal):
acc = 0
for l in rng:
acc += 1
if (trees[i][l] >= trees[i][j] and horizontal) or (
trees[l][j] >= trees[i][j] and not horizontal
):
return acc
return acc
lview = visual(range(j - 1, -1, -1), True)
rview = visual(range(j + 1, len(trees[0])), True)
tview = visual(range(i - 1, -1, -1), False)
bview = visual(range(i + 1, len(trees)), False)
return lview * rview * tview * bview
def problem_2():
trees = read_input()
return max(
scenic_score(trees, i, j)
for i, j in itertools.product(range(len(trees)), range(len(trees[0])))
)
if __name__ == "__main__":
print(problem_1())
print(problem_2())