diff options
-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 |