-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeHouse.py
More file actions
47 lines (46 loc) · 1.59 KB
/
Copy pathTreeHouse.py
File metadata and controls
47 lines (46 loc) · 1.59 KB
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
def findNumVisibleTrees(file_buffer):
grid = file_buffer.splitlines()
ans = 0
for x, row in enumerate(grid):
for y, tree in enumerate(row):
if all(grid[x][j] < tree for j in range(0,y)): # ->
ans += 1
elif all(grid[x][j] < tree for j in range(y+1,len(row))): # <-
ans += 1
elif all(grid[i][y] < tree for i in range(0,x)): # \/
ans += 1
elif all(grid[i][y] < tree for i in range(x+1,len(grid))): # /\
ans += 1
return ans
def findBestScenicTree(file_buffer):
grid = file_buffer.splitlines()
scenicValues = []
for x, row in enumerate(grid):
for y, tree in enumerate(row):
up, down, left, right = [0, 0, 0, 0]
for t in range(x-1,-1,-1):
up += 1
if grid[t][y] >= tree:
break
if up == 0:
continue
for t in range(x+1, len(grid)):
down += 1
if grid[t][y] >= tree:
break
if down == 0:
continue
for u in range(y-1,-1,-1):
left += 1
if grid[x][u] >= tree:
break
if left == 0:
continue
for u in range(y+1,len(row)):
right += 1
if grid[x][u] >= tree:
break
if right == 0:
continue
scenicValues.append(up*down*left*right)
return max(scenicValues)