07 speedup

This commit is contained in:
thornAvery 2021-12-14 04:29:43 +00:00
parent 9bbd9d8088
commit de4c80d259

View file

@ -5,23 +5,19 @@ import Data.List
main :: IO ()
main = do
raw <- getLine
let input = map read $ words $ repCom raw
let input = sort $ map read $ words $ repCom raw
putStrLn $ "day7a: " ++ (show $ solveA input)
putStrLn $ "day7b: " ++ (show $ solveB input)
solveA :: [Int] -> Int
solveA is = minimum $ map f us
solveA is = sum $ map (absdiff mid) is
where
f x = sum $ map (absdiff x) is
us = nub is
mid = is !! (length is `div` 2)
solveB :: [Int] -> Int
solveB is = minimum $ map f [low .. high]
solveB is = sum $ map (expdiff mid) is
where
high = maximum us
low = minimum us
f x = sum $ map (expdiff x) is
us = nub is
mid = (sum is) `div` (length is)
absdiff :: Int -> Int -> Int
absdiff x y = abs (x - y)