From 41de325442a821663f2a5d4ee7ff299af69fcc49 Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Wed, 4 Dec 2024 07:14:20 -0500 Subject: [PATCH] feat: part 3-1 --- advent-of-code-2024.ipkg | 4 +++- src/AllDays.idr | 1 + src/Day3/Part1.idr | 29 +++++++++++++++++++++++++++++ src/Main.idr | 2 +- 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/Day3/Part1.idr diff --git a/advent-of-code-2024.ipkg b/advent-of-code-2024.ipkg index 764448c..91f30e4 100644 --- a/advent-of-code-2024.ipkg +++ b/advent-of-code-2024.ipkg @@ -9,10 +9,12 @@ license = "MIT" sourcedir = "src" langversion >= 0.6.0 +depends = contrib executable = advent-of-code-2024 main = Main modules = Main, Utils, AllDays, Data.Problem, Day1.Part1, Day1.Part2, - Day2.Part1, Day2.Part2 + Day2.Part1, Day2.Part2, + Day3.Part1 diff --git a/src/AllDays.idr b/src/AllDays.idr index e3d32ef..242f8b9 100644 --- a/src/AllDays.idr +++ b/src/AllDays.idr @@ -4,3 +4,4 @@ import public Day1.Part1 import public Day1.Part2 import public Day2.Part1 import public Day2.Part2 +import public Day3.Part1 diff --git a/src/Day3/Part1.idr b/src/Day3/Part1.idr new file mode 100644 index 0000000..ca132be --- /dev/null +++ b/src/Day3/Part1.idr @@ -0,0 +1,29 @@ +module Day3.Part1 + +import Data.Either +import Data.Maybe +import Data.String +import Data.String.Parser + +--- PARSING + +export +scan : Monad m => ParseT m a -> ParseT m a +scan p = many (requireFailure p *> skip (satisfy (const True))) *> p + +export +mul : Parser Integer +mul = do + skip $ string "mul(" + x <- integer + skip $ string "," + y <- integer + skip $ string ")" + pure $ x * y + +--- SOLUTION + +export +solution : String -> Integer +solution = fromMaybe 0 . map (sum . fst) . eitherToMaybe . + parse (many $ scan mul) diff --git a/src/Main.idr b/src/Main.idr index 0ccb97d..3d43250 100644 --- a/src/Main.idr +++ b/src/Main.idr @@ -14,7 +14,7 @@ import AllDays ||| The latest problem that has been solved. -- NOTE: UPDATE AFTER EACH SOLUTION latest : Problem -latest = Pr 2 Part2 +latest = Pr 3 Part1 solMap : SortedMap Problem (String -> String)