This commit is contained in:
2024-12-01 21:14:25 +01:00
commit 2162be2dde
3 changed files with 59 additions and 0 deletions

21
01/silver.hs Normal file
View File

@@ -0,0 +1,21 @@
import Data.List -- sort
mapTuple :: (a -> b) -> (a, a) -> (b, b)
mapTuple f (a1, a2) = (f a1, f a2)
-- helper function for split
splitH :: [a] -> [a] -> [a] -> ([a], [a])
splitH ls rs (x:xs) = splitH rs (ls++[x]) xs
splitH ls rs _ = (ls, rs)
-- split: split list into two by alternating between left and right
split :: [a] -> ([a], [a])
split = splitH [] []
-- calculates the sum of the absolute distance between two lists
dist :: Int -> ([Int], [Int]) -> Int
dist acc (l:ls, r:rs) = dist (acc + abs (r - l)) (ls, rs)
dist acc _ = acc
main = interact $ show . dist 0 . mapTuple sort . split . map (read :: String -> Int) . words