44 lines
1.3 KiB
Julia
44 lines
1.3 KiB
Julia
include("utils.jl")
|
|
|
|
function parse_input(filename)
|
|
map(x->parse.(Int, x), split.(non_empty_lines(filename), ""))
|
|
end
|
|
|
|
function flash(octopuses)
|
|
sum(map(CartesianIndices((2:11, 2:11))) do coord
|
|
if octopuses[coord] > 9
|
|
octopuses[coord] = -8
|
|
for neighbor in CartesianIndices((coord[1]-1:coord[1]+1, coord[2]-1:coord[2]+1))
|
|
octopuses[neighbor] += 1
|
|
end
|
|
1
|
|
else
|
|
0
|
|
end
|
|
end)
|
|
end
|
|
|
|
function solution11_1()
|
|
octopuses = transpose(reshape(reduce(vcat, [[fill(-2000000, 12)]; map(x -> [-2000000; x; -2000000], parse_input("11.data")); [fill(-2000000, 12)]]),(12,12)))
|
|
sum(map(1:100) do _
|
|
map!(x->x+1, octopuses, octopuses)
|
|
flashes = sum(collect(Iterators.takewhile(>(0), (flash(octopuses) for _ in 1:100))))
|
|
clamp!(octopuses, 0, 9)
|
|
flashes
|
|
end)
|
|
end
|
|
|
|
function solution11_2()
|
|
octopuses = transpose(reshape(reduce(vcat, [[fill(-2000000, 12)]; map(x -> [-2000000; x; -2000000], parse_input("11_ex.data")); [fill(-2000000, 12)]]),(12,12)))
|
|
for i in 1:1000
|
|
map!(x->x+1, octopuses, octopuses)
|
|
flashes = sum(collect(Iterators.takewhile(>(0), (flash(octopuses) for _ in 1:100))))
|
|
if flashes == 100
|
|
return i
|
|
end
|
|
clamp!(octopuses, 0, 9)
|
|
end
|
|
end
|
|
|
|
solution11_2()
|