day15 p2 performance
Getting rid of the temp neighbor vector allocation is major speedup
This commit is contained in:
@@ -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))
|
||||
end
|
||||
if y > 1
|
||||
push!(v, CartesianIndex(y-1, x))
|
||||
end
|
||||
if y < nrows
|
||||
push!(v, CartesianIndex(y+1, x))
|
||||
end
|
||||
return v
|
||||
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]
|
||||
c1 = costs[row, col]
|
||||
c = c1
|
||||
if col > 1
|
||||
c = min(c, costs[row, col-1] + R[row, col])
|
||||
end
|
||||
if col < ncols
|
||||
c = min(c, costs[row, col+1] + R[row, col])
|
||||
end
|
||||
if row > 1
|
||||
c = min(c, costs[row-1, col] + R[row, col])
|
||||
end
|
||||
if row < nrows
|
||||
c = min(c, costs[row+1, col] + R[row, col])
|
||||
end
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user