aoc2021/15.jl

64 lines
1.9 KiB
Julia

include("utils.jl")
function parse_input(filename)
map(x->parse.(Int, x), split.(non_empty_lines(filename), ""))
end
function search(entry_risk)
width, height = size(entry_risk)
total_risk = fill(typemax(Int64), width, height)
total_risk[1, 1] = 0
to_visit = [(CartesianIndex(1,2), 0), (CartesianIndex(2,1), 0)]
while length(to_visit) > 0
rounds += 1
coord, cost = popfirst!(to_visit)
cost += entry_risk[coord]
if cost < total_risk[coord]
total_risk[coord] = cost
if coord[1] > 1
push!(to_visit, (CartesianIndex(coord[1] - 1, coord[2]), cost))
end
if coord[1] < width
push!(to_visit, (CartesianIndex(coord[1] + 1, coord[2]), cost))
end
if coord[2] > 1
push!(to_visit, (CartesianIndex(coord[1], coord[2] - 1), cost))
end
if coord[2] < height
push!(to_visit, (CartesianIndex(coord[1], coord[2] + 1), cost))
end
end
end
total_risk[width, height]
end
function solution15_1()
entry_risk = parse_input("15.data")
width = length(entry_risk)
height = length(entry_risk[1])
entry_risk = transpose(reshape(reduce(vcat, entry_risk), height, width))
search(entry_risk)
end
function solution15_2()
entry_risk = parse_input("15.data")
width = length(entry_risk)
height = length(entry_risk[1])
entry_risk = map(entry_risk) do line
[line;
map(x->mod((x-1)+1, 9)+1, line);
map(x->mod((x-1)+2, 9)+1, line);
map(x->mod((x-1)+3, 9)+1, line);
map(x->mod((x-1)+4, 9)+1, line);
]
end
for _ in 1:(4*height)
push!(entry_risk, map(x->mod((x-1)+1, 9)+1, entry_risk[length(entry_risk) - (height - 1)]))
end
entry_risk = transpose(reshape(reduce(vcat, entry_risk), height*5, width*5))
search(entry_risk)
end
solution15_2()