Langton's ant: Difference between revisions

Content added Content deleted
(→‎{{header|APL}}: Correct coordinate chirality)
m (→‎{{header|APL}}: Formatting)
Line 503: Line 503:
{{works with|Dyalog APL}}
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">langton ← {⍺←⍵ ⋄ grid←⍺ ⍵⍴0 ⋄ ant←2÷⍨⍺ ⍵ ⋄ dir←?4 ⋄ grid ant dir}
<syntaxhighlight lang="apl">langton ← {⍺←⍵ ⋄ grid←⍺ ⍵⍴0 ⋄ ant←2÷⍨⍺ ⍵ ⋄ dir←?4 ⋄ grid ant dir}

step ← {
step ← {
grid ant dir←⍵
grid ant dir←⍵
dir←1+4|dir+2×grid[⊂ant]
grid[⊂ant]←1-grid[⊂ant]
dir←1+4|dir+2×grid[⊂ant]
grid[⊂ant]←1-grid[⊂ant]
⍝ coordinates are matrix order: row,col. So up is -1 0, right is 0 1, down is 1 0, and left is 0 -1
ant+←(4 2⍴¯1 0 0 1 1 0 0 ¯1)[dir;]
coordinates are matrix order: row,col. So up is -1 0, right is 0 1, down is 1 0, and left is 0 -1
grid ant dir
ant+←(4 2⍴¯1 0 0 1 1 0 0 ¯1)[dir;]
}
grid ant dir
}
⍝ to watch it run, open the variable pic in the monitor before executing this step

{} { state ∘← ⍵ ⋄ pic ∘← '.⌺'[1+⊃1⌷⍵] ⋄ _←⎕dl ÷500 ⋄ step ⍵} ⍣≡ langton 100</syntaxhighlight>
⍝ to watch it run, open the variable pic in the monitor before executing this step
{} { state ∘← ⍵ ⋄ pic ∘← '.⌺'[1+⊃1⌷⍵] ⋄ _←⎕dl ÷500 ⋄ step ⍵} ⍣≡ langton 100</syntaxhighlight>


{{Out}}
{{Out}}