From 45212a6916d440abe70bcf7ea94f3a371f59d008 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Wed, 23 Dec 2020 09:34:21 -0800 Subject: [PATCH] [2020][ruby][23.1] --- 2020/ruby/day_23.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2020/ruby/day_23.rb diff --git a/2020/ruby/day_23.rb b/2020/ruby/day_23.rb new file mode 100644 index 0000000..6a4093f --- /dev/null +++ b/2020/ruby/day_23.rb @@ -0,0 +1,31 @@ +cups = ARGF.read.chars.map(&:to_i) +cups_range = Range.new(*cups.minmax) + +current = 0 +100.times do + current_value = cups.fetch(current % cups.size) + + pickup_indices = 3.times.map {|i| (current + i + 1) % cups.size } + pickup = cups.values_at(*pickup_indices) + pickup_indices.sort.reverse.each do |i| + cups.delete_at(i) + end + + dest_value = current_value - 1 + until cups_range.cover?(dest_value) && !pickup.include?(dest_value) + dest_value -= 1 + dest_value = cups_range.end unless cups_range.cover?(dest_value) + end + + + dest_index = cups.index(dest_value) + + cups.insert(dest_index+1, *pickup) + + current = cups.index(current_value) + current += 1 + current %= cups.size +end + +i = cups.index(1) +p (cups[i+1..-1] + cups[0...i]).join