Skip to content

Commit 63b748b

Browse files
committed
More clean up
1 parent 63b9013 commit 63b748b

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/aoc/year2024/day18.gleam

+6-9
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ fn input_p() -> Parser(List(Coord)) {
4646
party.sep1(coord_p(), party.string("\n"))
4747
}
4848

49-
fn neighbors(grid: Grid, coord: Coord) -> List(Coord) {
49+
fn neighbors(grid: Grid, seen: Set(Coord), coord: Coord) -> List(Coord) {
5050
let #(x, y) = coord
5151
[#(x + 1, y), #(x - 1, y), #(x, y + 1), #(x, y - 1)]
52-
|> list.filter(fn(c) { dict.get(grid, c) == Ok(".") })
52+
|> list.filter(fn(c) {
53+
dict.get(grid, c) == Ok(".") && !set.contains(seen, c)
54+
})
5355
}
5456

5557
fn steps(
@@ -67,13 +69,8 @@ fn steps(
6769
let seen = set.insert(seen, coord)
6870
let q =
6971
grid
70-
|> neighbors(coord)
71-
|> list.filter_map(fn(c) {
72-
case set.contains(seen, c) {
73-
True -> Error(Nil)
74-
False -> Ok(#(score + 1, c))
75-
}
76-
})
72+
|> neighbors(seen, coord)
73+
|> list.map(fn(c) { #(score + 1, c) })
7774
|> list.fold(q, pq.push)
7875

7976
steps(grid, q, goal, seen)

0 commit comments

Comments
 (0)