blob: 9b8d8de37e49aadaadaf47ebe5539cf46a33e9ce (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#!/usr/bin/env ruby
input = gets.chomp.split("\t").map(&:to_i)
def index_of_max(arr)
idx = 0
max = 0
(0...arr.length).each do |i|
if arr[i] > max then
idx = i
max = arr[i]
end
end
return idx
end
def redistribute(arr, idx)
new = arr.dup
len = new.length
blks = new[idx]
new[idx] = 0
i = idx
while blks > 0 do
i = (i + 1) % len
new[i] += 1
blks -= 1
end
return new
end
previous = []
state = input.dup
until previous.include?(state) do
newstate = redistribute(state, index_of_max(state))
previous << state
state = newstate
end
puts previous.length - previous.index(state)
|