-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.py
63 lines (49 loc) · 1.81 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
def part1():
return get_visible_trees(parse_trees())
def part2():
return get_top_scenic_score(parse_trees())
def parse_trees():
output = open("data-day8.txt", "r").read()
lines = output.split('\n')
trees = [[] for _ in range(len(lines))]
for row_index, row in enumerate(lines):
for tree in row:
trees[row_index].append(tree)
return trees
def get_visible_trees(trees):
visible_trees = 0
for r_i, row in enumerate(trees):
for c_i, tree in enumerate(row):
if r_i == 0 or c_i == 0 or r_i == len(trees) - 1 or c_i == len(row) - 1:
visible_trees += 1
continue
left, right, top, bottom = directions(c_i, r_i, trees)
visible = max(left) < tree or max(right) < tree or max(top) < tree or max(bottom) < tree
if visible:
visible_trees += 1
return visible_trees
def get_top_scenic_score(trees):
max_score = 0
for r_i, row in enumerate(trees):
for c_i, tree in enumerate(row):
left, right, top, bottom = directions(c_i, r_i, trees)
score = fov_length(left, tree) * fov_length(right, tree) * fov_length(top, tree) * fov_length(bottom, tree)
if score > max_score:
max_score = score
return max_score
def directions(column_index, row_index, trees):
row = trees[row_index]
left_fov = list(reversed(row[:column_index]))
right_fov = row[(column_index + 1):]
column = [r[column_index] for r in trees]
top_fov = list(reversed(column[:row_index]))
bottom_fov = column[(row_index + 1):]
return bottom_fov, left_fov, right_fov, top_fov
def fov_length(fov, tree):
score = 0
if fov:
for t in fov:
score += 1
if t >= tree:
break
return score