-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8_trees.py
160 lines (129 loc) · 4.44 KB
/
day8_trees.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
import sys
def parse_file(filename):
data = None
with open(filename) as f:
data = f.read().splitlines()
return data
def solve_part_one(data):
total_visible = (len(data) + (len(data[0]) - 2)) * 2
for i, row in enumerate(data):
# skip first and last row
if i == 0 or i == len(data) - 1:
continue
for j, tree in enumerate(row):
# skip left- and right-most columns
if j == 0 or j == len(data) - 1:
continue
i_cpy = i - 1
j_cpy = j
# check top
invisible = False
while i_cpy > -1:
if data[i_cpy][j_cpy] >= tree:
invisible = True
break
else:
i_cpy -= 1
if not invisible:
total_visible += 1
continue
i_cpy = i + 1
j_cpy = j
# check bottom
invisible = False
while i_cpy < len(data) :
if data[i_cpy][j_cpy] >= tree:
invisible = True
break
else:
i_cpy += 1
if not invisible:
total_visible += 1
continue
i_cpy = i
j_cpy = j - 1
# check left
invisible = False
while j_cpy > -1:
if data[i_cpy][j_cpy] >= tree:
invisible = True
break
else:
j_cpy -= 1
if not invisible:
total_visible += 1
continue
i_cpy = i
j_cpy = j + 1
# check right
invisible = False
while j_cpy < len(data) :
if data[i_cpy][j_cpy] >= tree:
invisible = True
break
else:
j_cpy += 1
if not invisible:
total_visible += 1
continue
return total_visible
def solve_part_two(data):
max_scenic = float('-inf')
for i, row in enumerate(data):
if i == 0 or i == len(data) - 1:
continue
for j, tree in enumerate(row):
if j == 0 or j == len(data) - 1:
continue
current_scenic_u = 0
current_scenic_r = 0
current_scenic_d = 0
current_scenic_l = 0
i_cpy = i - 1
j_cpy = j
# check top
while i_cpy > -1:
if data[i_cpy][j_cpy] >= tree:
current_scenic_u += 1
break
else:
i_cpy -= 1
current_scenic_u += 1
i_cpy = i + 1
j_cpy = j
# check bottom
while i_cpy < len(data) :
if data[i_cpy][j_cpy] >= tree:
current_scenic_d += 1
break
else:
i_cpy += 1
current_scenic_d += 1
i_cpy = i
j_cpy = j - 1
# check left
while j_cpy > -1:
if data[i_cpy][j_cpy] >= tree:
current_scenic_l += 1
break
else:
j_cpy -= 1
current_scenic_l += 1
i_cpy = i
j_cpy = j + 1
# check right
while j_cpy < len(data) :
if data[i_cpy][j_cpy] >= tree:
current_scenic_r += 1
break
else:
j_cpy += 1
current_scenic_r += 1
max_scenic = max(max_scenic, current_scenic_r * current_scenic_l * current_scenic_d * current_scenic_u)
return max_scenic
def main(filename):
data = parse_file(filename)
print(solve_part_one(data))
print(solve_part_two(data))
if __name__ == "__main__":
main(sys.argv[1])