問題はこちら:
オフラインリアルタイムで書いた私の実装はこちら:
鍋谷さんの実装がこちら:
パクって インスパイアされて書き直したものがこちら:
orde15nohil-2.rb
# see http://nabetani.sakura.ne.jp/hena/orde15nohil/
def to_xy(i)
s = i.to_s(3)
[s.tr('2', '0').to_i(2), s.tr('12', '01').to_i(2)]
end
def to_num(x, y)
return nil if ((x & y) != 0) || (x < 0) || (y < 0)
(x.to_s(2).to_i + y.to_s(2).tr('1', '2').to_i).to_s.to_i(3)
end
def solve(input)
x, y = to_xy(input.to_i)
[to_num(x, y - 1), to_num(x - 1, y), to_num(x + 1, y), to_num(x, y + 1)].compact.join(',')
end
def test(n, input, expected)
actual = solve(input)
print "#{format('%2d', n)}: "
if actual == expected
puts "\x1b[32mpassed\x1b[0m"
else
puts "\x1b[31mfailed input: #{input}, expected: #{expected}, actual: #{actual}\x1b[0m"
end
end
DATA.read.split("\n").each do |line|
n, input, expected = line.split
test(n, input, expected)
end
__END__
0 21 19,22,23
1 0 1,2
2 1 0,3
3 2 0,6
(テストデータ省略)