thd (_,_,x) = x waterInCity :: [Int] -> Int waterInCity = thd . waterInCity' (0,[],0) waterInCity' :: (Int,[Int],Int) -> [Int] -> (Int,[Int],Int) waterInCity' t [] = t waterInCity' (highest,buildings,water) (x:[]) = if x < highest then (highest,buildings,water) else (highest,[],water + sum(map ((min highest x)-) buildings)) waterInCity' (highest,buildings,water) (x:xs) = if x >= highest then waterInCity' (x,[],water + sum(map (highest-) buildings)) xs else waterInCity' (highest,x:buildings,water) xs main = do print $ waterInCity [] == 0 print $ waterInCity [1] == 0 print $ waterInCity [1,1] == 0 print $ waterInCity [1,0] == 0 print $ waterInCity [0,1] == 0 print $ waterInCity [1,0,1] == 1 print $ waterInCity [2,0,1,3] == 3 print $ waterInCity [6,3,4,7,4,3,7,7] == 3 + 2 + 3 + 4 print $ waterInCity [2,5,1,2,3,4,7,7,6] == 10 print $ waterInCity [1,5,4,3,2,1] print $ waterInCity [0,4,0,1,0,2,0,0,0]