1
1

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.

ฅ(・ω・ฅ){ Orenono gem と banner コマンドを利用して Brainf_ck を意識せずに Brainf_ck の AA を描く } #orenono

Last updated at Posted at 2015-02-16

:neckbeard: 概要

Orenono gem と banner コマンドを利用して Brainf_ck を意識せずに Brainf_ck の AA を描く

:question: orenono gem って何?

「また、 Brainf__k 派生か。もう飽き飽きだ。 :unamused: 」という意見はごもっともですが、
orenono gem はちょっと毛色が違います。

この gem を利用すると Brainf_ck 派生言語を Ruby の DSL だけで作れます。
わざわざ派生言語を作る価値が下がるのです。
つまり、量産される Brainf_ck 派生の価値を下げる gem です。

詳細はこちらをご確認ください。

:scroll: 仕様

以下の文字列を出力する Brainf_ck のコードを AA で出力します。
以下の分は :baby_chick: esa.io の説明文から拝借しました。

"Everything is not perfect from the beginning. We want to support the concept of information. Done is better than perfect. Then, update it again and again, shaping it up. After it has been nearly completed, they should be organized and categorized. "

上記の文字列を Brainf_ck のコードに置き換えた後に、以下の AA にあてはめます。
一応、トリのつもりです。

(\('>')/)

ちなみに esa.io の トリ AA 一覧 があります。
今回は、 banner コマンドできれいに出力できるように自分で絵文字を決めました。

手順

:neckbeard: banner コマンドで AA を作成

この AA のひな型に後程 Brainf_ck のコードを流し込みます。
出力結果をクリップボードにコピーしておき、後程 AA 生成用のソースコードに張り付けます。
(この部分のロジック作成はさぼります)

$ banner "(\('>')/)"
                                                                                
                                                                                
     X               X      X               X      X               X            
    X    X          X       X               X       X          X    X           
    X     X         X       X      X        X       X         X     X           
   X       X       X                X                X       X       X          
   X        X      X                 X               X      X        X          
   X         X     X                  X              X     X         X          
   X          X    X                 X               X    X          X          
   X           X   X                X                X   X           X          
    X               X              X                X               X           
    X               X                               X               X           
     X               X                             X               X            

:neckbeard: 出力文字列をもとに Brainf_ck のコードを生成する

orenono gem には Brainf_ck のコード生成機能があるので、それを利用します

$ orenono generate "Everything is not perfect from the beginning. We want to support the concept of information. Done is better than perfect. Then, update it again and again, shaping it up. After it has been nearly completed, they should be organized and categorized." > esa_service.bf

こんなコードが生成されます。

++++++++++[>++++++<-]>+++++++++.+++++++++++++++++++++++++++++++++++++++++++++++++.-----------------.+++++++++++++.+++++++.-----.------------.+.+++++.-------.-----------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++.-----------------------------------------------------------------------------------.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+.+++++.------------------------------------------------------------------------------------.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-----------.+++++++++++++.------------.-.--.+++++++++++++++++.------------------------------------------------------------------------------------.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++.---.--.-----------------------------------------------------------------------------.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.------------.---.---------------------------------------------------------------------.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++.++.++.+++++..-----.+++++.-------.---------------------------------------------------------.--------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++.---------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.----------------------.+++++++++++++.++++++.------------------------------------------------------------------------------------.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-----.-------------------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++.-----..-.+++.++.------------------------------------------------------------------------------------.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.------------.---.---------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++.-.-----------.++.+++++++++++.++++.------------------------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.---------.----------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++.--------.+++++++++.+++.-----.------------.+++++++++++++++++++.-----------.++++++.-.----------------------------------------------------------------.--------------.++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++++++++++++++++.-.---------.---------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++.-----------------------------------------------------------------------------------.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++.+++++++++++++++..---------------.+++++++++++++.----------------------------------------------------------------------------------.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.------------.-------.+++++++++++++.------------------------------------------------------------------------------.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-----------.+++++++++++++.------------.-.--.+++++++++++++++++.----------------------------------------------------------------------.--------------.++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++.---.+++++++++.------------------------------------------------------------------.------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-----.------------.---.+++++++++++++++++++.---------------.---------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++.------------------------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++.------.++++++++.+++++.------------------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++.----------.--------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++.------.++++++++.+++++.------------------------------------------------------------------.------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-----------.-------.+++++++++++++++.-------.+++++.-------.-----------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++.------------------------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-----.------------------------------------------------------------------.--------------.+++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++++++++++.++++++++++++++.---------------.+++++++++++++.----------------------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++.------------------------------------------------------------------------------------.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-------.++++++++++++++++++.-----------------------------------------------------------------------------------.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++..+++++++++.------------------------------------------------------------------------------.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.---------.----.+++++++++++++++++.------.+++++++++++++.-----------------------------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++.--.+++.----.-------.+++++++++++++++.---------------.-.--------------------------------------------------------.------------.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.------------.---.++++++++++++++++++++.-----------------------------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.-----------.+++++++.++++++.---------.--------.--------------------------------------------------------------------.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++.---------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++.-----------.------.+++++++++++++.-----.+++++++++++++++++.---------------------.-.--------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++.----------.--------------------------------------------------------------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.--.+++++++++++++++++++.---------------.++.++++++++.+++.---------.+++++++++++++++++.---------------------.-.------------------------------------------------------.

:neckbeard: 変換コードを実行

:recycle: 変換コード: bf_aa_converter.rb

変換コードです。

  • banner で生成した文字列を 2 進数に置き換える
    __END__ 以下は、事前に banner で作成しておいた AA のひな型です。
  • AA のテンプレートの文字列を配列化し、1文字ずつ Brainf_ck のコードを埋め込んでいきます
  • Brainf_ck のコードを使い切ったら、AA が中途半端にならないようにダミーの文字を埋めます
bf_aa_converter.rb
template = DATA.read.gsub("\n", '')
bit_template = template.chars.map { |e|e == 'X' ? 1 : 0 }

answer_bf = File.read('esa_service.bf')
answer_bf_bytes = answer_bf.chars

dummy = '='
results = []
last = false
loop {
  bit_template.each do |char|
    c = (char == 0) ? ' ' : last ? dummy : answer_bf_bytes.shift
    results << c
    last = true if answer_bf_bytes.size == 0
  end
  break if last
}

print results.each_slice(81).map { |e|e.join + "\n" }.join

__END__
                                                                                
     X               X      X               X      X               X            
    X    X          X       X               X       X          X    X           
    X     X         X       X      X        X       X         X     X           
   X       X       X                X                X       X       X          
   X        X      X                 X               X      X        X          
   X         X     X                  X              X     X         X          
   X          X    X                 X               X    X          X          
   X           X   X                X                X   X           X          
    X               X              X                X               X           
    X               X                               X               X           
     X               X                             X               X            

:runner: 実行

$ ruby bf_aa_converter.rb > aa_esa_service.bf

出力 (aa_esa_service.bf)

こんな トリ AA の Brainf_ck のコードが完成しました。

aa_esa_service.bf
                                                                                
     +               +      +               +      +               +            
    +    +          +       +               [       >          +    +           
    +     +         +       +      <        -       ]         >     +           
   +       +       +                +                +       +       +          
   +        .      +                 +               +      +        +          
   +         +     +                  +              +     +         +          
   +          +    +                 +               +    +          +          
   +           +   +                +                +   +           +          
    +               +              +                +               +           
    +               +                               +               +           
     +               +                             +               +            
# 非常に長いので省略します
     -               -      -               -      -               -            
    -    -          -       -               -       -          -    -           
    -     -         -       -      -        -       -         -     -           
   -       -       -                -                -       -       -          
   -        -      -                 -               -      -        -          
   -         -     -                  -              -     -         -          
   .          =    =                 =               =    =          =          
   =           =   =                =                =   =           =          
    =               =              =                =               =           
    =               =                               =               =           
     =               =                             =               =            

:neckbeard: 完成した Brainf_ck を orenono gem で実行してみます

% orenono e aa_esa_service.bf
Everything is not perfect from the beginning. We want to support the concept of information. Done is better than perfect. Then, update it again and again, shaping it up. After it has been nearly completed, they should be organized and categorized.
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?