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

Loading…
Cancel
Save