From 40ee1eca5709784c3cbff94027e2058a7d72b6ce Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Tue, 5 Dec 2017 21:09:15 -0800 Subject: [PATCH] [2017][ruby][6.x] speedrun --- 2017/input/day_06.txt | 2 ++ 2017/ruby/day_06.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 2017/input/day_06.txt create mode 100644 2017/ruby/day_06.rb diff --git a/2017/input/day_06.txt b/2017/input/day_06.txt new file mode 100644 index 0000000..5839c56 --- /dev/null +++ b/2017/input/day_06.txt @@ -0,0 +1,2 @@ +4 10 4 1 8 4 9 14 5 1 14 15 0 15 3 5 + diff --git a/2017/ruby/day_06.rb b/2017/ruby/day_06.rb new file mode 100644 index 0000000..4f54a2f --- /dev/null +++ b/2017/ruby/day_06.rb @@ -0,0 +1,27 @@ +banks = ARGF.read.strip.split("\t").map(&:to_i) + +history = { banks => true} +count = 0 +seen = false +loop do + v, i = banks.map.with_index {|v,i| [v,i] }.max_by(&:first) + banks[i] = 0 + i += 1 + while v > 0 + banks[i % banks.size] += 1 + v -= 1 + i += 1 + end + count += 1 + if history.has_key?(banks) + if seen + puts count + exit + else + history.clear + seen = true + count = 0 + end + end + history[banks] = true +end