From 5ff7dbf234fd768c040acb0ff8e5718d50a862d8 Mon Sep 17 00:00:00 2001 From: Crazazy Date: Sun, 1 Dec 2024 11:58:50 +0100 Subject: finished day 1 --- src/Day1.idr | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/Day1.idr') diff --git a/src/Day1.idr b/src/Day1.idr index 12e9217..9018a95 100644 --- a/src/Day1.idr +++ b/src/Day1.idr @@ -19,12 +19,20 @@ parseStr str = do maybe [] pure (parsePair nums) transposePairs : List (a, b) -> (List a, List b) -transposePairs [] = ([], []) -transposePairs (x :: xs) = ((fst x) :: (fst rest), (snd x) :: (snd rest)) where - rest = transposePairs xs +transposePairs ls = (map fst ls, map snd ls) + +part1 : (List Int, List Int) -> Int +part1 inp = sum $ zipWith (\x => \y => abs (x - y)) (sort $ fst inp) (sort $ snd inp) + +occur : Eq a => Num b => a -> List a -> b +occur x fs = foldr (\y => \r => if x == y then r + 1 else r) 0 fs + +part2 : (List Int, List Int) -> Int +part2 (l1, l2) = sum $ map (\x => x * (occur x l2)) l1 main : IO () main = do estr <- readFile "data/day1.txt" str <- either (die . show) pure estr - putStrLn $ show $ transposePairs $ parseStr str + putStrLn $ show $ part1 $ transposePairs $ parseStr str + putStrLn $ show $ part2 $ transposePairs $ parseStr str -- cgit 1.4.1-2-gfad0