Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Yokohama.rb 70回で出題された問題の回答です。
// coding: utf-8
// http://nabetani.sakura.ne.jp/yokohamarb/2016.07.ront/
Tiles = [
{top: :left, left: :top, right: :down, down: :right},
{top: :right, left: :down, right: :top, down: :left},
{top: :down, left: :right, right: :left, down: :top},
{top: :death, left: :death, right: :death, down: :death}
]
def input2tiles(input)
array_field = input.split("/").map { |e| e.split(//) }
field = []
array_field.each_with_index do |row, i|
field[i] = []
row.each_with_index do |col, j|
field[i][j] = Tiles[col.to_i]
end
end
field
end
def position2char(row, col)
('a'.ord + (4 * row + col)).chr
end
def in_area?(row, col)
0 <= row && row <= 3 && 0 <= col && col <= 3
end
def solve(input)
field = input2tiles(input)
row_position = 0
col_position = 1
face = :top
answer = "b"
begin
case field[row_position][col_position][face]
when :top
row_position -= 1
face = :down
when :left
col_position -= 1
face = :right
when :right
col_position += 1
face = :left
when :down
row_position += 1
face = :top
when :death
break
end
if in_area?(row_position, col_position)
answer << position2char(row_position, col_position)
else
break
end
end while true
answer
end
def test(input, expect)
if solve(input) != expect
raise "Test failed. input = #{input} expect = #{expect} actual = #{solve(input)}"
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