Last active
February 25, 2016 23:54
-
-
Save philipnilsson/8378133 to your computer and use it in GitHub Desktop.
Waterflow python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
def scanl(f,x,it): | |
return functools.reduce(lambda xs, y: xs + [f(xs[-1],y)], it, [x]) | |
def scanl1(f, it): | |
return scanl(f, it[0], it[1:]) | |
def scanr1(f, it): | |
return reversed(scanl1(f, list(reversed(it)))) | |
def waterflow(levels): | |
h_left = scanl1(max, levels) | |
h_right = scanr1(max, levels) | |
waterheighs = [min(left,right) for left,right in zip(h_left, h_right)] | |
return sum([h - l for h,l in zip(waterheighs, levels)]) | |
print(waterflow([5, 5, 1, 2, 3, 4, 7, 7, 6, 0, 1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment