Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mdr committed Dec 1, 2024
1 parent 879fa91 commit 861fa27
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
7 changes: 7 additions & 0 deletions Aoc2024/Day01/Examples.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def exampleInput :=
"3 4
4 3
2 5
1 3
3 9
3 3"
2 changes: 2 additions & 0 deletions Aoc2024/Day01/Parser.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
11 changes: 2 additions & 9 deletions Aoc2024/Day01/Solve.lean
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 861fa27

Please sign in to comment.