どう書く

オフラインリアルタイムどう書くF09の回答

40分ほどで
配列をshift/pop・push/unshiftさせるかを入力から変換しています。

f09.rb```
def parse( c )
  case c
  when /[a-c]/
    [ false, c.ord - 'a'.ord, :shift, :push, ]
  when /[d-f]/
    [ true, c.ord - 'd'.ord, :pop, :unshift, ]
  when /[g-i]/
    [ false, 2 - (c.ord - 'g'.ord), :pop, :unshift, ]
  when /[j-l]/
    [ true, 2 - (c.ord - 'j'.ord), :shift, :push, ]
  end
end

def solve( input )
  board = (1..9).each_slice(3).to_a
  input.chars { |c|
    is_trans, x, g, s = parse(c)
    board = board.transpose if is_trans
    temp = board[x].send(g)
    board[x].send(s, temp)
    board = board.transpose if is_trans
  }
  board.map(&:join).join('/')
end

def test( no, input, expected )
  actual = solve( input )
  if expected != actual
    puts '[%d] NG: %s, %s, %s' % [ no, input, expected, actual, ]
    exit 1
  else
    puts '[%d] OK' % no
  end
end

test( 0, "aegj", "286/435/971" );
test( 1, "a", "231/456/789" );
test( 2, "e", "183/426/759" );
test( 3, "g", "123/456/978" );
test( 4, "j", "126/459/783" );
test( 5, "bb", "123/645/789" );
test( 6, "jjj", "123/456/789" );
test( 7, "bd", "723/164/589" );
test( 8, "ah", "231/645/789" );
test( 9, "bj", "124/569/783" );
test( 10, "db", "723/561/489" );
test( 11, "dh", "723/615/489" );
test( 12, "dl", "123/456/789" );
test( 13, "hc", "123/645/897" );
test( 14, "gf", "128/453/976" );
test( 15, "hl", "623/745/189" );
test( 16, "ja", "261/459/783" );
test( 17, "ld", "123/456/789" );
test( 18, "ki", "315/486/729" );
test( 19, "lfa", "294/753/186" );
test( 20, "kga", "531/486/972" );
test( 21, "dbi", "372/561/489" );
test( 22, "che", "193/625/847" );
test( 23, "iea", "823/416/759" );
test( 24, "gbl", "523/964/178" );
test( 25, "egj", "186/425/973" );
test( 26, "jcf", "127/456/839" );
test( 27, "djh", "726/915/483" );
test( 28, "hld", "123/645/789" );
test( 29, "leeh", "453/678/129" );
test( 30, "heja", "851/629/743" );
test( 31, "cakh", "251/649/837" );
test( 32, "bhjik", "652/489/713" );
test( 33, "eabji", "483/269/751" );
test( 34, "cdbch", "823/156/974" );
test( 35, "ckgajc", "536/492/817" );
test( 36, "ggchha", "231/564/978" );
test( 37, "gfbkeg", "128/534/697" );
test( 38, "agfbcbf", "239/148/765" );
test( 39, "ekahijf", "123/645/789" );
test( 40, "hajdjbe", "789/432/615" );
test( 41, "elgililj", "976/325/814" );
test( 42, "chffefif", "317/629/845" );
test( 43, "ilbbihak", "462/587/319" );
test( 44, "abcdefghijkl", "123/456/789" );
test( 45, "hkijbglfaced", "768/125/493" );
test( 46, "dfkbjiechlga", "256/387/419" );
test( 47, "hgfkbidlajce", "186/745/239" );
test( 48, "baciefjhgkdl", "153/482/796" );