aoc/app/1.hs

31 lines
753 B
Haskell
Raw Normal View History

2024-12-01 09:56:24 +00:00
{-# 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
) <$>) .
2024-12-01 14:58:01 +00:00
(filter (/= mempty) . splitWhen isSpace <$>) .
2024-12-01 09:56:24 +00:00
lines
solve1 :: ([Int], [Int]) -> Int
solve1 = sum . uncurry (zipWith ((abs .) . (-)) `on` sort)
2024-12-01 10:00:28 +00:00
solve2 :: ([Int], [Int]) -> Int
2024-12-01 14:58:01 +00:00
solve2 (as,bs) = foldr (\a -> (a * length (filter (==a) bs) +)) 0 as
2024-12-01 10:00:28 +00:00
2024-12-01 09:56:24 +00:00
main :: IO ()
2024-12-01 10:00:28 +00:00
main = readFile "inputs/1" <&> parse >>= \i ->
print (solve1 i) >>
print (solve2 i)