第12回オフラインリアルタイムどう書くの参考問題をRubyで解きました。
問題はこちらのリンクから。
http://qiita.com/Nabetani/items/1de39df381dfeee305ab
CHARS1=[
('A'..'K').to_a,
('L'..'V').to_a,
('W'..'Z').to_a+('a'..'g').to_a
]
CHARS2=[
'52zwtqnkh'.split(''),
'630xuroli'.split(''),
'741yvspmj'.split('')
]
def make_map
map = {}
[CHARS1,CHARS2].each{|chars|
chars.each_with_index{|row, i|
row.each_with_index{|c, j|
map[c] = []
if j > 0
nc = chars[i][j-1]
map[nc][0] = c
map[c][2] = nc
end
if i > 0
nc = chars[i-1][j]
map[nc][1] = c
map[c][3] = nc
end
}
}
}
3.times{|i|
c = CHARS1[2][-1-i]
nc = CHARS2[i][0]
map[c][1] = nc+'L'
map[nc][2] = c+'R'
c = CHARS2[i][-1]
nc = CHARS1[2][i]
map[c][0] = nc+'L'
map[nc][1] = c+'R'
}
map
end
def rotate(dir,c)
if (c == 'L')
dir -= 1
dir = 3 if dir < 0
else
dir += 1
dir = 0 if dir > 3
end
dir
end
def solve(q)
map = make_map
seq = "A"
cur = "A"
dir = 0
q.chars.each{|c|
move = c.to_i(16)
if move == 0
dir = rotate(dir,c)
else
move.times{
cur = map[cur][dir]
return seq + '?' if cur.nil?
if cur.size > 1
dir = rotate(dir, cur[1])
cur = cur[0]
end
seq += cur
}
end
}
seq
end
DATA.readlines.each do |line|
no,q,a = line.chop.split(/\s+/)
ans = solve(q)
print no + "\t" + ans
puts ans == a ? ' o' : ' x'
end
__END__
0 2RcL3LL22 ABCNYjmpsvy147edcbcdef
1 L3R4L5RR5R3L5 A?
2 2ReLLe ABCNYjmpsvy147eTITe741yvspmjYNC
3 1ReRRe ABMXilorux036fUJUf630xuroliXMB