aoc2021/code/src/07/Main.hs
thornAvery 926a0099aa day 7
2021-12-07 05:27:49 +00:00

35 lines
716 B
Haskell

module Main where
import Data.List
main :: IO ()
main = do
raw <- getLine
let input = map read $ words $ repCom raw
putStrLn $ "day7a: " ++ (show $ solveA input)
putStrLn $ "day7b: " ++ (show $ solveB input)
repCom :: String -> String
repCom = map f
where f c = if c == ',' then ' ' else c
solveA :: [Int] -> Int
solveA is = minimum $ map f us
where
f x = sum $ map (absdiff x) is
us = nub is
solveB :: [Int] -> Int
solveB is = minimum $ map f [low .. high]
where
high = maximum us
low = minimum us
f x = sum $ map (expdiff x) is
us = nub is
absdiff :: Int -> Int -> Int
absdiff x y = abs (x - y)
expdiff :: Int -> Int -> Int
expdiff x y = sum $ take (absdiff x y) [1..]