@@ -15,7 +15,15 @@ function day16(input::String = readInput(joinpath(@__DIR__, "..", "data", "day16
15
15
endpos = findfirst (== (' E' ), data)
16
16
p1 = minimum (dist[endpos[1 ], endpos[2 ], dir] for dir in 1 : 4 )
17
17
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)
19
27
return [p1, sum (visited)]
20
28
end
21
29
@@ -61,13 +69,13 @@ function dijkstra(passable::BitMatrix, startpos::CartesianIndex{2}, nrows::Int,
61
69
return dist, prev
62
70
end
63
71
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 )
65
73
visited = falses (nrows, ncols)
66
74
queue = Deque {Tuple{Int,Int,Int}} ()
67
75
enqueued = falses (nrows, ncols, 4 )
68
76
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
71
79
if ! isempty (prev[pos... ])
72
80
push! (queue, pos)
73
81
enqueued[pos... ] = true
0 commit comments