Created
December 9, 2021 15:25
-
-
Save opqdonut/0ab3022c72f39717c05f0645650f6e2c to your computer and use it in GitHub Desktop.
Advent of Code 2021 Day 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Day7 where | |
import Data.List | |
parse :: String -> [Int] | |
parse s = read ("["++s++"]") | |
input = parse <$> readFile "input.7" | |
example = [16,1,2,0,4,2,7,1,2,14] | |
part1 i = | |
let s = sort i | |
median = s !! (length s `div` 2) | |
in sum [abs (x-median) | x <- s] | |
cost diff = d*(d+1)`div`2 | |
where d = abs diff | |
-- the average should be the best mean square error estimator of a dataset | |
part2 i = | |
-- why is floor right and not round? | |
let avg = floor (fromIntegral (sum i) / fromIntegral (length i)) | |
in sum [cost (x-avg) | x <- i] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment