aboutsummaryrefslogtreecommitdiff
path: root/day06/part1
diff options
context:
space:
mode:
Diffstat (limited to 'day06/part1')
-rwxr-xr-xday06/part147
1 files changed, 47 insertions, 0 deletions
diff --git a/day06/part1 b/day06/part1
new file mode 100755
index 0000000..e615e84
--- /dev/null
+++ b/day06/part1
@@ -0,0 +1,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