summaryrefslogtreecommitdiff
path: root/2024/1.hs
blob: 3301ceb2c4a2ec38608b035ddc96b95fa25e28bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
import Data.Bifunctor (bimap)
import Data.List (sort)
import qualified Data.Map.Strict as Map

main :: IO ()
main = do
  (a, b) <- bimap sort sort . unzip
          . map (\l -> let [x,y] = words l in (read @Int x, read y))
          . lines <$> getContents
  print $ sum (zipWith (\x y -> abs (x - y)) a b)
  let hist = Map.fromAscListWith (+) (map (,1 :: Int) b)
  print $ sum [n * Map.findWithDefault 0 n hist | n <- a]