Skip to content

Commit 3f7e1bf

Browse files
committed
misc
1 parent 72911cf commit 3f7e1bf

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

Aoc2024/Day06/Parser.lean

+14-9
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,25 @@ open Std.Internal.Parsec.String
66
open Std.Internal.Parsec
77
open Std (HashSet)
88

9-
-- private def inputParser : Parser (List Int) := sorry
9+
instance : Bind List where bind := List.bind
10+
instance : Pure List where pure := List.pure
1011

11-
private def decorateWithCoordinates (s: String): List (Point × Char) :=
12-
s.splitOn "\n" |>.enum |>.bind (λ ⟨y, line⟩ =>
13-
line.toList.enum.map (λ ⟨x, c⟩ => (⟨x, y⟩, c))
14-
)
12+
private def decorateWithCoordinates (s: String): List (Point × Char) := do
13+
let (y, line) <- s.splitOn "\n" |> .enum
14+
let (x, c) <- line.toList.enum
15+
pure (⟨x, y⟩, c)
1516

1617
def parseInput (s: String): Except String PuzzleInput := do
1718
let decoratedChars := decorateWithCoordinates s
1819
let obstacles := decoratedChars.filterMap (λ ⟨p, c⟩ => (c == '#').toOption p) |>.toSet
1920
let start <- decoratedChars.filterMap (λ ⟨p, c⟩ => (c == '^').toOption p) |>.head? |>.getOrThrow "no start found"
2021
let (xs, ys) := decoratedChars.map (·.1.toPair) |>.unzip
21-
let width <- xs.toArray.max?.map (· + 1 |>.toNat) |>.getOrThrow "empty input"
22-
let height <- ys.toArray.max?.map (· + 1 |>.toNat) |>.getOrThrow "empty input"
23-
pure { obstacles, start, width, height }
22+
let width <- xs.max?.map (· + 1 |>.toNat) |>.getOrThrow "empty input"
23+
let height <- ys.max?.map (· + 1 |>.toNat) |>.getOrThrow "empty input"
24+
let bounds: Rectangle := { topLeft := Point.origin, width, height }
25+
pure { obstacles, start, bounds }
2426

25-
-- #guard parseReports exampleInput == Except.ok 42
27+
private def examplePuzzleInput := parseInput exampleInput
28+
#guard (examplePuzzleInput.map (·.bounds)) == Except.ok (Rectangle.mk Point.origin 10 10)
29+
#guard (examplePuzzleInput.map (·.start)) == Except.ok ⟨4, 6
30+
#guard (examplePuzzleInput.map (·.obstacles.size)) == Except.ok 8

Aoc2024/Day06/Types.lean

+29-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ open Std (HashSet)
44
structure Point where
55
x : Int
66
y : Int
7-
deriving BEq, Hashable, Repr
7+
deriving BEq, Hashable, Repr, Inhabited
88

99
def Point.toPair (p : Point) : (Int × Int) := (p.x, p.y)
1010

@@ -14,9 +14,35 @@ inductive Direction where
1414
| Down
1515
| Left
1616

17+
def Direction.turnRight : Direction -> Direction
18+
| Direction.Up => Direction.Right
19+
| Direction.Right => Direction.Down
20+
| Direction.Down => Direction.Left
21+
| Direction.Left => Direction.Up
22+
23+
def Point.step (p : Point) (d : Direction) : Point :=
24+
match d with
25+
| Direction.Up => { p with y := p.y - 1 }
26+
| Direction.Right => { p with x := p.x + 1 }
27+
| Direction.Down => { p with y := p.y + 1 }
28+
| Direction.Left => { p with x := p.x - 1 }
29+
30+
def Point.origin : Point := { x := 0, y := 0 }
31+
32+
structure Rectangle where
33+
topLeft : Point
34+
width: Nat
35+
height: Nat
36+
deriving Repr, BEq, Hashable, Inhabited
37+
38+
def Rectangle.contains (r : Rectangle) (p : Point) : Bool :=
39+
r.topLeft.x ≤ p.x && p.x < r.topLeft.x + r.width &&
40+
r.topLeft.y ≤ p.y && p.y < r.topLeft.y + r.height
41+
42+
#guard Rectangle.contains { topLeft := Point.origin, width := 2, height := 2 } { x := 1, y := 1 }
43+
1744
structure PuzzleInput where
1845
obstacles : HashSet Point
19-
width : Nat
20-
height : Nat
46+
bounds : Rectangle
2147
start : Point
2248
deriving Repr

0 commit comments

Comments
 (0)