aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-12-06 10:53:41 +0000
committerNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-12-06 10:53:41 +0000
commitf33d510a4dda510fa93137a8897230ebf539f435 (patch)
tree5081302d7df1db978f9d8bde8fe4f1a4c9db851d
parentf108ad65874aea085825edd8548a615a90d26d7b (diff)
Day 06
-rw-r--r--day06/input1
-rwxr-xr-xday06/part147
-rwxr-xr-xday06/part245
3 files changed, 93 insertions, 0 deletions
diff --git a/day06/input b/day06/input
new file mode 100644
index 0000000..32e8d7e
--- /dev/null
+++ b/day06/input
@@ -0,0 +1 @@
+11 11 13 7 0 15 5 5 4 4 1 1 7 1 15 11
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
diff --git a/day06/part2 b/day06/part2
new file mode 100755
index 0000000..9b8d8de
--- /dev/null
+++ b/day06/part2
@@ -0,0 +1,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)