aoc2021-day07b Haskell/GHC program
source code
-- author: f-a
import Data.List as L
main = main2
main1 = sol1 <$> input >>= print
main2 = sol2 <$> input >>= print
input :: IO [Int]
input = read . ('[':) . (++"]") <$> getContents
cost :: (Int -> Int) -> [Int] -> Int
cost d ps = sum $ map d ps
median :: [Int] -> Int
median ps = last . take (quot (length ps) 2) $ L.sort ps
mean :: [Int] -> Int
mean ps = quot (sum ps) (length ps)
-- sol1 <$> input 344138
-- sol2 <$> input 94862124
sol1, sol2 :: [Int] -> Int
sol1 ps = cost (abs . subtract (median ps)) ps
sol2 ps = cost (\p -> sum [1..(abs $ (mean ps - p))]) ps
notes, command-line, and program output
NOTES:
Linux
Sun, 23 Jan 2022 14:45:52 GMT
MAKE:
ghc -O2 aoc2021_day07b.hs-1.hs -o app_hs
[1 of 1] Compiling Main ( aoc2021_day07b.hs-1.hs, aoc2021_day07b.hs-1.o )
Linking app_hs ...
4.86s to complete and log all make actions
COMMAND LINE:
./app_hs 0 < aoc2021_day07b-input1000000.txt
PROGRAM OUTPUT:
97051441112217645