{-# Language QuasiQuotes #-}
{-|
Module      : Main
Description : Day 7 solution
Copyright   : (c) Eric Mertens, 2021
License     : ISC
Maintainer  : emertens@gmail.com

<https://adventofcode.com/2021/day/7>

Find the minimum fuel cost to move the submarines to a common point.

-}
module Main (main) where

import Advent (format)
import Data.List (sort)

-- | >>> :main
-- 336721
-- 91638945
main :: IO ()
IO ()
main =
 do [Int]
inp <- [format|2021 7 %u&,%n|]

    let median :: Int
median = [Int] -> [Int]
forall a. Ord a => [a] -> [a]
sort [Int]
inp [Int] -> Int -> Int
forall a. HasCallStack => [a] -> Int -> a
!! ([Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
inp Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2)
    Int -> IO ()
forall a. Show a => a -> IO ()
print ([Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Int -> Int
forall a. Num a => a -> a
abs (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
median) | Int
x <- [Int]
inp])

    let mean :: Int
mean = [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Int]
inp Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` [Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
inp
    Int -> IO ()
forall a. Show a => a -> IO ()
print ([Int] -> Int
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [[Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Int -> Int
triangle (Int -> Int
forall a. Num a => a -> a
abs (Int
xInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
a)) | Int
x <- [Int]
inp] | Int
a <- [Int
mean, Int
meanInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1]])

-- | Sum of numbers from 1 to @n@
triangle :: Int -> Int
triangle :: Int -> Int
triangle Int
n = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2