Last active
August 29, 2015 14:01
-
-
Save Pranz/45eff77d201ef1e2dc61 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module Raining where | |
waterVolume :: [Int] -> Int | |
waterVolume xs = water 0 0 [] xs | |
where water maxHeight accWater puddles [] = accWater | |
water maxHeight accWater puddles (x:xs) | |
| x >= maxHeight = water x (accWater + sum (map (flip subtract maxHeight) puddles)) [] xs | |
| null puddles = water maxHeight accWater [x] xs | |
| x > head puddles = let (waterFilled, rest) = break (>= x) puddles in water maxHeight (accWater + sum (map (flip subtract x) waterFilled)) ((waterFilled >> return x) ++ rest) xs | |
| otherwise = water maxHeight accWater (x:puddles) xs | |
main :: IO () | |
main = print . waterVolume $ [2,1,3,2,0,0,7,4,5,6,8,4,3,2,1,5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment