-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.py
71 lines (60 loc) · 2.02 KB
/
8.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
# --- Day 8: Treetop Tree House ---
# I have to find tall trees that are visible from outside the grid
# Open and read file
f = open("8_input.txt", "r")
lines = f.readlines()
f.close()
# Create a 2D list of tree heights
num_rows = len(lines)
num_cols = len(lines[0].strip())
trees = [[0 for _ in range(num_rows)] for _ in range(num_cols)]
for index, line in enumerate(lines):
trees[index] = list(line.strip())
print(trees)
# Count hidden trees and calculate tree scores
hidden_trees = 0
highest_score = 0
for row_index, row in enumerate(trees):
for col_index, height in enumerate(row):
height = int(height)
# Check left
left = False
left_dist = 0
for index in reversed(range(0, row_index)):
left_dist += 1
if int(trees[index][col_index]) >= height:
left = True
break
# Check right
right = False
right_dist = 0
for index in range(row_index + 1, len(row)):
right_dist += 1
if int(trees[index][col_index]) >= height:
right = True
break
# Check above
above = False
above_dist = 0
for index in reversed(range(0, col_index)):
above_dist += 1
if int(trees[row_index][index]) >= height:
above = True
break
# Check below
below = False
below_dist = 0
for index in range(col_index + 1, len(row)):
below_dist += 1
if int(trees[row_index][index]) >= height:
below = True
break
if left and right and above and below:
hidden_trees += 1
score = left_dist * right_dist * above_dist * below_dist
if score > highest_score:
highest_score = score
total_trees = num_rows * num_cols
print(total_trees)
print("Visible trees:", total_trees - hidden_trees)
print("Top score:", highest_score)