Skip to content

Commit 96d8bb6

Browse files
committed
[Day 16] Fix test issue.
1 parent 6236261 commit 96d8bb6

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/day16.jl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ function day16(input::String = readInput(joinpath(@__DIR__, "..", "data", "day16
1515
endpos = findfirst(==('E'), data)
1616
p1 = minimum(dist[endpos[1], endpos[2], dir] for dir in 1:4)
1717

18-
visited = bfs_trace(prev, endpos, nrows, ncols)
18+
# Collect end positions with minimal distance
19+
endpositions = Tuple{Int,Int,Int}[]
20+
for dir in 1:4
21+
if dist[endpos[1], endpos[2], dir] == p1
22+
push!(endpositions, (endpos[1], endpos[2], dir))
23+
end
24+
end
25+
26+
visited = bfs_trace(prev, endpositions, nrows, ncols)
1927
return [p1, sum(visited)]
2028
end
2129

@@ -61,13 +69,13 @@ function dijkstra(passable::BitMatrix, startpos::CartesianIndex{2}, nrows::Int,
6169
return dist, prev
6270
end
6371

64-
function bfs_trace(prev::Array{Vector{Tuple{Int,Int,Int}},3}, endpos::CartesianIndex{2}, nrows::Int, ncols::Int)
72+
function bfs_trace(prev::Array{Vector{Tuple{Int,Int,Int}},3}, endpositions::Vector{Tuple{Int,Int,Int}}, nrows::Int, ncols::Int)
6573
visited = falses(nrows, ncols)
6674
queue = Deque{Tuple{Int,Int,Int}}()
6775
enqueued = falses(nrows, ncols, 4)
6876

69-
for dir in 1:4
70-
pos = (endpos[1], endpos[2], dir)
77+
# Initialize only with minimal distance end positions
78+
for pos in endpositions
7179
if !isempty(prev[pos...])
7280
push!(queue, pos)
7381
enqueued[pos...] = true

0 commit comments

Comments
 (0)