diff options
author | Crazazy | 2024-12-01 11:58:50 +0100 |
---|---|---|
committer | Erik Oosting | 2024-12-01 12:01:06 +0100 |
commit | 5ff7dbf234fd768c040acb0ff8e5718d50a862d8 (patch) | |
tree | a829a48a5e7f1234d3fe3c366427b2778050ffa1 /src/Day1.idr | |
parent | 9f4a26f9188f74aa6869583ab991b4b8eccbb0b7 (diff) |
finished day 1
Diffstat (limited to 'src/Day1.idr')
-rw-r--r-- | src/Day1.idr | 16 |
1 files changed, 12 insertions, 4 deletions
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 |