0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

第15回オフラインリアルタイムどう書く rubyで

Last updated at Posted at 2013-11-01

オフラインリアルタイムどう書く
第15回(11月1日) http://atnd.org/events/43825
の、問題「異星の電光掲示板」
http://nabetani.sakura.ne.jp/hena/ord15elebubo/
の実装例。

rubyで。

他の言語などの解答例は
http://qiita.com/Nabetani/items/cba03c96d1ea55f6e861
から辿れます。

ちなみに16回
http://atnd.org/events/45016
も参加者募集中。

def solve( src )
  t,b = src.split("/").map{ |i| ( "%032b" % i.to_i(16) ).chars }
  m=t.zip(b).map{ |i| i.join.to_i(2) }.join
    .gsub( "21", "201" )
    .gsub( "12", "102" )
  m.split( /0+/ ).map{ |i|
    { ""=>"", "31"=>"L", "32"=>"R", "13"=>"J", "232"=>"T",
      "313"=>"U", "323"=>"N", "132"=>"S", "231"=>"Z" }[ i ]  || ( raise i ) }.join
end

$stdout.sync=true

DATA.all? do |line|
  num, src, expected = line.split( /\s+/ )
  next true unless expected
  actual = solve( src )
  ok = actual==expected;
  puts "%s %s->%s ( %s )" % [ ok ? "ok" : "***NG***", src, actual, expected ]
  ok
end.tap{ |ok| p ok }

__END__
0 2ed8aeed/34b0ea5b LTRSUNTSJ 
1 00000200/00000300 L 
2 00018000/00010000 R

例によってテストデータの大半は省略。

で。
最初に書いた実装。

  1. zip と to_i(2) なんかをつかって上段と下段を一緒くたにした文字列を作る。
  2. L のすぐ後に T のようになった場合に文字の境目がわかりにくくなるので、そういう場合にゼロを挟む。
  3. 1個以上のゼロで split して、パターンと文字の対応表を引く

で完成。

今見ると、split して map するより gsub が良かったかも。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?