[2020][ruby][23.2]

pull/2/head
Alpha Chen 4 years ago
parent 45212a6916
commit f057404538

@ -1,31 +1,32 @@
cups = ARGF.read.chars.map(&:to_i) cups = Hash.new {|h,k|
cups_range = Range.new(*cups.minmax) fail if k > 1000000
h[k] = k + 1
}
current = 0 cup_values = ARGF.read.chars.map(&:to_i)
100.times do current = cup_values.first
current_value = cups.fetch(current % cups.size)
pickup_indices = 3.times.map {|i| (current + i + 1) % cups.size } cup_values = [1_000_000, *cup_values, cup_values.max + 1]
pickup = cups.values_at(*pickup_indices) cup_values.each_cons(2) do |a,b|
pickup_indices.sort.reverse.each do |i| cups[a] = b
cups.delete_at(i) end
end cups_range = Range.new(*cups.keys.minmax)
10_000_000.times do |i|
pickup = 2.times.inject([cups[current]]) {|p,_| p << cups[p.last] }
cups[current] = cups[pickup.last]
dest_value = current_value - 1 dest_value = current - 1
until cups_range.cover?(dest_value) && !pickup.include?(dest_value) loop do
dest_value = cups_range.max if dest_value == 0
break unless pickup.include?(dest_value)
dest_value -= 1 dest_value -= 1
dest_value = cups_range.end unless cups_range.cover?(dest_value)
end end
cups[pickup.last] = cups[dest_value]
cups[dest_value] = pickup.first
dest_index = cups.index(dest_value) current = cups[current]
cups.insert(dest_index+1, *pickup)
current = cups.index(current_value)
current += 1
current %= cups.size
end end
i = cups.index(1) p 1.times.inject([cups[1]]) {|p,_| p << cups[p.last] }.inject(&:*)
p (cups[i+1..-1] + cups[0...i]).join

Loading…
Cancel
Save