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