Water collected between towers: Difference between revisions

m
(Added 11l)
Line 1,331:
towerPools :: [Int] -> [(Int, Int)]
towerPools =
zipWith min . scanl1 max <*> scanr1 max >>= zipWith ((<*>) (,) . (-))
>>= zipWith ((<*>) (,) . (-))
 
main :: IO ()
Line 1,337 ⟶ 1,338:
mapM_
(putStrLn . display . towerPools)
[ [1, 5, 3, 7, 2],
, [5, 3, 7, 2, 6, 4, 5, 9, 1, 2],
, [2, 6, 3, 5, 2, 8, 1, 4, 2, 2, 5, 3, 5, 7, 4, 1],
, [5, 5, 5, 5],
, [5, 6, 7, 8],
, [8, 7, 7, 6],
, [6, 7, 10, 7, 6]
]
 
display :: [(Int, Int)] -> String
display = (++<>) . showTowers <*> (('\n' :) . showLegend)
 
showTowers :: [(Int, Int)] -> String
showTowers xs =
let upper = maximum (fst <$> xs)
in '\n' :
(unlines .unlines
transpose . transpose
. fmap
( \(x, d) ->
concat $
replicate (upper - (x + d)) " " ++
replicate d "x" ++ <> replicate xd "x"))
<> replicate x "█"
xs
)
)
xs
 
showLegend :: [(Int, Int)] -> String
showLegend =
((++<>) . show . fmap fst) <*> ((" -> " ++) . show . foldr ((+) . snd) 0)</lang>
<*> ((" -> " <>) . show . foldr ((+) . snd) 0)</lang>
{{Out}}
<pre>
9,655

edits