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:[]) = if x < highest
  9. then (highest,buildings,water)
  10. else (highest,[],water + sum(map ((min highest x)-) buildings))
  11. waterInCity' (highest,buildings,water) (x:xs) = if x >= highest
  12. then waterInCity' (x,[],water + sum(map (highest-) buildings)) xs
  13. else waterInCity' (highest,x:buildings,water) xs
  14.  
  15. main = do
  16. print $ waterInCity [] == 0
  17. print $ waterInCity [1] == 0
  18. print $ waterInCity [1,1] == 0
  19. print $ waterInCity [1,0] == 0
  20. print $ waterInCity [0,1] == 0
  21. print $ waterInCity [1,0,1] == 1
  22. print $ waterInCity [2,0,1,3] == 3
  23. print $ waterInCity [6,3,4,7,4,3,7,7] == 3 + 2 + 3 + 4
  24. print $ waterInCity [2,5,1,2,3,4,7,7,6] == 10
  25. print $ waterInCity [1,5,4,3,2,1]
  26.  
Success #stdin #stdout 0s 6224KB
stdin
Standard input is empty
stdout
True
True
True
True
True
True
True
True
True
0