solve and cleanup day9
This commit is contained in:
parent
bbd18bcfd0
commit
1bf812ce9d
86
src/day_9.jl
86
src/day_9.jl
@ -4,87 +4,57 @@ using Pipe: @pipe
|
|||||||
using Combinatorics
|
using Combinatorics
|
||||||
|
|
||||||
|
|
||||||
function p1(input::Vector{String})
|
|
||||||
map = []
|
|
||||||
for l in input
|
|
||||||
push!(map, [ parse(Int, c) for c in l ])
|
|
||||||
end
|
|
||||||
risks = 0
|
|
||||||
for x in 1:length(map)
|
|
||||||
for y in 1:length(map[1])
|
|
||||||
adjacent = []
|
|
||||||
for (a,b) in [[0,1], [1,0], [0,-1], [-1,0]]
|
|
||||||
i = x + a
|
|
||||||
j = y + b
|
|
||||||
|
|
||||||
if i < 1 || i > length(map) || j < 1 || j > length(map[1])
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
push!(adjacent, map[i][j])
|
|
||||||
end
|
|
||||||
@info map[x][y]
|
|
||||||
@info adjacent
|
|
||||||
if map[x][y] < minimum(adjacent)
|
|
||||||
@info "low"
|
|
||||||
risks += map[x][y] + 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return risks
|
|
||||||
end
|
|
||||||
|
|
||||||
function lowpoints(map)
|
function lowpoints(map)
|
||||||
low = []
|
low = Dict()
|
||||||
for x in 1:length(map)
|
for x in 1:length(map)
|
||||||
for y in 1:length(map[1])
|
for y in 1:length(map[1])
|
||||||
adjacent = []
|
adjacent = []
|
||||||
for (a,b) in [[0,1], [1,0], [0,-1], [-1,0]]
|
for (i,j) in [ [x+i,y+j] for (i,j) in zip([0,1,0,-1], [1,0,-1,0]) ]
|
||||||
i = x + a
|
if i in 1:length(map) && j in 1:length(map[1])
|
||||||
j = y + b
|
|
||||||
|
|
||||||
if i < 1 || i > length(map) || j < 1 || j > length(map[1])
|
|
||||||
continue
|
|
||||||
end
|
|
||||||
push!(adjacent, map[i][j])
|
push!(adjacent, map[i][j])
|
||||||
end
|
end
|
||||||
|
end
|
||||||
if map[x][y] < minimum(adjacent)
|
if map[x][y] < minimum(adjacent)
|
||||||
push!(low, [x,y])
|
low[[x,y]] = map[x][y]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return low
|
return low
|
||||||
end
|
end
|
||||||
|
|
||||||
function search(map, pos)
|
function p1(input::Vector{String})
|
||||||
queue = [pos]
|
map = [ parse.(Int, collect(l)) for l in input ]
|
||||||
visited = [pos]
|
low = lowpoints(map)
|
||||||
|
return sum(values(low)) + length(low)
|
||||||
|
end
|
||||||
|
|
||||||
while length(queue) > 0
|
|
||||||
c = popfirst!(queue)
|
function search(map, pos)
|
||||||
h = map[c[1], c[2]]
|
q = [pos]
|
||||||
for pos in [ [x+i,y+j] for (i,j) in zip([0,1,0,-1], [1,0,-1,0]) ]
|
v = [pos]
|
||||||
if p[1] < 1 || p[1] > length(map) || p[2] < 1 || p[2] > length(map[1])
|
|
||||||
continue
|
while length(q) > 0
|
||||||
end
|
c = popfirst!(q)
|
||||||
if pos ∉ visited
|
h = map[c[1]][c[2]]
|
||||||
|
for p in [ [c[1]+i,c[2]+j] for (i,j) in zip([0,1,0,-1], [1,0,-1,0]) ]
|
||||||
|
if p[1] in 1:length(map) && p[2] in 1:length(map[1])
|
||||||
|
if p ∉ v
|
||||||
h2 = map[p[1]][p[2]]
|
h2 = map[p[1]][p[2]]
|
||||||
if h2 < 9 && h2 > h
|
if h < h2 < 9
|
||||||
push!(visited, pos)
|
push!(v, p)
|
||||||
push!(queue, pos)
|
push!(q, p)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return visited
|
end
|
||||||
|
return v
|
||||||
end
|
end
|
||||||
|
|
||||||
function p2(input::Vector{String})
|
function p2(input::Vector{String})
|
||||||
map = []
|
map = [ parse.(Int, collect(l)) for l in input ]
|
||||||
for l in input
|
|
||||||
push!(map, [ parse(Int, c) for c in l ])
|
|
||||||
end
|
|
||||||
low = lowpoints(map)
|
low = lowpoints(map)
|
||||||
basin_sizes = [ length(search(map, p)) for p in low ]
|
basin_sizes = [ length(search(map, p)) for p in keys(low) ]
|
||||||
return prod(sort(basin_sizes)[end-2:end])
|
return prod(sort(basin_sizes)[end-2:end])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user