Jump to content

Langton's ant: Difference between revisions

m
(→‎{{header|APL}}: Correct coordinate chirality)
m (→‎{{header|APL}}: Formatting)
Line 503:
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">langton ← {⍺←⍵ ⋄ grid←⍺ ⍵⍴0 ⋄ ant←2÷⍨⍺ ⍵ ⋄ dir←?4 ⋄ grid ant dir}
 
step ← {
grid ant dir←⍵
dir←1+4|dir+2×grid[⊂ant]
grid[⊂ant]←1-griddir←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
coordinates are matrix ant+←(4order: 2⍴¯row,col. So up is -1 0, right is 0 1, down is 1 0, and left is 0 ¯-1)[dir;]
ant+←(4 2⍴¯1 0 0 grid1 ant1 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}}
1,480

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.