I tried to solve the problem of "multi-stage selection" with Ruby

problem

24th Offline Real-time Writing Problem

Reference (Clojure version)

I solved it in Clojure earlier. https://qiita.com/kts_h/items/64bb74fdcef642198bc5

Ruby version

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 insolve` 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

I tried to solve the problem of "multi-stage selection" with Ruby
[Beginner's point of view] I tried to solve the FizzBuzz problem "easily" with Ruby!
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
I tried to solve the problem of Google Tech Dev Guide
I tried to solve the Ruby karaoke machine problem (there is an example of the answer)
I tried to solve the Ruby bonus drink problem (there is an example of the answer)
I tried to solve the Ruby bingo card creation problem (there is an example of the answer)
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
I tried to summarize the basic grammar of Ruby briefly
I tried to build the environment of PlantUML Server with Docker
I tried to check the operation of gRPC server with grpcurl
I tried to solve the paiza campaign problem "Challenge from Kaito 813"
[Competition Pro] Solve the knapsack problem with Ruby
I checked the number of taxis with Ruby
[Ruby] I tried to diet the if statement code with the ternary operator
I tried to make full use of the CPU core in Ruby
I tried to visualize the access of Lambda → Athena with AWS X-Ray
I tried to measure and compare the speed of GraalVM with JMH
I tried DI with Ruby
I tried to summarize the state transition of docker
[Java] I tried to solve Paiza's B rank problem
05. I tried to stub the source of Spring Boot
I tried to compare the infrastructure technology of engineers these days with cooking.
I tried to reduce the capacity of Spring Boot
[At Coder] Solve the ABC183 D problem with Ruby
I tried to get the distance from the address string to the nearest station with ruby
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
I tried the FizzBuzz problem
[At Coder] Solve the ABC182 D problem with Ruby
I tried to check the operation of http request (Put) with Talented API Tester
I tried to investigate the mechanism of Emscripten by using it with the Sudoku solver
I tried to increase the processing speed with spiritual engineering
I tried to summarize the basics of kotlin and java
[Swift] I tried to implement the function of the vending machine
I tried to automate LibreOffice Calc with Ruby + PyCall.rb (Ubuntu 18.04)
I tried to build the environment of WSL2 + Docker + VSCode
[Ruby] I want to reverse the order of the hash table
I tried upgrading from CentOS 6.5 to CentOS 7 with the upgrade tool
I tried to interact with Java
I tried to explain the method
Sazae-san's rock-paper-scissors I tried to verify the theoretical value and the measured value of the probability of the same hand 5 consecutive times with Ruby
[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]
I tried to implement the image preview function with Rails / jQuery
I tried to reimplement Ruby Float (arg, exception: true) with builtin
I tried to summarize the methods of Java String and StringBuilder
I want to change the value of Attribute in Selenium of Ruby
I tried a calendar problem in Ruby
I tried to summarize the methods used
I tried to get started with WebAssembly
I tried to express the result of before and after of Date class with a number line
I tried to solve AOJ's Binary Search
I tried to implement the Iterator pattern
I tried to take a look at the flow of Android development environment construction with Android Studio
I tried to summarize the Stream API
I tried to build Ruby 3.0.0 from source
I tried to implement ModanShogi with Kinx
[Beginner] Let's solve AtCoder problem with Ruby while looking at the article!
I tried to make a parent class of a value object in Ruby
I tried to summarize the key points of gRPC design and development
[Ruby] I want to extract only the value of the hash and only the key
How to solve the local environment construction of Ruby on Rails (MAC)!