Skip to content

Commit

Permalink
Optimize by blocking only relevant coords in part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mhoward540 committed Dec 6, 2024
1 parent 15d6085 commit 271c701
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions aoc2024/src/aoc_2024/day_6.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,23 @@ pub fn pt_2(input: String) {
input
|> parse_input

gridutil.iter_grid(grid)
|> iterator.map(fn(entry) {
let #(coord, space) = entry
case space {
"#" -> False
_ -> {
let g = dict.insert(grid.matrix, coord, "#")
let #(_, loops) =
visit(
gridutil.Grid(matrix: g, max_y: grid.max_y, max_x: grid.max_x),
lib,
set.new(),
)

loops
}
}
let #(coords_to_visit, _) = visit(grid, lib, set.new())

let coords_to_visit =
coords_to_visit
|> set.map(fn(x) { x.location })

set.to_list(coords_to_visit)
|> list.map(fn(coord) {
let g = dict.insert(grid.matrix, coord, "#")
let #(_, loops) =
visit(
gridutil.Grid(matrix: g, max_y: grid.max_y, max_x: grid.max_x),
lib,
set.new(),
)

loops
})
|> iterator.fold(0, fn(acc, looped) { acc + bool.to_int(looped) })
|> list.fold(0, fn(acc, looped) { acc + bool.to_int(looped) })
}

0 comments on commit 271c701

Please sign in to comment.