diff --git a/Aoc2024/Day01/Examples.lean b/Aoc2024/Day01/Examples.lean new file mode 100644 index 0000000..293a5b4 --- /dev/null +++ b/Aoc2024/Day01/Examples.lean @@ -0,0 +1,7 @@ +def exampleInput := +"3 4 +4 3 +2 5 +1 3 +3 9 +3 3" diff --git a/Aoc2024/Day01/Parser.lean b/Aoc2024/Day01/Parser.lean index 3076fae..fee6e8f 100644 --- a/Aoc2024/Day01/Parser.lean +++ b/Aoc2024/Day01/Parser.lean @@ -12,3 +12,5 @@ private def lineParser : Parser (Int × Int) := do private def inputParser : Parser (List (Int × Int)) := sepBy lineParser (String.skipChar '\n') def parseLines : String -> Except String (List (Int × Int)) := inputParser.run + +#guard parseLines "23 24\n14 13" == Except.ok [(23, 24), (14, 13)] diff --git a/Aoc2024/Day01/Solve.lean b/Aoc2024/Day01/Solve.lean index a82920b..7f48bdf 100644 --- a/Aoc2024/Day01/Solve.lean +++ b/Aoc2024/Day01/Solve.lean @@ -1,19 +1,12 @@ import Aoc2024.Day01.Parser import Aoc2024.Utils +import Aoc2024.Day01.Examples namespace Aoc2024.Day01 -private def exampleInput := -"3 4 -4 3 -2 5 -1 3 -3 9 -3 3" - private def solvePart1 (pairs : List (Int × Int)) : Int := let (firsts, seconds) := pairs.unzip let distance (a b : Int) : Int := (a - b).natAbs - (firsts.mergeSort.zipWith distance seconds.mergeSort) |> sumList + firsts.mergeSort.zipWith distance seconds.mergeSort |> sumList def parseAndSolvePart1 : String -> Except String Int := .map solvePart1 ∘ parseLines