From 0581fd9c888bc282ea3b7b7cdc808cf6fed1bddc Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Wed, 6 Dec 2017 19:02:14 +0100 Subject: [PATCH] 6th day --- memory_alloc.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 memory_alloc.rb 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