{-# 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 inp <- [format|2021 7 %u&,%n|]

    let 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)
    print (sum [abs (x - median) | x <- inp])

    let 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
    print (minimum [sum [triangle (abs (x-a)) | x <- inp] | a <- [mean, mean+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