{-# Language QuasiQuotes #-}
module Main (main) where
import Advent (format)
import Data.List (sort)
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]])
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