diff --git a/memory_alloc.rb b/memory_alloc.rb new file mode 100644 index 0000000..6b92ac3 --- /dev/null +++ b/memory_alloc.rb @@ -0,0 +1,22 @@ +input = "5 1 10 0 1 7 13 14 3 12 8 10 7 12 0 6".split("\t").map(&:to_i) + +snapshots = [] +res = 0 +while not snapshots.include? input do + snapshots << input.dup + max_val = input.max + idx = input.index max_val + input[idx] = 0 + while max_val > 0 do + idx += 1 + input[idx % input.length] += 1 + max_val -= 1 + end + res += 1 +end +puts res + + +cycle_first = snapshots.find_index(input) +puts cycle_first +puts res - cycle_first