day15 p2 performance

Getting rid of the temp neighbor vector allocation is major speedup
main
Bryce Allen 4 years ago
parent ceff063ed0
commit 08a93a5418

@ -154,30 +154,27 @@ risk_map = tile_map(risk_map)
#println() #println()
#exit() #exit()
function neighbor_idx(nrows, ncols, y, x) function relax(nrows, ncols, R, costs, row, col; first=false)
v = CartesianIndex[] c1 = costs[row, col]
if x > 1 c = c1
push!(v, CartesianIndex(y, x-1)) if col > 1
c = min(c, costs[row, col-1] + R[row, col])
end end
if x < ncols if col < ncols
push!(v, CartesianIndex(y, x+1)) c = min(c, costs[row, col+1] + R[row, col])
end end
if y > 1 if row > 1
push!(v, CartesianIndex(y-1, x)) c = min(c, costs[row-1, col] + R[row, col])
end end
if y < nrows if row < nrows
push!(v, CartesianIndex(y+1, x)) c = min(c, costs[row+1, col] + R[row, col])
end end
return v
end
function relax(nrows, ncols, R, costs, row, col; first=false) if c < c1
idx = neighbor_idx(nrows, ncols, row, col)
c = minimum(costs[i] for i in idx) + R[row, col]
if first || c < costs[row, col]
costs[row, col] = c costs[row, col] = c
return true return true
end end
return false return false
end end
@ -233,7 +230,7 @@ function get_costs(R)
return costs, dirs return costs, dirs
end end
costs, dirs = get_costs(risk_map) @time costs, dirs = get_costs(risk_map)
if length(costs) < 1000 if length(costs) < 1000
display(costs) display(costs)

Loading…
Cancel
Save