[RUBY] Rails on Tiles (how to write)

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

The problem is finding a route that goes out of range when moving along the rails of a given tile.

Ruby


module RailsOnTiles
  Tiles = [[2, 3, 0, 1], [1, 0, 3, 2], [3, 2, 1, 0]]
  Dirs = [[0, -1], [1, 0], [0, 1], [-1, 0]]
  module_function
  
  def go(input)
    field = input.chars.map(&:to_i).each_slice(3).to_a
    solve(field).map { |i| ("A".."I").to_a[i] }.join
  end
  
  def solve(field, x=1, y=-1, dir=2, route=[])
    dx, dy = Dirs[dir]
    x += dx
    y += dy
    return route if x < 0 || x > 2 || y < 0 || y > 2
    rev = Tiles[0][dir]
    nxt = Tiles[field[y][x]][rev]
    tile_num = y * 3 + x
    solve(field, x, y, nxt, route + [tile_num])
  end
end


if __FILE__ == $0
  require 'minitest/autorun'
  describe 'RailsOnTiles' do
    [
      ["101221102", "BEDGHIFEH"],
      ["000000000", "BEH"],
      ["111111111", "BCF"],
      ["222222222", "BAD"],
      ["000211112", "BEFIHEDGH"],
      ["221011102", "BADGHIFEBCF"],
      ["201100112", "BEHIFCBADEF"],
      ["000111222", "BEFIH"],
      ["012012012", "BC"],
      ["201120111", "BEDABCFI"],
      ["220111122", "BADEHGD"],
      ["221011022", "BADG"],
      ["111000112", "BCFIHEBA"],
      ["001211001", "BEFI"],
      ["111222012", "BCFEHIF"],
      ["220111211", "BADEHI"],
      ["211212212", "BCFEBAD"],
      ["002112210", "BEFC"],
      ["001010221", "BEF"],
      ["100211002", "BEFIHG"],
      ["201212121", "BEFCBAD"]
    ].each do |input, expect|
      it input do
        assert_equal RailsOnTiles.go(input), expect 
      end
    end
  end
end

field represents the arrangement of tiles, and the tile type is stored in Tiles. As for the direction type, 0,1,2,3 represent the upper right lower left respectively. If you go down from the previous tile, the next tile will "reverse" so that it comes from the top, so it goes into rev. After that, it recurses until it goes out of field and returns the result.

In addition, since the route route represents the position of the tile with a number, it is converted to the alphabet at the end.

Recommended Posts

Rails on Tiles (how to write)
How to write Rails
How to write Rails validation
How to write Rails seed
How to write Rails routing
How to deploy jQuery on Rails
[Ruby on Rails] How to write enum in Japanese
How to use Ruby on Rails
How to deploy Bootstrap on Rails
[Rails] How to write exception handling?
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
How to write dockerfile
How to write docker-compose
How to write Mockito
How to write migrationfile
[RSpec on Rails] How to write test code for beginners by beginners
Rails: How to write a rake task nicely
[Rails] How to write when making a subquery
[Ruby on Rails] How to display error messages
How to add / remove Ruby on Rails columns
[Rails MySQL] How to reset DB on heroku
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
[Java] Memo on how to write the source
Notes on how to write comments in English
How to write good code
[Rails] How to use enum
How to write java comments
How to read rails routes
[Refactoring] How to write routing
[Note] How to write Dockerfile/docker-compose.yml
How to use rails join
How to write Junit 5 organized
How to terminate rails server
[Rails] How to use validation
[Ruby] How to write blocks
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
[Ruby On Rails] How to reset DB in Heroku
How to write a date comparison search in Rails
(Ruby on Rails6) How to create models and tables
[Rails] How to use gem "devise"
[Rails] How to install Font Awesome
[Rails] How to use devise (Note)
How to deploy Laravel on CentOS 7
[Rails] Two ways to write form_with
[Rails] How to use flash messages
[rails] How to display db information
Studying Java # 6 (How to write blocks)
How to "hollow" View on Android
How to write a migration from Rails datetime type to date type
[Rails] How to prevent screen transition
How to install ImageMagick on Windows 10