aoc2021/20.jl

59 lines
1.5 KiB
Julia

include("utils.jl")
function convert_to_binary(string)
map(x->x=='#' ? 1 : 0, collect(string))
end
function parse_input(filename, iterations)
lines = collect(non_empty_lines(filename))
algorithm = convert_to_binary(lines[1])
dim = length(lines[2])
buffered_dim = 2 * iterations + dim
fix_lines = fill(fill(0, buffered_dim), iterations)
i = [fix_lines;
map(convert_to_binary, map(x-> reduce(*, fill(".", iterations)) * x * reduce(*, fill(".", iterations)), lines[2:length(lines)]));
fix_lines]
image = transpose(reshape(reduce(vcat, i), buffered_dim, buffered_dim))
algorithm, image
end
function getv(algorithm, image, i, x, y)
address = []
for ox in (x-1):(x+1)
for oy in (y-1):(y+1)
if ox < 1 || ox > size(image)[1] || oy < 1 || oy > size(image)[1]
# example
# push!(address, "0")
# real
push!(address, i % 2 == 1 ? "0" : "1")
else
push!(address, string(image[ox,oy]))
end
end
end
algorithm[parse(Int, reduce(*, address), base=2)+1]
end
function enhance(fname, iterations)
algorithm, image = parse_input(fname, iterations)
for i in 1:iterations
new = copy(image)
for x in 1:size(image)[1]
for y in 1:size(image)[1]
new[x, y] = getv(algorithm, image, i, x, y)
end
end
image = new
end
length(filter(==(1), image))
end
function solution20_1()
enhance("20.data", 50)
end
solution20_1()