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]:
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]:
In [5]:
@time redistribution_cycles([0 2 7 0])
Out[5]:
In [11]:
@time redistribution_cycles(readdlm("inputs/day6.txt", Int16))
Out[11]:
In [ ]: