16 lines
481 B
Haskell
16 lines
481 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
|
|
print middle2
|
|
|
|
fuel :: Int -> (Int -> Int) -> [Int] -> Int
|
|
fuel middle f = sum . map (f . abs . subtract middle)
|