-
Notifications
You must be signed in to change notification settings - Fork 8
/
waterFallStream.py
34 lines (34 loc) · 1.2 KB
/
waterFallStream.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
def waterfallStreams(array, source):
rowAbove = array[0][:]
rowAbove[source] = -1
for row in range(1, len(array)):
currentRow = array[row][:]
for idx in range(len(rowAbove)):
valueAbove = rowAbove[idx]
hasWaterAbove = valueAbove < 0
hasBlock = currentRow[idx] == 1
if not hasWaterAbove:
continue
if not hasBlock:
currentRow[idx] += valueAbove
continue
splitWater = valueAbove/2
rightIdx = idx
while rightIdx+1 < len(rowAbove):
rightIdx += 1
if rowAbove[rightIdx] == 1:
break
if currentRow[rightIdx] != 1:
currentRow[rightIdx] += splitWater
break
leftIdx = idx
while leftIdx-1 >= 0:
leftIdx -= 1
if rowAbove[leftIdx] == 1:
break
if currentRow[leftIdx] != 1:
currentRow[leftIdx] += splitWater
break
rowAbove = currentRow
finalPercentages = list(map(lambda num: num*-100, rowAbove))
return finalPercentages