From e26833939c622174b72bde873a9a6c465a299cd3 Mon Sep 17 00:00:00 2001 From: HeNine <> Date: Sat, 11 Dec 2021 09:20:22 +0100 Subject: [PATCH] Day ll --- AoC2021.jl | 2 ++ day11/Project.toml | 4 +++ day11/data/case00.txt | 10 ++++++ day11/data/case01.txt | 10 ++++++ day11/src/day11.jl | 83 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 day11/Project.toml create mode 100644 day11/data/case00.txt create mode 100644 day11/data/case01.txt create mode 100644 day11/src/day11.jl diff --git a/AoC2021.jl b/AoC2021.jl index 2fcf879..59288a3 100644 --- a/AoC2021.jl +++ b/AoC2021.jl @@ -1,3 +1,5 @@ +using Printf + function verify(day, task, case, expresult) taskfun = getfield(Main, Symbol("task", task - 1)) filename = "day$(@sprintf "%02d" day)\\data\\case$(@sprintf "%02d" case).txt" diff --git a/day11/Project.toml b/day11/Project.toml new file mode 100644 index 0000000..f2cf869 --- /dev/null +++ b/day11/Project.toml @@ -0,0 +1,4 @@ +name = "day11" +uuid = "eeb063b9-3c05-4ffd-8498-60dcd43cb32f" +authors = ["HeNine "] +version = "0.1.0" diff --git a/day11/data/case00.txt b/day11/data/case00.txt new file mode 100644 index 0000000..a3819c9 --- /dev/null +++ b/day11/data/case00.txt @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 \ No newline at end of file diff --git a/day11/data/case01.txt b/day11/data/case01.txt new file mode 100644 index 0000000..c4a08de --- /dev/null +++ b/day11/data/case01.txt @@ -0,0 +1,10 @@ +7777838353 +2217272478 +3355318645 +2242618113 +7182468666 +5441641111 +4773862364 +5717125521 +7542127721 +4576678341 \ No newline at end of file diff --git a/day11/src/day11.jl b/day11/src/day11.jl new file mode 100644 index 0000000..34c6e3d --- /dev/null +++ b/day11/src/day11.jl @@ -0,0 +1,83 @@ +include("../../AoC2021.jl") + +## + +function parsefile(file) + (eachline(file) .|> Base.Fix2(split, "") .|> + (x -> parse.(Int, x)) .|> + permutedims) |> x -> vcat(x...) +end + +function advancegen(mappe) + mappe .+= 1 + + reset = [] + + topop = findall(mappe .> 9) + reset = copy(topop) + + while length(topop) > 0 + + o = pop!(topop) + + for ox ∈ -1:1, oy ∈ -1:1 + mappe[o+CartesianIndex(oy, ox)] += 1 + if mappe[o+CartesianIndex(oy, ox)] > 9 && o + CartesianIndex(oy, ox) ∉ reset + push!(topop, o + CartesianIndex(oy, ox)) + push!(reset, o + CartesianIndex(oy, ox)) + end + end + end + + mappe[reset] .= 0 + mappe[1, :] .= -9 + mappe[end, :] .= -9 + mappe[:, 1] .= -9 + mappe[:, end] .= -9 + + mappe +end + +## + +function task0(file) + raw_mappe = parsefile(file)::Matrix{Int} + mappe = fill(-9, size(raw_mappe) .+ 2) + mappe[2:(end-1), 2:(end-1)] = raw_mappe + + popped = 0 + + for gen = 1:100 + mappe = advancegen(mappe) + popped += sum(mappe .== 0) + end + popped +end + +## + +verify(11, 1, 0, 1656) +verify(11, 1, 1, 1721) + +## + +function task1(file) + + raw_mappe = parsefile(file)::Matrix{Int} + mappe = fill(-9, size(raw_mappe) .+ 2) + mappe[2:(end-1), 2:(end-1)] = raw_mappe + + gen = 0 + + while !all(mappe[2:(end-1), 2:(end-1)] .== 0) + gen += 1 + mappe = advancegen(mappe) + end + + gen +end + +## + +verify(11, 2, 0, 195) +verify(11, 2, 1, 298) \ No newline at end of file