1
1
# read input
2
-
3
2
with open ('input.txt' , 'r' ) as file :
4
3
data = file .read ().strip ().split ('\n ' )
5
4
6
- # part 1
7
5
totalVisibleTrees = 0
8
6
rows = len (data )
9
7
columns = len (data [0 ])
10
8
edges = rows * 2 + columns * 2 - 4
11
9
totalVisibleTrees = edges
10
+ totalScenicScore = []
12
11
13
12
for row in range (1 , rows - 1 ):
14
13
for col in range (1 , columns - 1 ):
15
14
tree = data [row ][col ]
16
- left = [data [row ][col - i ] for i in range (1 , col + 1 )]
17
- right = [data [row ][col + i ] for i in range (1 , columns - col )]
18
15
up = [data [row - i ][col ] for i in range (1 , row + 1 )]
19
16
down = [data [row + i ][col ] for i in range (1 , rows - row )]
17
+ left = [data [row ][col - i ] for i in range (1 , col + 1 )]
18
+ right = [data [row ][col + i ] for i in range (1 , columns - col )]
20
19
20
+ # part 1
21
21
if (max (left ) < tree or max (right ) < tree or max (up ) < tree or max (down ) < tree ):
22
22
totalVisibleTrees += 1
23
23
24
+ # part 2
25
+ scenicScore = 1
26
+ for list in (up , down , left , right ):
27
+ count = 0
28
+ for i in range (len (list )):
29
+ if (list [i ] < tree ):
30
+ count = count + 1
31
+ else :
32
+ count = count + 1
33
+ break
34
+ scenicScore = scenicScore * count
35
+ totalScenicScore .append (scenicScore )
36
+
37
+ # part 1
24
38
print ('Total number of visible trees: ' , totalVisibleTrees )
39
+
40
+ # part 2
41
+ highestScenicScore = max (totalScenicScore )
42
+ print ('Highest scenic score: ' , highestScenicScore )
0 commit comments