summary refs log tree commit diff
path: root/src/Day1.idr
diff options
context:
space:
mode:
authorCrazazy2024-12-01 11:58:50 +0100
committerErik Oosting2024-12-01 12:01:06 +0100
commit5ff7dbf234fd768c040acb0ff8e5718d50a862d8 (patch)
treea829a48a5e7f1234d3fe3c366427b2778050ffa1 /src/Day1.idr
parent9f4a26f9188f74aa6869583ab991b4b8eccbb0b7 (diff)
finished day 1
Diffstat (limited to 'src/Day1.idr')
-rw-r--r--src/Day1.idr16
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