Skip to content

Commit

Permalink
move to parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mdr committed Dec 8, 2024
1 parent 8457172 commit 04cc980
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 11 deletions.
4 changes: 3 additions & 1 deletion Aoc2024/Day08/Parser.lean
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ private def decorateWithCoordinates (s: String): List PointAndChar := do
let (x, c) <- line.toList.enum
pure ⟨⟨x, y⟩, c⟩

private def removeDots (decoratedChars : List PointAndChar) : List PointAndChar := decoratedChars.filter (·.char != '.')

def parseInput (s : String): PuzzleInput :=
let decoratedChars := decorateWithCoordinates s
let (xs, ys) := decoratedChars.map (·.point.toPair) |>.unzip
let width := xs.max?.map (· + 1 |>.toNat) |>.getD 0
let height := ys.max?.map (· + 1 |>.toNat) |>.getD 0
let bounds: Rectangle := { topLeft := Point.origin, width, height }
{ bounds, decoratedChars }
{ bounds, decoratedChars := removeDots decoratedChars }
4 changes: 1 addition & 3 deletions Aoc2024/Day08/Solve.lean
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ open Std (HashMap)

-- Part 1

private def removeDots (decoratedChars : List PointAndChar) : List PointAndChar := decoratedChars.filter (·.char != '.')

private def getAntennaeGroups (input : PuzzleInput) : List (List Point) :=
input.decoratedChars |> removeDots |>.groupByAndTransformValues (·.char) (·.point) |>.values
input.decoratedChars |>.groupByAndTransformValues (·.char) (·.point) |>.values

private def findAntinodes (antennaGroup : List Point) : List Point := do
let antenna1 <- antennaGroup
Expand Down
7 changes: 0 additions & 7 deletions Aoc2024/Utils.lean
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ instance [BEq α] [BEq β] : BEq (Except α β) where
| Except.error x, Except.error y => x == y
| _, _ => false

def getOrThrow (message : String) : Option α -> Except String α
| some x => pure x
| none => throw message

#guard (getOrThrow "Error" (some 42) == Except.ok 42)
#guard (getOrThrow "Error" (none : Option Int) == Except.error "Error")

def sepBy (p : Parser α) (sep : Parser β) : Parser (List α) :=
(do
let x ← p
Expand Down

0 comments on commit 04cc980

Please sign in to comment.