AoC/2021/day07/day07.hs
2021-12-16 21:58:39 +01:00

15 lines
465 B
Haskell

import Data.List.Split
import Data.List
main :: IO ()
main = do
input <- splitOn "," <$> readFile "day07/input"
let crabs = sort $ map read input :: [Int]
let middle1 = crabs !! (length crabs `div` 2)
let middle2 = (sum crabs) `div` genericLength crabs
print $ fuel middle1 id crabs
print $ fuel middle2 (\x->last $ take (x+1) $ scanl1 (+) [0..]) crabs
fuel :: Int -> (Int -> Int) -> [Int] -> Int
fuel middle f = sum . map (f . abs . subtract middle)