ラングトンの蟻
わくわくパズルランド (岩波ジュニア新書) の「一歩進んで曲がる」や XScreenSaver の Ant は「ラングトンの蟻」と呼ばれるものだという事を おそらくはそれさえも平凡な日々: JavaScriptでラングトンの蟻 で知った記念で、PostScript でラングトンの蟻を書いてみた。
追記 : これもどう書く.org に投稿してみました
余談ですが、わくわくパズルランド (岩波ジュニア新書) には BASIC 版プログラムが掲載されています。
以下ソース。
%!PS gsave 0 dict begin /fieldsize 100 def /white [1 1 1] def /field [ fieldsize{ [fieldsize{ white }repeat] }repeat ] def /check-mark{ % ant -- bool begin field position-x get position-y get color eq end }def /draw-mark{ % ant -- begin field position-x get position-y color put end }def /erase-mark{ % ant -- begin field position-x get position-y white put end }def /generate-ant{ % init-x init-y direction-x direction-y color -- dict 5 dict begin /color exch def /direction-y exch def /direction-x exch def /position-y exch def /position-x exch def currentdict end }def /move{ % ant -- begin /position-x position-x direction-x add def /position-y position-y direction-y add def end }def /turn-left{ % ant -- begin direction-y neg direction-x /direction-y exch def /direction-x exch def end }def /turn-right{ % ant -- begin direction-y direction-x neg /direction-y exch def /direction-x exch def end }def /draw-field{ 2 2 scale 0 1 fieldsize 1 sub{ % x 0 1 fieldsize 1 sub{ % x y field 2 index get 1 index get aload pop setrgbcolor 2 copy 1 1 rectfill pop }for pop }for showpage }def /ant1 fieldsize 2 idiv dup 0 1 [0 0 0] generate-ant def 0 1 20000{ ant1 dup check-mark{ dup erase-mark dup turn-right move }{ dup draw-mark dup turn-left move }ifelse ant1 begin position-x dup 0 lt exch fieldsize ge or position-y dup 0 lt exch fieldsize ge or or end { exit }if % 20歩毎に表示 20 mod 0 eq{ draw-field }if }for end grestore