Skip to content

Instantly share code, notes, and snippets.

@Pranz Pranz/raining
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.