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)
