From 08a93a5418d0ccb5d5c2697123878d6591b38844 Mon Sep 17 00:00:00 2001 From: Bryce Allen Date: Fri, 17 Dec 2021 16:54:37 -0500 Subject: [PATCH] day15 p2 performance Getting rid of the temp neighbor vector allocation is major speedup --- day15/part2.jl | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/day15/part2.jl b/day15/part2.jl index 208a1ac..9e8635a 100755 --- a/day15/part2.jl +++ b/day15/part2.jl @@ -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)) +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 x < ncols - push!(v, CartesianIndex(y, x+1)) + if col < ncols + c = min(c, costs[row, col+1] + R[row, col]) end - if y > 1 - push!(v, CartesianIndex(y-1, x)) + if row > 1 + c = min(c, costs[row-1, col] + R[row, col]) end - if y < nrows - push!(v, CartesianIndex(y+1, x)) + if row < nrows + c = min(c, costs[row+1, col] + R[row, col]) 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] + 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)