From fd4501f3c4948121a86a42883fe4cc2269116003 Mon Sep 17 00:00:00 2001 From: eriedaberrie Date: Mon, 2 Dec 2024 15:12:48 -0800 Subject: [PATCH] 2024 day 1: Haskell solutions --- 2024/1/Main1.hs | 11 +++++++++++ 2024/1/Main2.hs | 14 ++++++++++++++ flake.nix | 1 + 3 files changed, 26 insertions(+) create mode 100644 2024/1/Main1.hs create mode 100644 2024/1/Main2.hs diff --git a/2024/1/Main1.hs b/2024/1/Main1.hs new file mode 100644 index 0000000..8932eb8 --- /dev/null +++ b/2024/1/Main1.hs @@ -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 " " diff --git a/2024/1/Main2.hs b/2024/1/Main2.hs new file mode 100644 index 0000000..192bb73 --- /dev/null +++ b/2024/1/Main2.hs @@ -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 " " diff --git a/flake.nix b/flake.nix index 9723b50..4c2e22a 100644 --- a/flake.nix +++ b/flake.nix @@ -33,6 +33,7 @@ with p; [ containers extra + multiset regex-compat split text