ラングトンのアリアスキーアート。


22

ラングトンのアリの道を描きます。

説明

平面上の正方形は、黒または白のさまざまな色で表示されます。1つの正方形を「アリ」として任意に識別します。アリは、各ステップで4つの基本方向のいずれかに移動できます。アリは以下のルールに従って移動します。

  • 白い正方形で、90°右に曲がり、正方形の色を反転させて、1ユニット先に進みます
  • 黒い四角で、左に90°回転し、四角の色を反転し、1単位先に進みます

仕様書

  • 入力:0から725までの整数N(両端を含む)。
  • 出力:ステップNの時点でのアリの「パス」を表す17 x 17のグリッド。

ルール

  • アリは右向きになります(3時)。
  • アリはグリッドの中心から始まります。
  • 利用_#@それぞれ白四角、黒四角及びアリのために。
  • グリッドは最初は完全に白です。
  • インタプリタ言語で完全なプログラムまたは関数を作成できます。
  • stdinまたは引数による入力。

更新:ケースのN = 450出力が間違っていました。

N = 0

_________________
_________________
_________________
_________________
_________________
_________________
_________________
_________________
________@________
_________________
_________________
_________________
_________________
_________________
_________________
_________________
_________________

N = 1

_________________
_________________
_________________
_________________
_________________
_________________
_________________
_________________
________#________
________@________
_________________
_________________
_________________
_________________
_________________
_________________
_________________

N = 450

_________________
_________________
___________##____
____##______##___
___#__##___##_#__
__###_#@#__#__#__
__#_#_#_#__#_#___
_____###___#_____
_____#___________
_____#__###______
___#_#_#__#_#_#__
__#__#_#____###__
__#_##__##___#___
___##______##____
____##___________
_________________
_________________

@ジョイ:はい "完全なプログラムまたは関数。引数または標準入力による入力。"、フリースタイル:)
Eelvex

@Joey:それが明確でない場合は申し訳ありません。インタプリタ言語または完全なプログラムで関数を作成できます。stdinから入力を取得するか、引数として提供できます。
エルベックス

@Joey:ステップ1で、アリが最初に右に曲がり(現在は北を向いている)、次に前進することに注意してください。それを考慮に入れていますか?
エルベックス

@Joey:はい、前のコメントでを意味しました、あなたは正しいです、最後の例は異なるNについてでした:-/(例のセクションを更新しました)。
エルベックス

回答:


10

GolfScript-67文字

~17.'_'*n+*\153:|;{|/()[124^.2/6+:6.1&17*)\2&(*|+:|;]@++}*|/();'@'@

hallvaboのPythonソリューションはこれに最も似ているため、主な違いのみを説明します。

ボードは配列ではなく文字列として保存されます。これは、文字列が常にフラットであるため、より少ない文字でボード上の値を更新できるため、目的の出力形式に簡単に変更できます。

アリの位置は、式((d&1)*17+1)*((d&2)-1)(つまり.1&17*)\2&(*)によって増分されます。ここで、dは方向です。6初期化をスキップできるように変数を使用します。


1
Awww、今はGolfScriptの初心者のように感じます。
aaaaaaaaaaaa

:6-とてもヒップ。私はあなたのコードをデバッグしたくない:
ジョンドヴォルザーク

9

Ruby 1.9、104文字

f=->z{l=[*[r=1]*17,2]*17;c=152;z.times{c+=r=(r*r>1?r/18:-r*18)*l[c]*=-1};l[c]=0;l.map{|a|putc"@_
#"[a]}}

関数引数を介した入力。

  • (146-> 142)インライン m
  • (142-> 140)のr*r>1代わりに確認r.abs>1
  • (142-> 128)String#scan出力の生成に使用します。に変更し==ました>
  • (128-> 125)廃止された変数を削除
  • (125-> 122)String#tr条件付きで置換
  • (122-> 122)更新された例と同じ出力を生成するようになりました
  • (122-> 111)アリのパスを生成するとき、charsの代わりにintを使用します。
  • (111-> 109)括弧を保存するためにいくつかの式を並べ替える
  • (109-> 108)コードは関数になりました
  • (108-> 104)すべての文字を個別に印刷する

機能許可されています。
エルベックス

@Eelvex:関数は文字列を返す必要がありますか、それとも出力する必要がありますか?
ヴェンテロ

出力。
エルベックス

6

Python、123

n = input()
d = x = 152
g =(17 * [95] + [10])* 17
一方、n:d + = g [x] / 2; g [x] ^ = 124; x + =(1、-18、-1,18)[d%4]; n- = 1
g [x] = 64
print "%c" * 306%tuple(g)

http://golf.shinh.org/p.rb?Langtons+AntからPythonソリューションを少し修正しただけです。


5

GolfScript 96 94 89

私のお気に入りのヘイト言語は、半読み取り可能なソルタバイトコードの別の束で戻ってきました。

89バージョン、@を出力ループに統合することができました。

~289[0:c]*145:b;{.b>\b<)!..c++(4%:c[1 17-1-17]=b+:b;@++}@*{{(b(:b!.++'_#@@'1/=\}17*n\}17*

94バージョン:

~306[0:c]*152:b;{.b<\b>(!..c++(4%:c[1 18-1-18]=b+:b;\++}@*{{('_#'1/=\}17*(;n\}17*].b<\b>(;'@'\

コメント:

               #Initialization.
~                  #Parse input.
306[0:c]*          #Make array of 306 0s, set c to 0 in the middle of that operation.
152:b;             #Set b to 152, remove 152 from the stack.
                   #b is a value for the ant's position, c for its rotation.

               #Run the algorithm.
{                  #Start of block.
    .b<\b>(        #Split the array at index b into before, after and value at b.
    !..            #Not the value and make 2 copies of it.
    c++            #Add the 2 copies to c.
    (4%:c          #Subtract 1, modulus by 4 and save the result to c.
    [1 18-1-18]=   #Define an array and take element number c.
    b+:b;          #Add b to the value, save result to b, remove result from stack.
    \++            #Reform the array.
}@*                #Switch the input to the top of the stack and run the block input times.

               #Convert array of 1s and 0s to the correct characters.
{                  #Start of block.
    {              #Start of block.
        ('_#'1/=   #Take the first array element, convert it to either '_' or '#'.
        \          #Switch the array to the top of the stack.
    }17*           #Execute block 17 times.
    (;n\           #Discard the 18th element of the line, write a lineshift.
}17*               #Execute block 17 times.

               #Insert the @.
]                  #Put everything in an array.
.b<\b>(            #Split the array at index b into before, after and value at b.
;'@'\              #Ditch the value at b, write a @ and shift it into place.

編集、私は同様に大きなバージョンを作るかもしれません、ここに59 * 59と10500回の反復があります:

~59:a.*[0:c]*1741:b;{.b>\b<)!..c++(4%:c[1 a-1-59]=b+:b;@++}@*{{(b(:b!.++'_#@@'1/=\}a*n\}a*

___________________________________________________________
___________________________________________________________
_________________________##__##____________________________
________________________#__@_###___________________________
_______________________###__#_#_#__________________________
_______________________#####_#__##_________________________
________________________#___##_##_#________________________
_________________________###___#__##_______________________
__________________________#___##_##_#______________________
___________________________###___#__##_____________________
____________________________#___##_##_#__##________________
_____________________________###___#__##__##_______________
______________________________#___##_##__##___#____________
________________________####___###___#___#__###____________
_______________________#____#___#___##_####___#____________
______________________###____#___#_#______#_##_#___________
______________________###____#_##_____#_##__#_##___________
_______________________#____#___##_#_#_____##______________
_______________________#_#______#_#####__#___#_____________
______________________#___#####__________##_######_________
______________________###__##__#_##_#_#_#___##_#_##________
____________________##__#_#######_#___#__###____##_#_______
___________________#__#__######_##___#__#_##___#___#_______
__________________#____#_#_##_#__######_#######___#________
__________________#_####_##_#_####____##__##_#_##_#________
___________________#____####___#__#_######_##____###_______
______________________#___#_##_#_###_#__##__##___###_______
_________________________#######____#__##_##_#_____#_______
_________________####__##_##__####_##_##_##__#_____#_______
________________#____#_#___###_##_###____#_####____#_______
_______________###_______###_#_#_#####____#_#______#_______
_______________#_#___###_####_##_#___##_###_##_____#_______
_____________________##_##__####____####_#_#_#_____#_______
________________#____#__##___###__###_____###______#_______
________________##___##_###_####__#______###___##__#_______
________________##_#_####_____#___#__#_##_###_##___#_______
_______________####_##___##_####__#_#__#__#__###___#_______
_______________#_##_###__#_#_##_#_#_____#_#_____#_#________
___________________#_#__#____##_##__#_#__###_##____________
___________________##_#____#__#####_#____#____#__#_#_______
__________________#_##_#__#____##_##_#__###______###_______
________________#_#___#__#__#__#__###___##__##____#________
_______________###_#_#####_######_###_#######_#_##_________
_______________#_#_#____#####___##__#####_#####____________
_________________#__##___#______#__#_##__###_###___________
______________####___#####_#########___#_#_________________
_________##____#__#_____###_#_#___#_###__###_______________
________#__#__####_##___###_##___###_##_____##_____________
_______###____#_##_#_#####___#____#__#__##_###_____________
_______#_#####_#_#___##__##_____#____#___#__#______________
___________######_####__##_#___#__##__#_#_##_______________
_________##______#_###_##__####___#___###__________________
__________#__#_#####__#___#_##___#__#__#___________________
__________##_###_#######_____#_____#_##____________________
_________#_#__##_##______#___##____#_______________________
________#__#_####________###__##__#________________________
________#_##_###____________##__##_________________________
_________##________________________________________________
__________##_______________________________________________

5

Windows PowerShell、119 118

for($p,$n,$g=144,+"$args"+,1*289;$n--){$d+=$g[$p]*=-1
$p+='B0@R'[$d%4]-65}$g[$p]=0
-join'@_#'[$g]-replace'.{17}',"$&
"

4

PHP、350 309 307 312 174 161 166 159 151 149 147 144 143

<?$p=144;while($i=$argv[1]--){$g[$p]=$a=2-$g[$p];$d+=--$a;$p+=(1-($d&2))*(1+16*($d&1));}while($i++<288)echo$i%17?$i!=$p?$g[$i]?"#": _:"@":"\n";

非ゴルフ

$p = 144; // Set initial pointer

while($i = $argv[1]--){ // Ends at -1
    $g[$p] = $a = 2 - $g[$p]; // Either returns true (2) or false(0)

    $d += --$a; // Adds 1 (2-1) or removes 1 (0-1) from the direction

    $p += (1 - ($d & 2)) * (1 + 16 * ($d & 1));
}

while($i++ < 288)
    echo $i % 17? $i != $p? $g[$i]? "#" : @_ : "@" : "\n"; // Prints the correct character

350-> 309: for()ループを使用したさまざまな圧縮技術。正しい出力を表示するように更新されました。
309-> 307:メインのfor()ループをwhile()ループに変換しました。
307-> 312: argvを使用するように変更するのを忘れた。
312-> 174:別の回答に基づいて再コーディング。
174-> 161:アレイ全体のデフォルトはなくなりました。
161-> 166: Argvが再び勝ちます。
166-> 159: argv [1]を再定義する必要はありません。
159-> 151:デフォルトではなくなり、PHPが自動的に行います。
151-> 149:括弧のセットを削除しました。操作の順序により必要がなくなりました。
149-> 147:最後のfor()ループを短縮し、中括弧は不要です。
147-> 144:最後のfor()ループはwhile()ループになりました。
144-> 143:一時変数を使用してキャラクターを保存しました。


あなたは私のグリッドと方向のトリックを使用し、コードから138文字を削除したことがわかりました!
PatrickvL

4

C、166 162

ここで、DelphiのアプローチをCに翻訳し、Cがどれだけコンパクトであるかを示します。私はfR0DDYから条件付き改行トリックを借りました(mateに感謝!):

g[289]={0},a=144,d,i,N;main(){scanf("%d",&N);while(N--)g[a]=2-g[a],d+=g[a]-1,a+=(1-(d&2))*(1+d%2*16);for(g[a]=1;i<289;)printf("%s%c",i++%17?"":"\n","_@#"[g[i]]);}

インデントされたコメント付きバージョンは次のようになります。

g[289]={0}, // g: The grid is initially completely white. (size=17*17=289)
a=144, // a: Ant position starts at the center of the grid (=8*17+8=144)
d, // Assume 0=d: Ant start 'd'irection faces right (=0, see below)
i,
N;
main(){
  scanf("%d",&N);
  while(N--)
    // Flip the color of the square:
    g[a]=2-g[a],
    // Turn 90° right if at an '_' space, 90° left otherwise :
    d+=g[a]-1,
    // Move one unit forward;
    //   For this, determine the step size, using the two least significant bits of d.
    //   This gives the following relation :
    //     00 = 0 =  90° = right =   1
    //     01 = 1 = 180° = down  =  17
    //     10 = 2 = 270° = left  = - 1
    //     11 = 3 =   0° = up    = -17
    //   (d and 2) gives 0 or 2, translate that to 1 or -1
    //   (d and 1) gives 0 or 1, translate that to 1 or 17
    //   Multiply the two to get an offset 1, 17, -1 or -17 :
    a+=(1-(d&2))*(1+d%2*16);
  // Place the ant and print the grid :
  for(g[a]=1;i<289;)
    printf("%s%c",i++%17?"":"\n","_@#"[g[i]]); // 0 > '_', 1='@', 2 > '#'
}

+1。Iトリックのように"_@#"[g[i]]してa+=(1-(d&2))*(1+(16*(d&1)))
fR0DDY

(1+d%2*16)いくつかの文字を保存します。
ナブ

@Nabb:確かに、それは提案をありがとう、4文字を節約します!
-PatrickvL

4

Delphi、217

var g,a:PByte;i,d,Word;begin g:=AllocMem(306);a:=g+153;Read(i);for n:=1to i do begin a^:=2-a^;d:=d-1+a^;a:=a+(1-2and d)*(1+17*(1and d))end;a^:=1;for n:=1to 306do if n mod 18=0then WriteLn else Write('_@#'[1+g[n]])end.

インデントされたコメント付きのコードは次のようになります。

var
  g,a:PByte;
  i,d,n:Int32;
begin
  g:=AllocMem(306); // g: The grid is initially completely white. (size=18*17=306)
  // Assume 0=d: Ant start 'd'irection faces right (=0, see below)
  a:=g+153; // a: Ant position starts at the center of the grid (=8*18+9=153)
  Read(i);
  for n:=1to i do
  begin
    // Flip the color of the square;
    a^:=2-a^;
    // Turn 90° right if at an '_' space, 90° left otherwise;
    d:=d-1+a^;
    // Move one unit forward;
    //   For this, determine the step size, using the two least significant bits of d.
    //   This gives the following relation :
    //     00 = 0 =  90° = right =   1
    //     01 = 1 = 180° = down  =  18
    //     10 = 2 = 270° = left  = - 1
    //     11 = 3 =   0° = up    = -18
    //   (d and 2) gives 0 or 2, translate that to 1 or -1
    //   (d and 1) gives 0 or 1, translate that to 1 or 18
    //   Multiply the two to get an offset 1, 18, -1 or -18 :
    a:=a+(1-2and d)*(1+17*(1and d))
  end;
  // Place the ant and print the grid :
  a^:=1; // 0 > '_', 1='@', 2 > '#'
  for i:=1to 306do
    if i mod 18=0then // we insert & abuse column 0 for newlines only (saves a begin+end pair)
      WriteLn
    else
      Write('_@#'[1+g[i]])
end.

入力:

450

出力:

_________________
_________________
___________##____
____##______##___
___#__##___##_#__
__###_#@#__#__#__
__#_#_#_#__#_#___
_____###___#_____
_____#___________
_____#__###______
___#_#_#__#_#_#__
__#__#_#____###__
__#_##__##___#___
___##______##____
____##___________
_________________
_________________

@Patrick:例が間違っていました。更新を確認してください。(そして、ステップ451を出力したようです:))。
エルベックス

@Eelvex:ありがとう。4文字のコストで 'N = 0'のケースを修正しました。今度はそれらを再び獲得する必要があります。;-)
PatrickvL

@Eelvex:PS:3時間前にエラーを発見しても+1はありません。謙虚な発言だけで私のせいかもしれません。;)
PatrickvL

@パトリック:私は200未満を待っていたが大丈夫... :)
Eelvex

@Eelvex:LOL、そこに取得し...(すでに238まで)
PatrickvL

3

C 195文字

x=144,T,p=1,i,N[289]={0},a[]={-17,1,17,-1};c(t){p=(p+t+4)%4;x+=a[p];}main(){scanf("%d",&T);while(T--)N[x]=(N[x]+1)%2,c(N[x]?1:-1);for(;i<289;i++)printf("%s%c",i%17?"":"\n",i-x?N[i]?'#':'_':'@');}

http://www.ideone.com/Dw3xW

725でこれを取得します。

_________________
_________________
___________##____
____##______##___
___#___##__##_#__
__###____#_#__#__
__#_#_#__#_#_#___
______###____#__@
_______###__#__#_
_____#_#____#___#
___#_#_#_##____#_
__#__#_#_#_#_###_
__#_##_#_____####
___##_#____#_####
____###___####_#_
_______#__#__##__
________####_____

p+=t+4;x+=a[p%4];代わりにp=(p+t+4)%4;x+=a[p];を使用すると、3文字節約されます。
ジョーイ14

3

sed、481文字

#n
1{s/.*/_________________/;h;H;H;H;G;G;G;G;s/^\(.\{152\}\)_/\1@/;s/$/;r/;ta;};x;:a;/;r/br;/;d/bd;/;l/bl;/;u/bu;:w;y/rdlu/dlur/;bz;:b;y/rdlu/urdl/;bz;:r;s/@\(.\{17\}\)_/#\1@/;tw;s/@\(.\{17\}\)#/#\1!/;tw;s/_\(.\{17\}\)!/@\1_/;tb;s/#\(.\{17\}\)!/!\1_/;tb;:d;s/_@/@#/;tw;s/#@/!#/;tw;s/!_/_@/;tb;s/!#/_!/;tb;:l;s/_\(.\{17\}\)@/@\1#/;tw;s/#\(.\{17\}\)@/!\1#/;tw;s/!\(.\{17\}\)_/_\1@/;tb;s/!\(.\{17\}\)#/_\1!/;tb;:u;s/@_/#@/;tw;s/@#/#!/;tw;s/_!/@_/;tb;s/#!/!_/;tb;:z;h;${s/!/@/;s/;.//p}

最初の行を削除して実行すると、478文字に削減される場合があります -n

入力にはN行が必要です。として実行するとき

seq 450 | sed -f ant.sed

出力:

_________________
_________________
___________##____
____##______##___
___#__##___##_#__
__###_#@#__#__#__
__#_#_#_#__#_#___
_____###___#_____
_____#___________
_____#__###______
___#_#_#__#_#_#__
__#__#_#____###__
__#_##__##___#___
___##______##____
____##___________
_________________
_________________

3

Perl、110文字

$p=144;$p+=(1,-17,-1,17)[($d+=($f[$p]^=2)+1)%4]for 1..<>;$f[$p]=1;print$_%17?'':$/,qw(_ @ #)[$f[$_]]for 0..288

番号は、STDINの最初の行から読み取られます。入力の残りは無視されます。

少し読みやすい:

$p=144;
$p += (1,-17,-1,17)[($d+=($f[$p]^=2)+1) % 4] for 1..<>;
$f[$p]=1;
print $_%17 ? '' : $/, qw(_ @ #)[$f[$_]] for 0..288

編集

  • (112→111)$dモジュロ4値で更新する必要はありません。

  • (111→110)$dインクリメントをインライン化できるようになりました

補遺(109文字)

特殊なN=0失敗の場合に満足している場合は、1文字短くすることができます(@アリの文字は出力されません)。他のすべての入力は正しく機能します。

$p+=(1,-17,-1,17)[($d+=($f{$p+0}^=2)+1)%4]for 1..<>;$f{$p}=1;print$_%17-9?'':$/,qw(_ @ #)[$f{$_}]for-144..144

違いは、現在では%fなくを使用しているため@f、負のインデックスを使用できることと、の-144..144代わりにから反復することです0..288。初期化する必要がなくなります$p



1

> <>、122バイト

小さなスレッドネクロマンシーのリスクがあるため、> <>で答えを書くことは興味深い課題になると思いました...

1&f8r\
1-:?!\r:@@:@$:@@:@g:2*1+&+4%:&}1$-@p{:3$-5gaa*-$@+@5gaa*-+r
2}p70\~
a7+=?\:@@:@g4+5go$1+:
o053.>~1+:64*=?;a
dedc_#@

このプログラムは、計算するステップ数が実行前にスタック上に存在することを想定しています。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.