2024 day 1: Haskell solutions
This commit is contained in:
parent
7a0450ec6c
commit
fd4501f3c4
11
2024/1/Main1.hs
Normal file
11
2024/1/Main1.hs
Normal 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
14
2024/1/Main2.hs
Normal 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 " "
|
Loading…
Reference in a new issue