[RUBY] Y-shaped road tour (how to write)

http://nabetani.sakura.ne.jp/hena/ord3ynode/

When A Graph is given a continuous instruction of "turn right/go forward/turn left/go back", it is a problem to output a column of passing nodes.

Ruby


module YshapedRoadTour
  Graph = {"A"=>"DCB", "B"=>"CEA", "C"=>"FBA",
           "D"=>"EFA", "E"=>"DBF", "F"=>"ECD"}
           
  def self.solve(input)
    input = input.each_char
    result = ""
    
    go = ->(s, e) {
      result << e
      idx = Graph[e].index(s)
      case input.next
      when "b"
        go.(e, s)
      when "r"
        i = (idx - 1) % 3
      when "l"
        i = (idx + 1) % 3
      end
      go.(e, Graph[e][i])
    }
    go.("B", "A")
  rescue StopIteration
    result
  end
end


if __FILE__ == $0
  require 'minitest/autorun'
  describe 'YshapedRoadTour' do
    [
      ["b", "AB"],
      ["l", "AD"],
      ["r", "AC"],
      ["bbb", "ABAB"],
      ["rrr", "ACBA"],
      ["blll", "ABCAB"],
      ["llll", "ADEBA"],
      ["rbrl", "ACADE"],
      ["brrrr", "ABEDAB"],
      ["llrrr", "ADEFDE"],
      ["lrlll", "ADFEDF"],
      ["lrrrr", "ADFCAD"],
      ["rllll", "ACFDAC"],
      ["blrrrr", "ABCFEBC"],
      ["brllll", "ABEFCBE"],
      ["bbbrrlrl", "ABABEDFCB"],
      ["rbllrrrr", "ACABCFEBC"],
      ["lbrlrrblr", "ADABCFEFCA"],
      ["rlbrrrrbl", "ACFCADFCFD"],
      ["bllrlrbrrb", "ABCADEFEBCB"],
      ["rllrllllbb", "ACFDEBADEDE"],
      ["blblrlrrlbr", "ABCBEDFCABAC"],
      ["lrlrrrrrbrb", "ADFEBCFEBEDE"],
      ["rblllrlrrlrr", "ACABCADEFDABE"],
      ["rbrrlrblrllb", "ACADFEBEFDACA"],
      ["lrrrlrllrrllr", "ADFCABEFCADEBC"],
      ["rrlblllrrlrrb", "ACBEBADEFDABEB"],
      ["brbllrrbbrlrll", "ABEBADFCFCABEFC"],
      ["rrrbbrlbrlblrb", "ACBABACFCABADFD"],
      ["lllllllllllblrr", "ADEBADEBADEBEFDE"],
      ["llllllrllrlbrrr", "ADEBADEFCBADABED"]
    ].each do |input, expect|
      it input do
        assert_equal YshapedRoadTour.solve(input), expect 
      end
    end
  end
end

It's difficult to express a right turn or a left turn. Here, the graph is represented by Hash, but if it is {"A "=>" DCB "}, it means that it is D, C, B clockwise around node A.

Input is converted to Enumerator with each_char and extracted character by character with next. When there is nothing to retrieve, an exception StopIteration will occur, so I try to catch it with rescue and return the result.

go. (S, e) means going from s to e.

I'm using recursion here, but I think loops are better. But I somehow like recursion (laughs).

Loop version

Is it like this with a loop? After all this is better (laughs).

module YshapedRoadTour
  Graph = {"A"=>"DCB", "B"=>"CEA", "C"=>"FBA",
           "D"=>"EFA", "E"=>"DBF", "F"=>"ECD"}
           
  def self.solve(input)
    input = input.each_char
    result = ""
    s, e = "B", "A"
    
    loop do
      result << e
      idx = Graph[e].index(s)
      case input.next
      when "b"
        s, e = e, s
      when "r"
        i = (idx - 1) % 3
        s, e = e, Graph[e][i]
      when "l"
        i = (idx + 1) % 3
        s, e = e, Graph[e][i]
      end
    end
    result
  end
end

Note that loop do ~ end catches the exception StopIteration, so Enumerator # next can be written straightforwardly.

Recommended Posts

Y-shaped road tour (how to write)
How to write Rails
How to write dockerfile
How to write docker-compose
How to write Mockito
How to write migrationfile
How to write good code
Bit Tetris (how to write)
How to write java comments
Great poor (how to write)
How to write Junit 5 organized
How to write Rails validation
How to write Rails seed
[Ruby] How to write blocks
How to write Rails routing
Studying Java # 6 (How to write blocks)
[Rails] How to write in Japanese
How to write a ternary operator
Rails on Tiles (how to write)
[Rails] How to write exception handling?
How to write Java variable declaration
How to write easy-to-understand code [Summary 3]
[RSpec] How to write test code
[Basic] How to write a Dockerfile Self-learning ②
[Introduction to Java] How to write a Java program
[Java] How to output and write files!
How to write Spring AOP pointcut specifier
How to write an RSpec controller test
[SpringBoot] How to write a controller test
How to write and explain Dockerfile, docker-compose
How to deploy
JDBC promises and examples of how to write
Rails: How to write a rake task nicely
[JavaFX] How to write Eclipse permissions in build.gradle
How to write offline 15th reference question answer
[Rails] How to write when making a subquery
Java Development Basics ~ How to Write Programs * Exercise 1 ~
How to write an if statement to improve readability-java
JUnit 5: How to write test cases in enum
How to write test code with Basic authentication
How to write React Native bridge ~ Android version ~
[Java] Memo on how to write the source
How to write Java String # getBytes in Kotlin?
Notes on how to write comments in English
Javaer tries to summarize how to write properties in C #
How to call AmazonSQSAsync
[Ruby on Rails] How to write enum in Japanese
How to use Map
How to use rbenv
How to write Scala from the perspective of Java
[Java] Types of comments and how to write them
How to write a unit test for Spring Boot 2
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
java: How to write a generic type list [Note]
How to use map
[Even beginners can do it! ] How to write Javadoc
How to write to apply gem Pagy (pagination) to an array
How to use collection_select