Part 1 and 2


In [1]:
function next_status(s)
    i = indmax(s)
    ent = div(s[i], length(s))
    res = s[i] % length(s)
    s[i] = 0
    s += ent
    s[1:(res - (length(s) - i))] += 1
    s[i+1:min(length(s), i+res)] += 1

    s
end


Out[1]:
next_status (generic function with 1 method)

In [2]:
function redistribution_cycles(s)
    i, p = 0, Dict()
    h = hash(s)
    while !haskey(p, h)
        p[h] = i
        s = next_status(s)
        h = hash(s)
        i += 1
    end
    i, i - p[h]
end


Out[2]:
redistribution_cycles (generic function with 1 method)

In [5]:
@time redistribution_cycles([0 2 7 0])


  0.000053 seconds (42 allocations: 3.234 KiB)
Out[5]:
(5, 4)

In [11]:
@time redistribution_cycles(readdlm("inputs/day6.txt", Int16))


  0.019458 seconds (59.77 k allocations: 4.619 MiB)
Out[11]:
(5042, 1086)

In [ ]: