Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,24 @@
import Data.Vector.Unboxed (Vector)
import qualified Data.Vector.Unboxed as V
waterCollected :: Vector Int -> Int
waterCollected =
V.sum . -- Sum of the water depths over each of
V.filter (> 0) . -- the columns that are covered by some water.
(V.zipWith (-) =<< -- Where coverages are differences between:
(V.zipWith min . -- the lower water level in each case of:
V.scanl1 max <*> -- highest wall to left, and
V.scanr1 max)) -- highest wall to right.
main :: IO ()
main =
mapM_
(print . waterCollected . V.fromList)
[ [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]
]

View file

@ -0,0 +1,49 @@
import Data.List (replicate, transpose)
-------------- WATER COLLECTED BETWEEN TOWERS ------------
towerPools :: [Int] -> [(Int, Int)]
towerPools =
zipWith min . scanl1 max <*> scanr1 max
>>= zipWith ((<*>) (,) . (-))
--------------------------- TEST -------------------------
main :: IO ()
main =
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]
]
------------------------- DIAGRAMS -----------------------
display :: [(Int, Int)] -> String
display = (<>) . showTowers <*> (('\n' :) . showLegend)
showTowers :: [(Int, Int)] -> String
showTowers xs =
let upper = maximum (fst <$> xs)
in '\n' :
( unlines
. transpose
. fmap
( \(x, d) ->
concat $
replicate (upper - (x + d)) " "
<> replicate d "x"
<> replicate x ""
)
)
xs
showLegend :: [(Int, Int)] -> String
showLegend =
((<>) . show . fmap fst)
<*> ((" -> " <>) . show . foldr ((+) . snd) 0)