module Day16

_inp = "59773419794631560412886746550049210714854107066028081032096591759575145680294995770741204955183395640103527371801225795364363411455113236683168088750631442993123053909358252440339859092431844641600092736006758954422097244486920945182483159023820538645717611051770509314159895220529097322723261391627686997403783043710213655074108451646685558064317469095295303320622883691266307865809481566214524686422834824930414730886697237161697731339757655485312568793531202988525963494119232351266908405705634244498096660057021101738706453735025060225814133166491989584616948876879383198021336484629381888934600383957019607807995278899293254143523702000576897358"

inp = parse.(Int, split(Day16._inp, ""))

pat(i) = IterTools.imap(x -> (x for _ in 1:i), [0, 1, 0, -1]) |> Iterators.flatten

function simulate(v, reps)
    v = v |> copy |> x -> repeat(x, reps)
    nextv = v |> similar
    for j in 1:100
        for i in 1:length(v)
            pattern = i |> pat |> Iterators.cycle |> x -> Iterators.drop(x, 1)
            nextv[i] = abs(sum(prod, zip(v, pattern)) % 10)
        end

        v = nextv
    end

    v
end

solvea(v) = simulate(v, 1)[1:8] |> join

function solveb(v)
    offset = sum(v[k] * 10^(k - 1) for k in 1:7)
    simulate(v, 10000)[offset:offset + 8] |> join
end

end
