fork(1) download
  1. thd (_,_,x) = x
  2.  
  3. waterInCity :: [Int] -> Int
  4. waterInCity = thd . waterInCity' (0,[],0)
  5.  
  6. waterInCity' :: (Int,[Int],Int) -> [Int] -> (Int,[Int],Int)
  7. waterInCity' t [] = t
  8. waterInCity' (highest,buildings,water) (x:[]) = (highest,[],water + sum(map ((min highest x)-) buildings))
  9. waterInCity' (highest,buildings,water) (x:xs) = if x >= highest
  10. then waterInCity' (x,[],water + sum(map (highest-) buildings)) xs
  11. else waterInCity' (highest,x:buildings,water) xs
  12.  
  13. main = do
  14. print $ waterInCity [] == 0
  15. print $ waterInCity [1] == 0
  16. print $ waterInCity [1,1] == 0
  17. print $ waterInCity [1,0] == 0
  18. print $ waterInCity [0,1] == 0
  19. print $ waterInCity [1,0,1] == 1
  20. print $ waterInCity [2,0,1,3] == 3
  21. print $ waterInCity [6,3,4,7,4,3,7,7] == 3 + 2 + 3 + 4
  22. print $ waterInCity [2,5,1,2,3,4,7,7,6] == 10
  23. print $ waterInCity [1,5,4,3,2,1]
  24.  
Success #stdin #stdout 0s 6268KB
stdin
Standard input is empty
stdout
True
True
True
True
True
True
True
True
True
-6