From face2d85f846d78be3c01161b485e1dc51087d2b Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Thu, 15 Dec 2016 08:14:32 -0800 Subject: [PATCH] [2016][ruby][15.0] --- 2016/ruby/day_15.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 2016/ruby/day_15.rb diff --git a/2016/ruby/day_15.rb b/2016/ruby/day_15.rb new file mode 100644 index 0000000..36b9ae0 --- /dev/null +++ b/2016/ruby/day_15.rb @@ -0,0 +1,20 @@ +Disc = Struct.new(:positions, :start_position) do + def position_at(time) + (start_position + time) % positions + end +end + +discs = DATA.each_line.map {|line| + /Disc #\d has (?\d+) positions; at time=0, it is at position (?\d+)./ =~ line + Disc.new(positions.to_i, position.to_i) +} + +p (0..Float::INFINITY).find {|t| + discs.each.with_index.all? {|disc, index| + disc.position_at(t + index) == 0 + } +} + +__END__ +Disc #1 has 5 positions; at time=0, it is at position 4. +Disc #2 has 2 positions; at time=0, it is at position 1.