clean up day 6 and add "precompilation" function that runs part once to precompile the function and then times the compiled result

This commit is contained in:
Patrick Michl 2021-12-06 07:41:20 +01:00
parent 9174bfc690
commit 0fc3fcc67b
2 changed files with 21 additions and 13 deletions

18
aoc.jl
View File

@ -35,15 +35,29 @@ end
function _run_parts(day) function _run_parts(day)
input = _input(day)
if "precompile" ARGS
@info "precompilation is on"
end
if isdefined(Main, :p1) if isdefined(Main, :p1)
p1_res = @time Main.p1(_input(day)) if "precompile" ARGS
i = copy(input)
Main.p1(i)
end
i = copy(input)
p1_res = @time Main.p1(i)
@info "Part 1: $p1_res" @info "Part 1: $p1_res"
else else
@info "p1() not defined. skipping..." @info "p1() not defined. skipping..."
end end
if isdefined(Main, :p2) if isdefined(Main, :p2)
p2_res = @time Main.p2(_input(day)) if "precompile" ARGS
i = copy(input)
Main.p2(i)
end
i = copy(input)
p2_res = @time Main.p2(i)
@info "Part 2: $p2_res" @info "Part 2: $p2_res"
else else
@info "p2() not defined. skipping..." @info "p2() not defined. skipping..."

View File

@ -3,21 +3,15 @@ import .Aoc: @aoc
using Pipe: @pipe using Pipe: @pipe
function solve(input, days) function solve(input::Vector{String}, days::Int64)
fish = @pipe split(input[1], ",") |> parse.(Int, _) fish = @pipe split(input[1], ",") |> parse.(Int, _)
state = Dict{Int, Int}([ [x, 0] for x in 0:8 ]) state = Dict{Int, Int}([ [x, count(==(x), fish)] for x in 0:8 ])
for f in fish for _ in 1:days
state[f] += 1
end
for d in 1:days
next_day = Dict{Int, Int}([ [x, 0] for x in 0:8 ]) next_day = Dict{Int, Int}([ [x, 0] for x in 0:8 ])
next_day[8] = get(state, 0, 0) next_day[8] = next_day[6] = state[0]
next_day[6] = get(state, 0, 0)
for i in 0:7 for i in 0:7
k = get(state, i+1, 0) next_day[i] += state[i+1]
next_day[i] += k
end end
state = next_day state = next_day
end end