fix my stupidity and refactor p1 to use generic solution

This commit is contained in:
Patrick Michl 2021-12-06 07:02:23 +01:00
parent da41491861
commit 9174bfc690

View File

@ -3,33 +3,15 @@ import .Aoc: @aoc
using Pipe: @pipe
function p1(input::Vector{String})
function solve(input, days)
fish = @pipe split(input[1], ",") |> parse.(Int, _)
for d in 1:80
for f in 1:length(fish)
if fish[f] == 0
fish[f] = 6
push!(fish, 8)
else
fish[f] -= 1
end
end
end
return length(fish)
end
function p2(input::Vector{String})
fish = @pipe split(input[1], ",") |> parse.(Int, _)
state = Dict{Int, Int}()
fish = [3,4,3,1,2]
state = Dict{Int, Int}([ [x, 0] for x in 0:8 ])
for f in fish
haskey(state, f) ? (state[f] += 1) : (state[f] = 1)
state[f] += 1
end
@info state
for d in 1:256
for d in 1:days
next_day = Dict{Int, Int}([ [x, 0] for x in 0:8 ])
next_day[8] = get(state, 0, 0)
next_day[6] = get(state, 0, 0)
@ -39,7 +21,15 @@ function p2(input::Vector{String})
end
state = next_day
end
return length(values(state))
return sum(values(state))
end
function p1(input::Vector{String})
return solve(input, 80)
end
function p2(input::Vector{String})
return solve(input, 256)
end
@aoc(2021, 6)