2024 day 1: Haskell solution

This commit is contained in:
eriedaberrie 2024-12-02 15:12:48 -08:00
parent 7a0450ec6c
commit a7dc708a65
3 changed files with 26 additions and 0 deletions

11
2024/1/Main1.hs Normal file
View file

@ -0,0 +1,11 @@
import Data.List (sort, transpose)
import Data.List.Split (splitOn)
solve :: [[Int]] -> Int
solve = sum . map abs . (\[x, xx] -> zipWith (-) x xx) . map sort
main :: IO ()
main =
print . solve . transpose . map parseLine . lines =<< readFile "data.txt"
where
parseLine = map read . filter (not . null) . splitOn " "

14
2024/1/Main2.hs Normal file
View file

@ -0,0 +1,14 @@
import qualified Data.IntMultiSet as IMultiSet
import Data.List (sort, transpose)
import Data.List.Split (splitOn)
solve :: [[Int]] -> Int
solve [l, r] = sum $ map (\x -> x * IMultiSet.occur x rs) l
where
rs = IMultiSet.fromList r
main :: IO ()
main =
print . solve . transpose . map parseLine . lines =<< readFile "data.txt"
where
parseLine = map read . filter (not . null) . splitOn " "

View file

@ -33,6 +33,7 @@
with p; [ with p; [
containers containers
extra extra
multiset
regex-compat regex-compat
split split
text text