aboutsummaryrefslogtreecommitdiff
path: root/day06/part1
blob: e615e8469cb5cf1364f8839487e06f6f79d7bc1e (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
46
47
#!/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
steps = 0

until previous.include?(state) do
  newstate = redistribute(state, index_of_max(state))
  previous << state
  state = newstate
  steps += 1
end

puts steps