70th Yokohama.rb どう書く http://nabetani.sakura.ne.jp/yokohamarb/2016.07.ront/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# とりあえず動けば良いバージョン | |
require "pry" | |
# U: 0, L: 1, R: 2, D: 3, Dead: 4 | |
class Tile | |
def initialize(table) | |
@table = table | |
end | |
def from(i) | |
@table[i] | |
end | |
end | |
TILES = [ | |
tile1 = Tile.new({ 0 => 1, 1 => 0, 2 => 3, 3 => 2}), | |
tile2 = Tile.new({ 0 => 2, 2 => 0, 1 => 3, 3 => 1}), | |
tile3 = Tile.new({ 0 => 3, 3 => 0, 1 => 2, 2 => 1}), | |
tile2 = Tile.new({ 0 => 4, 1 => 4, 2 => 4, 3 => 4}), | |
] | |
st = :a | |
PLACE_TO_CODE = [st] | |
15.times { PLACE_TO_CODE << st.succ; st = st.succ } | |
class Field | |
def initialize(field_str) | |
arr_field = field_str.split("/").map { |x| x.split(//) } | |
@field = [] | |
arr_field.each_with_index do |line, h| | |
@field[h] = [] | |
line.each_with_index do |x, w| | |
@field[h][w] = TILES[x.to_i] | |
end | |
end | |
end | |
def solve(h, w, d) | |
result = "" | |
loop do | |
break if h < 0 | |
break if w < 0 | |
break if h >= 4 | |
break if w >= 4 | |
result += PLACE_TO_CODE[h * 4 + w].to_s | |
nd = @field[h][w].from(d) | |
case nd | |
when 0 | |
h -= 1 | |
d = 3 | |
when 1 | |
w -= 1 | |
d = 2 | |
when 2 | |
w += 1 | |
d = 1 | |
when 3 | |
h += 1 | |
d = 0 | |
when 4 | |
break | |
end | |
end | |
result | |
end | |
end | |
#f = Field.new("0113/1201/2201/2100") | |
#p f.solve(0, 1, 0) | |
def test(str, ans) | |
f = Field.new(str) | |
if f.solve(0, 1, 0) == ans | |
warn "OK" | |
else | |
warn "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