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