aoc/app/1.hs
2024-12-01 11:00:28 +01:00

32 lines
763 B
Haskell

{-# LANGUAGE LambdaCase #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Redundant <&>" #-}
module Main where
import Data.Functor
import Data.List.Split
import Data.Char
import Data.Function
import Data.List
parse :: String -> ([Int], [Int])
parse = unzip .
((\case
[a,b] -> (read a, read b)
e -> error $ "Parsing failed on: " ++ show e
) <$>) .
(filter (/= mempty) <$>) .
(splitWhen isSpace <$>) .
lines
solve1 :: ([Int], [Int]) -> Int
solve1 = sum . uncurry (zipWith ((abs .) . (-)) `on` sort)
solve2 :: ([Int], [Int]) -> Int
solve2 (as,bs) = foldr (\a b -> b + a * length (filter (==a) bs)) 0 as
main :: IO ()
main = readFile "inputs/1" <&> parse >>= \i ->
print (solve1 i) >>
print (solve2 i)