非表示のステレオグラムメッセージ


29

テキストの段落、空の行、非表示のメッセージを含む入力文字列に従ってテキストステレオグラムを生成する必要があります。結果は1組の段落として表示されます。1つは異なる間隔であり、立体視で見たときに効果が上がる(説明はここにあります)。

入力:

I invented vegetarianism.  It is a diet involving no meat, just vegetables.  It is also common in cows - they are awesome.

vegetarianism. is awesome.

出力:

I      invented    I      invented
 vegetarianism.    vegetarianism. 
It   is a  diet    It  is  a  diet
involving    no    involving    no
meat,      just    meat,      just
vegetables.  It    vegetables.  It
is  also common    is  also common
in cows  - they    in cows  - they
are    awesome.    are   awesome. 

ボーナス

  • ユーザー入力として平行または十字のいずれかを選択するオプションを追加(-20)
  • ユーザー入力として調整可能な列幅(-50)

これはコードゴルフなので、ボーナスが獲得された後の最短コードです。


3
テキスト付きのステレオグラムを見るのは初めてです。これはすごい。
マイケルM. 14年

彼らは私のために動作しません:/(私は立体視を阻害する目の問題があります)
デイビッドウィルキンス14年

私はちょうどそれを見ました...これは印象的です!
WallyWest 14年

@Glenn Randers-Pehrsonは、[sorting]タグを追加した理由を説明してもらえますか?
user12205 14年

それは間違いでした。私は別の質問をしていると思ったので、キャンセルする方法を見つけることができません。承認されたら、ロールバックします。
グレンランダース-パーソン14年

回答:


1

Bash、sed: 228 223 197 (242-70)= 172

c=${5:-=};R=$c;L=;for f in r l;do
e="sed -e ";$e"$ d;s/$\|  */ \n/g" $1>m
o=1;for w in `$e"$ p;d" $1`;do
$e"$o,/^$w /s/^$w /$L$w$R /" m>n;o="/$c/"
cp n m;done;tr -d \\n<n|fold -sw${2:-35}|$e"s/$c/ /g">$f
L=$c;R=;done;pr -tmw${3:-80} ${4:-l r}

スクリプトが「ステレオ」と呼ばれる実行可能ファイルにある場合は、次を入力します。

stereo file.in [column_width [page_width ["r l"]]]

column_widthは数値です。25〜45が機能し、デフォルトは35です。

page_widthは数値で、column_widthの約2倍で、デフォルトは80です

寄り目で見るには、4番目の引数として「r l」を使用します。デフォルトは「l r」で、並列表示用に設定されています。

編集:ファイルを1行に1ワードに分割して書き直し、最後に再構築します。注:独自の使用のために「=」記号を予約します。入力ファイル内の「=」記号は空白になります。

編集:メッセージに「=」記号が含まれている場合、5番目のパラメーターとして指定することで、スクリプトが使用する別の記号を選択できます。

入力:vegetarianism.txt:

I invented vegetarianism.  It is a diet involving no meat, just
vegetables.  It is also common in cows - they are awesome.

vegetarianism. is awesome.

結果

./stereo vegetarianism.txt 32 72 "l r":| 展開(内部作業シンボルにコロンを使用)

I invented  vegetarianism. It       I invented vegetarianism.  It
 is a diet involving no meat,       is  a diet involving no meat,
just vegetables. It is also         just vegetables. It is also
common in cows - they are           common in cows - they are
 awesome.                           awesome.

./stereo washington.txt 35 75 "l r" |展開

In a little district west of          In a little district west of
 Washington Square the streets        Washington  Square the streets
have run crazy and broken             have run crazy and broken
themselves into small strips          themselves into small strips
called 'places'. These 'places'       called 'places'. These 'places'
make strange angles and curves.       make strange angles and curves.
One Street crosses itself a time      One Street crosses itself a time
or two. An artist once discovered     or two. An artist once discovered
a valuable possibility in this        a valuable possibility in this
street. Suppose a collector with a    street. Suppose a collector with a
bill for  paints, paper and canvas    bill for paints,  paper and canvas
should, in traversing this route,     should, in traversing this route,
suddenly meet  himself coming         suddenly meet himself  coming
back, without a cent having been      back, without a cent having been
paid on account!                      paid on account!

「| expand」は必要ありませんが、出力を4桁シフトすると、TABが正しく処理されません。7バイトのコストでスクリプトに挿入できます。

ImageMagickバリエーション

最後の行をテキストからイメージへのImageMagickコマンドで置き換える:

c=${6:-=};R=$c;L=;for f in r l;do
e="sed -e ";$e"$ d;s/$\|  */ \n/g" $1>m
o=1;for w in `$e"$ p;d" $1`;do
$e"$o,/^$w /s/^$w /$L$w$R /" m>n;o="/$c/"
cp n m;done;tr -d \\n<n|fold -sw${2:-35}|$e"s/$c/ /g">$f
L=$c;R=;done;
convert -border 10x30 label:@${4:-l} label:@${5:-r} +append show:

この1つでは、寄り目と並列表示の「r」と「l」は別々の引数です。

./im_stereo vegetarianism.txt 40 80 lr =


(ソース:simplesystems.org

編集3:ImageMagickバリエーションを追加しました。


8

TeX 212

ASCIIではなく組版システムを使用しています。列幅90ptは4行目で変更することで変更できますが、50バイトの割引を受ける資格があるかどうかはわかりません。テキストの2つのコピー間の距離9ptは、4行目でもを変更することで変更できます。コードはおそらく短くすることができます。各改行を単一のスペースに置き換えることはできますが、完全に削除することはできません。

\let\e\expandafter\read5to\t\read5to\.\def\a#1
{\def\~##1#1##2\a{\def\t{##1\hbox{\
#1\~{}}##2}\a}\e\~\t\a}\e\a\.{}\shipout\hbox
spread9pt{\hsize90pt\fontdimen3\font\hsize\vbox{\t}\
\let\~\ \def\ {}\vbox{\t}}\end.

tex filename.texターミナルで呼び出した後、ユーザーはメインテキストを入力するように求められ、次にシフトする単語のリストを再度求められます。間に空行はありません。2行目に指定された(スペースで区切られた)単語のリストは、本文とまったく同じように表示されます(句読点は文字と同様に扱われ、単語のみがスペースで区切られます)。


7
TeXコンパイラがありません。写真を見せてもらえますか?
aebabis 14年

1
コードを編集するのではなく、ユーザー入力で調整できることを意味します。そうしないと、このボーナスはほとんどすべてのコードに適用されます。
kitcar2000 14年

4

Javascript 391(441〜50)

(私の最初のコードゴルフ)

k=' ';Q='length';A=prompt().split(k);S=prompt().split(k);i=-1;M=25;L=[[]];j=0;R='';while(i++<A[Q]-1){if((j+A[i][Q])<M){if(S.indexOf(A[i])>-1){A[i]=(j?k+k:k)+A[i]}L[L[Q]-1].push(A[i]);j+=A[i][Q]+1}else{j=0;i--;L.push([])}}for(i=0;i<L[Q]-1;P(L[i++].join(C))){C=k;while(L[i].join(C+k)[Q]<M){C+=k}}P(L[i].join(k)+k);function P(a){while(a[Q]<M){a=a.replace(k,k+k)}R+=a;for(c in S){a=a.split(k+k+S[c]).join(k+S[c]+k)}R+=k+k+a+'\n'}console.log(R);

結果

In    a  little  district   In    a  little  district
west    of     Washington   west    of    Washington 
Square   the streets have   Square   the streets have
run    crazy  and  broken   run    crazy  and  broken
themselves    into  small   themselves    into  small
strips   called 'places'.   strips   called 'places'.
These     'places'   make   These     'places'   make
strange     angles    and   strange     angles    and
curves.     One    Street   curves.     One    Street
crosses  itself a time or   crosses  itself a time or
two.     An  artist  once   two.     An  artist  once
discovered    a  valuable   discovered    a  valuable
possibility     in   this   possibility     in   this
street.      Suppose    a   street.      Suppose    a
collector   with  a  bill   collector   with  a  bill
for   paints,  paper  and   for  paints ,  paper  and
canvas      should,    in   canvas      should,    in
traversing   this  route,   traversing   this  route,
suddenly   meet   himself   suddenly   meet  himself 
coming    back, without a   coming    back, without a
cent  having been paid on   cent  having been paid on
account!                    account! 

長いコード:

var arr = "In a little district west of Washington Square the streets have run crazy and broken themselves into small strips called 'places'. These 'places' make strange angles and curves. One Street crosses itself a time or two. An artist once discovered a valuable possibility in this street. Suppose a collector with a bill for paints, paper and canvas should, in traversing this route, suddenly meet himself coming back, without a cent having been paid on account!".split(' ');
var secret = "Washington paints himself".split(' ');
var i = -1;
var MAX_WIDTH = 25;
var lines = [[]];
var _l = 0;

var result = '';

while (i++ < arr.length - 1) {
    if ((_l + arr[i].length) < MAX_WIDTH) {
        if (secret.indexOf(arr[i]) > -1) {arr[i] = (_l?'  ':' ') + arr[i]}
        lines[lines.length - 1].push(arr[i]);
        _l += arr[i].length + 1;

    } else {
        _l = 0;
        i--;
        lines.push([]);
    }
}

for (var i = 0; i < lines.length - 1; putText(lines[i++].join(chars))) {
  // Align text
  var chars = ' ';
  while (lines[i].join(chars + ' ').length < MAX_WIDTH) {
    chars += ' ';
  }
}
putText(lines[i].join(' ') + ' ');
function putText(line) {
  while (line.length < MAX_WIDTH) {
    line = line.replace(' ', '  ');
  }
  // Make the illusion
  result += line;
  for (var val in secret) {
    line = line.split('  '+secret[val]).join(' ' + secret[val] + ' ');
  }
  result += ('   ' + line) + '\n';
}
console.log(result);

1
よくできました。あなたが追加する場合は、束(〜17)を保存することができQ='length'、その後のようなものを交換してくださいA.lengthA[Q]
DocMax 14年

@DocMaxに感謝、それは素晴らしいトリックです。私はcodegolfの初心者です。ご提案ありがとうございます。)
TrungDQ 14年

1
「ワシントンは自分自身を描く」?
ジョーZ.

3

Javascript 493(最小予想)

g=" ";l=prompt().split(g);r=l.slice();m=prompt().split(g);f=[];s=f.slice();w=0;n=0;a="";for(i=0;i<l.length;i++){if(l[i]==m[0]){m.shift();l[i]=g+r[i];r[i]+=g;}if(l[i].length+1>w)w=l[i].length+1;}while(l.length){f[f.length]="";s[s.length]="";while(l.length&&f[f.length-1].length+l[0].length<w){f[f.length-1]+=l[0]+g;s[s.length-1]+=r[0]+g;l.shift();r.shift();}f[f.length-1]+=g.repeat(w-f[f.length-1].length);}console.log(f,s);while(f.length){a+=f[0]+s[0]+"\n";f.shift();s.shift();}console.log(a);

このコードは、2つの行の配列(左と​​右)をセットアップし、それらを文字列に配置してf12コンソールに出力します。

これは最低限の答えであり、勝つことを意図したものではありません。


1
JavaScriptを使用した418:L=b=>b.length;c=console.log;p=prompt;r=(l=p().split(g=" ")).slice(),m=p().split(g),s=(f=[]).slice(),n=w=a="";for(i=0;i<L(l);i++)l[i]==m[0]&&(m.shift(),l[i]=g+r[i],r[i]+=g),L(l[i])+1>w&&(w=L(l[i])+1);for(;L(l);){f[L(f)]="";for(s[L(s)]="";L(l)&&L(f[L(f)-1])+L(l[0])<w;)f[L(f)-1]+=l[0]+g,s[L(s)-1]+=r[0]+g,l.shift(),r.shift();f[L(f)-1]+=g.repeat(w-L(f[L(f)-1]))}for(c(f,s);L(f);)a+=f[0]+s[0]+"\n",f.shift(),s.shift();c(a)
WallyWest 14年

3

GolfScript 209(279 -50 -20)

これは私の最初の大きなGolfScriptプログラムです。最適化を行う必要がある場合、私は驚かないでしょう。両方のボーナスがサポートされています。それらは、次のようなメッセージ入力後に検出されることが期待されています。

"I invented vegetarianism.  It is a diet involving no meat, just vegetables.  It is also common in cows - they are awesome."

"vegetarianism. is awesome."

16  # column width
0   # view type, 1 for cross eyed (?)

そのファイルを保存したinput(そしてGolfScriptをダウンロードした)場合、次のコマンドでスクリプトを呼び出すことができます。

> cat input | ruby golfscript.rb

ゴルフ

~{{\}}{{}}if:v;:w;n%~' '%\' '%[.]zip 0:c;{' '*}:s;{[.;]}:r;\{:x;{.c=0=x=}{1c+:c;}until.c<\1c+>[[x' 'v+' 'x v+]]\++}/zip{0:c;[[]]\{.,.c+w<{1c++:c;\)@r+r+}{:c;[r]+}if}/{.{,}%{+}*w\- 1$,1-.{1$1$/@@%@0:g;{3$3$g>+s\++1g+:g;}*\;\;}{[;.2/\2%1$s@@+s@)\;\]{+}*}if}%}%zip{{4s\++}*}%n*puts

非ゴルフ

~
#The program:

# Parameters, in reverse natural order

{{\}}{{}}if:v;   # view - truthy for parallel, falsey for cross-eyed
:w;         # col width

n%~         # split input on newlines

' '%\       # split secret message tokens
' '%        # split public message

[.]zip      # left and right

0:c;        # word count

{' '*}:s;   # spaces
{[.;]}:r;   # array of top

# for each secret word
\{

  :x;       # word

  {.c=0=x=}
  {1c+:c;} until
  # next public word is this private word

  # splice edits
  .c< \1c+> [[x' 'v+  ' 'x v+]]\ ++

}/
zip

# layout both messages
{

  0:c;    # char count

  [[]]\   # line accumulator

  # split lines
  {

    .,.c+w<
    # if enough room on line

    #append to current line
    {1c++:c;
    \)@r+r+
    }

    #append as new line
    {:c;
    [r]+
    }if

  }/

  # set lines
  {

    .{,}%{+}* # line chars
    w\-       # space chars
    1$,1-     # gaps between words

    # if multi word
    .{

      1$1$      # duplicate params

      /@@       # chars per space
      %         # extra space gaps

      @         # load line
      0:g;      # current gap

      # join row
      {
        3$3$    # params

        g>+     # extra space
        s       # space chars

        \++     # append

        1g+:g;  # update gap
      }*

      \;\;      # drop params

    }
    # else one word
    {
      [
        ;         # drop gap count
        .         # num spaces needed

        2/\       # spaces per side
        2%        # extra space

        1$s       # left space
        @@+s      # right space

        @)\;\     # word

      ]{+}*     # join

    }if

  }% # end line layout

}% # end message layout

zip

{{4s\++}*}%

n*

puts

1

JavaScript 391

_='L=b=>b.length;c=console.log;p=prompt;r=(l*=" ")3m*),s=(f=[]3n=w=a52i=0;i<67i++)l/==m@&&(m!,l/=g+r/,r/8g),?>w&&(w=?72;67){9$]5‌​2:]56)&&%#)+64)<w;)#8l4+g,:-1]8r@+g,l!,r!;#8g.repeat(w-%#))}2c(f,s7%f7)a8$f4+s4+"‌​\\n",f!,s!;c(a)!.shift()#9-1]$??%L(*=p().split(g/[i]2for(3).slice(),4[0]5="";6%l7)‌​;8+=9f[%f):s[%s)?6/)+1@[$0]';for(Y in $='@?:98765432/*%$#!')with(_.split($[Y]))_=join(pop());eval(_)
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.