Skip to content

Instantly share code, notes, and snippets.

@opqdonut
Created December 9, 2021 15:25
Show Gist options
  • Save opqdonut/0ab3022c72f39717c05f0645650f6e2c to your computer and use it in GitHub Desktop.
Save opqdonut/0ab3022c72f39717c05f0645650f6e2c to your computer and use it in GitHub Desktop.
Advent of Code 2021 Day 7
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