diff --git a/advent-of-code-2024.ipkg b/advent-of-code-2024.ipkg index 03eb6b3..ae79350 100644 --- a/advent-of-code-2024.ipkg +++ b/advent-of-code-2024.ipkg @@ -14,4 +14,5 @@ executable = advent-of-code-2024 main = Main modules = Main, Utils, AllDays, Data.Problem, - Day1.Part1, Day1.Part2 + Day1.Part1, Day1.Part2, + Day2.Part1 diff --git a/src/AllDays.idr b/src/AllDays.idr index c384c4b..27c663c 100644 --- a/src/AllDays.idr +++ b/src/AllDays.idr @@ -2,3 +2,4 @@ module AllDays import public Day1.Part1 import public Day1.Part2 +import public Day2.Part1 diff --git a/src/Day2/Part1.idr b/src/Day2/Part1.idr new file mode 100644 index 0000000..9278dec --- /dev/null +++ b/src/Day2/Part1.idr @@ -0,0 +1,43 @@ +module Day2.Part1 + +import Data.List +import Data.List1 +import Data.String +import Utils + +%default total + +--- TYPES + +public export +Report : Type +Report = List1 Nat + +--- PARSING + +parseLine : String -> Maybe Report +parseLine = traverse parseNat . split (==' ') + +export +parseInput : String -> Maybe (List Report) +parseInput = traverse parseLine . lines + +--- DATA + +differences : Report -> List Integer +differences ns = + let is = map natToInteger ns + in zipWith (-) (tail is) (forget is) + +export +isSafe : Report -> Bool +isSafe rep = + let diff = differences rep + in (all (>0) diff || all (<0) diff) + && all ((<=3) . abs) diff + +--- SOLUTION + +export +solution : String -> Maybe Nat +solution inp = count isSafe <$> parseInput inp diff --git a/src/Main.idr b/src/Main.idr index 9cb5f9c..f9dce6c 100644 --- a/src/Main.idr +++ b/src/Main.idr @@ -16,7 +16,7 @@ import AllDays ||| The latest problem that has been solved. -- NOTE: UPDATE AFTER EACH SOLUTION latest : Problem -latest = Pr 1 Part2 +latest = Pr 2 Part1 solMap : SortedMap Problem (String -> String)