-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.py
104 lines (85 loc) · 2.76 KB
/
08.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
def step1(grid):
visible = 0
for row in range(len(grid)):
for col in range(len(grid[0])):
treeheight = grid[row][col]
# outer edges
if row == 0 or row == len(grid) - 1 or col == 0 or col == len(grid[0]) - 1:
visible += 1
continue
# Left view
for n in range(0, col):
if grid[row][n] >= treeheight:
break
else:
visible += 1
continue
# Right view
for n in range(col + 1, len(grid[0])):
if grid[row][n] >= treeheight:
break
else:
visible += 1
continue
# Top view
for n in range(0, row):
if grid[n][col] >= treeheight:
break
else:
visible += 1
continue
# Bottom view
for n in range(row + 1, len(grid)):
if grid[n][col] >= treeheight:
break
else:
visible += 1
continue
print(f"Step 1: {visible}")
def step2(grid):
best_treescore = 0
for row in range(len(grid)):
for col in range(len(grid[0])):
treeheight = grid[row][col]
# outer edges
if row == 0 or row == len(grid) - 1 or col == 0 or col == len(grid[0]) - 1:
continue
score = 1
# Right view
visible_count = 0
for n in range(col + 1, len(grid[0])):
visible_count += 1
if grid[row][n] >= treeheight:
break
score *= visible_count
# Left view
visible_count = 0
for n in reversed(range(0, col)):
visible_count += 1
if grid[row][n] >= treeheight:
break
score *= visible_count
# Top view
visible_count = 0
for n in reversed(range(0, row)):
visible_count += 1
if grid[n][col] >= treeheight:
break
score *= visible_count
# Bottom view
visible_count = 0
for n in range(row + 1, len(grid)):
visible_count += 1
if grid[n][col] >= treeheight:
break
score *= visible_count
if score > best_treescore:
best_treescore = score
print(f"Step 2: {best_treescore}")
def main():
with open('./data/08.dat', 'r') as f:
data = [[int(height) for height in str.strip(l)] for l in f.readlines()]
step1(data)
step2(data)
if __name__=="__main__":
main()