24th Offline Real-time Writing Problem
I solved it in Clojure earlier. https://qiita.com/kts_h/items/64bb74fdcef642198bc5
Many answers by Ruby have already been issued, but I tried to solve it myself because it became interesting, so I will post it.
I wanted to write the ʻeach_charblock in
solve` neatly, so I made it a class and changed the contents of the instance variables one after another.
class MultiStageSelection
def self.solve(cmds)
new.solve(cmds)
end
def initialize
@enum = 1.step
end
def drop_nth(n)
enum = @enum
@enum = Enumerator.new do |y|
n -= 1
loop do
n.times { y << enum.next }
enum.next
end
end
self
end
def drop_before(test)
enum = @enum
@enum = Enumerator.new do |y|
a, b = enum.next, enum.next
loop do
y << a unless send(test, b)
a, b = b, enum.next
end
end
self
end
def drop_after(test)
enum = @enum
@enum = Enumerator.new do |y|
a, b = enum.next, enum.next
y << a
loop do
y << b unless send(test, a)
a, b = b, enum.next
end
end
self
end
def drop_100
enum = @enum
@enum = Enumerator.new do |y|
100.times { enum.next }
loop { y << enum.next }
end
self
end
def solve(cmds)
cmds.each_char do |cmd|
case cmd
when "s" then drop_before(:square?)
when "S" then drop_after(:square?)
when "c" then drop_before(:cube?)
when "C" then drop_after(:cube?)
when "h" then drop_100
else drop_nth(cmd.to_i)
end
end
puts @enum.take(10).join(",")
end
private
def square?(n)
Math.sqrt(n).truncate**2 == n
end
def cube?(n)
Math.cbrt(n).truncate**3 == n
end
end
MultiStageSelection.solve("ss6cc24S")
MultiStageSelection.solve("4scC3hh982Cc5s")
Recommended Posts