Skip to content

Instantly share code, notes, and snippets.

@kymmt90 kymmt90/dokaku.rb
Created Jul 10, 2016

Embed
What would you like to do?
Yokohama.rb #70 どう書く
class Walker
Position = Struct.new(:x, :y)
MAP = [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j', 'k', 'l'], ['m', 'n', 'o', 'p']]
def initialize(tiles)
@current = Position.new(1, 0)
@previous = nil
@from = nil
@tiles = tiles.split('/').map { |l| l.split('') }
end
def walk
route = ''
loop do
break if out_of_map?
route += MAP[@current.y][@current.x]
break if dead_end?
@from = from
@previous = @current.dup
move(@tiles[@current.y][@current.x].to_i)
end
route
end
def out_of_map?
@current.x < 0 || @current.x > 3 || @current.y < 0 || @current.y > 3
end
def dead_end?
@tiles[@current.y][@current.x].to_i == 3
end
def from
return :above if @previous.nil?
diff_x = @current.x - @previous.x
return :left if diff_x > 0
return :right if diff_x < 0
diff_y = @current.y - @previous.y
return :above if diff_y > 0
return :below if diff_y < 0
nil
end
def move(tile_type)
case tile_type
when 0
@current.y -= 1 if @from == :left
@current.y += 1 if @from == :right
@current.x -= 1 if @from == :above
@current.x += 1 if @from == :below
when 1
@current.y += 1 if @from == :left
@current.y -= 1 if @from == :right
@current.x += 1 if @from == :above
@current.x -= 1 if @from == :below
when 2
@current.x += 1 if @from == :left
@current.x -= 1 if @from == :right
@current.y += 1 if @from == :above
@current.y -= 1 if @from == :below
end
end
end
def test(input, output)
walker = Walker.new(input)
if walker.walk == output
puts 'ok'
else
puts 'ng'
end
end
test( "0113/1201/2201/2100", "bcgfeabfjnoklpo" ) # 0
test( "2110/2013/2210/0122", "bcgh" ) # 1
test( "2222/2130/2121/2002", "bfg" ) # 2
test( "0021/2212/2102/1220", "baeimnoplhdcbfjkgfe" ) # 3
test( "0213/1221/0220/1103", "bfjnokgcbaefghlkjimn" ) # 4
test( "3201/3120/3333/3333", "bfghdcgk" ) # 5
test( "3233/3233/3133/3333", "bfjk" ) # 6
test( "3333/3333/3333/3333", "b" ) # 7
test( "1212/1201/2123/2220", "bfjkl" ) # 8
test( "2212/3102/1002/2100", "bfgcba" ) # 9
test( "0023/2221/1102/0031", "baeijnm" ) # 10
test( "1121/3120/0212/1120", "bcdhgfba" ) # 11
test( "2202/3211/2120/1210", "bfjklhgcd" ) # 12
test( "3201/3211/1111/0100", "bfjkonjie" ) # 13
test( "0121/1120/1111/1211", "bcdhgfbaefjkop" ) # 14
test( "1212/1213/2103/0213", "bfjkgfea" ) # 15
test( "1121/2212/1323/3031", "bcdhl" ) # 16
test( "0030/1230/1121/0031", "baefg" ) # 17
test( "2223/1211/0002/1200", "bfjimnokl" ) # 18
test( "3210/0033/0201/0130", "bfei" ) # 19
test( "0213/2220/0021/3002", "bfjim" ) # 20
test( "2121/2112/1110/1010", "bcdhlkgfba" ) # 21
test( "0113/1003/2303/2220", "bcgfj" ) # 22
test( "2202/1110/1302/0313", "bfgkj" ) # 23
test( "1211/3202/2102/0222", "bfjkgh" ) # 24
test( "3113/0002/0112/1022", "bcgfjko" ) # 25
test( "1200/3000/0121/0121", "bfe" ) # 26
test( "2221/1122/1031/2200", "bfgh" ) # 27
test( "1202/0121/0222/1300", "bfghlpo" ) # 28
test( "0002/1012/1021/3300", "baefbc" ) # 29
test( "0211/1200/2220/2103", "bfjnokghdc" ) # 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.