Skip to content

Commit db33b67

Browse files
committed
day 8 part 2
1 parent 09c7870 commit db33b67

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

day8/day8.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
11
# read input
2-
32
with open('input.txt', 'r') as file:
43
data = file.read().strip().split('\n')
54

6-
# part 1
75
totalVisibleTrees = 0
86
rows = len(data)
97
columns = len(data[0])
108
edges = rows * 2 + columns * 2 - 4
119
totalVisibleTrees = edges
10+
totalScenicScore = []
1211

1312
for row in range(1, rows - 1):
1413
for col in range(1, columns - 1):
1514
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)]
1815
up = [data[row - i][col] for i in range(1, row + 1)]
1916
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)]
2019

20+
# part 1
2121
if (max(left) < tree or max(right) < tree or max(up) < tree or max(down) < tree):
2222
totalVisibleTrees += 1
2323

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
2438
print('Total number of visible trees: ', totalVisibleTrees)
39+
40+
# part 2
41+
highestScenicScore = max(totalScenicScore)
42+
print('Highest scenic score: ', highestScenicScore)

0 commit comments

Comments
 (0)