From 9174bfc690a27b1475790a23a3a04fd07b5ec4b3 Mon Sep 17 00:00:00 2001 From: Patrick Michl Date: Mon, 6 Dec 2021 07:02:23 +0100 Subject: [PATCH] fix my stupidity and refactor p1 to use generic solution --- src/day_6.jl | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/src/day_6.jl b/src/day_6.jl index 6277164..3e2454b 100644 --- a/src/day_6.jl +++ b/src/day_6.jl @@ -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)