From 9dfdb20a55fe3ef7763aaee1be7de1b2179c46de Mon Sep 17 00:00:00 2001 From: Crazazy Date: Tue, 3 Dec 2024 12:05:53 +0100 Subject: Finish day3 easiest day so far --- src/Day3.idr | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/Day3.idr (limited to 'src/Day3.idr') diff --git a/src/Day3.idr b/src/Day3.idr new file mode 100644 index 0000000..5f14827 --- /dev/null +++ b/src/Day3.idr @@ -0,0 +1,40 @@ +module Day3 + +import Data.String.Parser +import Data.String + +mul : Parser Integer +mul = (*) <$ string "mul(" <*> integer <* string "," <*> integer <* string ")" + + +partParser : Parser a -> String -> List a +partParser p str = case str of + "" => [] + _ => case parse p str of + Left _ => partParser p (strSubstr 1 (strLength str) str) + Right (num, bla) => num :: (partParser p (strSubstr bla (strLength str) str)) + +part1 : String -> List Integer +part1 = partParser mul + +export sol1 : String -> String +sol1 = show . sum . part1 + +dont : Parser (Either Bool Integer) +dont = Left <$> ((True <$ string "do()") <|> (False <$ string "don't()")) + +mul2 : Parser (Either Bool Integer) +mul2 = (Right <$> mul) <|> dont + +part2 : String -> List (Either Bool Integer) +part2 = partParser mul2 + +toggleSum : Bool -> List (Either Bool Integer) -> Integer +toggleSum x [] = 0 +toggleSum x ((Left y) :: xs) = toggleSum y xs +toggleSum False ((Right y) :: xs) = toggleSum False xs +toggleSum True ((Right y) :: xs) = y + (toggleSum True xs) + +export +sol2 : String -> String +sol2 = show . toggleSum True . part2 -- cgit 1.4.1-2-gfad0