June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,16 +1,18 @@
|
|||
[[wp:Langton's ant|Langton's ant]] is a cellular automaton that models an ant sitting on a plane of cells, all of which are white initially, facing in one of four directions.
|
||||
[[wp:Langton's ant|Langton's ant]] is a cellular automaton that models an [https://en.wikipedia.org/wiki/Ant ant] sitting on a plane of cells, all of which are white initially, the ant facing in one of four directions.
|
||||
|
||||
Each cell can either be black or white.
|
||||
|
||||
The ant moves according to the color of the cell it is currently sitting in, with the following rules:
|
||||
::# If the cell is black, it changes to white and the ant turns left;
|
||||
::# If the cell is white, it changes to black and the ant turns right;
|
||||
::# The ant then moves forward to the next cell, and repeat from step 1.
|
||||
|
||||
<br>This rather simple ruleset leads to an initially chaotic movement pattern, and after about 10000 steps, a cycle appears where the ant moves steadily away from the starting location in a diagonal corridor about 10 pixels wide.
|
||||
Conceptually the ant can then travel infinitely far away.
|
||||
<br>This rather simple ruleset leads to an initially chaotic movement pattern, and after about 10000 steps, a cycle appears where the ant moves steadily away from the starting location in a diagonal corridor about 10 cells wide.
|
||||
Conceptually the ant can then walk infinitely far away.
|
||||
|
||||
|
||||
;Task:
|
||||
Start the ant near the center of a 100 by 100 field of cells, which is about big enough to contain the initial chaotic part of the movement.
|
||||
Start the ant near the center of a 100<small>x</small>100 field of cells, which is about big enough to contain the initial chaotic part of the movement.
|
||||
|
||||
Follow the movement rules for the ant, terminate when it moves out of the region, and show the cell colors it leaves behind.
|
||||
|
||||
|
|
|
|||
|
|
@ -4,25 +4,20 @@ is_white(list map, integer x, integer y)
|
|||
integer p, w;
|
||||
data b;
|
||||
|
||||
b = l_q_data(map, y);
|
||||
w = b_character(b, x >> 3);
|
||||
b = map[y];
|
||||
w = b[x >> 3];
|
||||
p = 1 << (7 - (x & 7));
|
||||
b_replace(b, x >> 3, w ^ p);
|
||||
b[x >> 3] = w ^ p;
|
||||
|
||||
return !(w & p);
|
||||
!(w & p);
|
||||
}
|
||||
|
||||
void
|
||||
ant(integer x, integer y, integer d, list map)
|
||||
{
|
||||
while (-1 < x && x < 100 && -1 < y && y < 100) {
|
||||
if (is_white(map, x, y)) {
|
||||
d += 3;
|
||||
d &= 3;
|
||||
} else {
|
||||
d += 1;
|
||||
d &= 3;
|
||||
}
|
||||
d += is_white(map, x, y) ? 3 : 1;
|
||||
d &= 3;
|
||||
|
||||
if (d & 1) {
|
||||
y += (d & 2) - 1;
|
||||
|
|
@ -41,29 +36,16 @@ main(void)
|
|||
|
||||
i = 100;
|
||||
while (i) {
|
||||
data b;
|
||||
integer j;
|
||||
|
||||
i -= 1;
|
||||
j = 13;
|
||||
while (j) {
|
||||
j -= 1;
|
||||
b_append(b, 0);
|
||||
}
|
||||
|
||||
l_l_data(l, -1, b);
|
||||
l_n_data(l, -1).run(13, 0);
|
||||
}
|
||||
|
||||
ant(50, 50, 2, l);
|
||||
|
||||
f_open(f, "ant.pbm", OPEN_CREATE | OPEN_TRUNCATE | OPEN_WRITEONLY, 00644);
|
||||
f.create("ant.pbm", 00644);
|
||||
|
||||
f_text(f, "P4\n100 100\n");
|
||||
i = 100;
|
||||
while (i) {
|
||||
f_b_post(f, l_q_data(l, -i));
|
||||
i -= 1;
|
||||
}
|
||||
f.text("P4\n100 100\n");
|
||||
l.ucall(f_data, 1, f);
|
||||
|
||||
return 0;
|
||||
0;
|
||||
}
|
||||
|
|
|
|||
34
Task/Langtons-ant/Kotlin/langtons-ant.kotlin
Normal file
34
Task/Langtons-ant/Kotlin/langtons-ant.kotlin
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
// version 1.2.0
|
||||
|
||||
enum class Direction { UP, RIGHT, DOWN, LEFT }
|
||||
|
||||
const val WHITE = 0
|
||||
const val BLACK = 1
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val width = 75
|
||||
val height = 52
|
||||
val maxSteps = 12_000
|
||||
var x = width / 2
|
||||
var y = height / 2
|
||||
val m = Array(height) { IntArray(width) }
|
||||
var dir = Direction.UP
|
||||
var i = 0
|
||||
while (i < maxSteps && x in 0 until width && y in 0 until height) {
|
||||
val turn = m[y][x] == BLACK
|
||||
val index = (dir.ordinal + if (turn) 1 else -1) and 3
|
||||
dir = Direction.values()[index]
|
||||
m[y][x] = if (m[y][x] == BLACK) WHITE else BLACK
|
||||
when (dir) {
|
||||
Direction.UP -> y--
|
||||
Direction.RIGHT -> x--
|
||||
Direction.DOWN -> y++
|
||||
Direction.LEFT -> x++
|
||||
}
|
||||
i++
|
||||
}
|
||||
for (j in 0 until height) {
|
||||
for (k in 0 until width) print(if(m[j][k] == WHITE) '.' else '#')
|
||||
println()
|
||||
}
|
||||
}
|
||||
37
Task/Langtons-ant/REXX/langtons-ant-1.rexx
Normal file
37
Task/Langtons-ant/REXX/langtons-ant-1.rexx
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/*REXX program implements Langton's ant walk and displays the ant's path (finite field).*/
|
||||
parse arg dir char seed . /*obtain optional arguments from the CL*/
|
||||
if datatype(seed, 'W') then call random ,,seed /*Integer? Then use it as a RANDOM SEED*/
|
||||
if dir=='' | dir=="," then dir=random(1, 4) /*ant is facing a random direction, */
|
||||
if char=='' | char=="," then char= '#' /*binary colors: 0≡white, 1≡black. */
|
||||
parse value scrSize() with sd sw . /*obtain the terminal's depth and width*/
|
||||
sd=sd -6; sw=sw -1 /*adjust for terminal's useable area. */
|
||||
xHome=1000000; yHome=1000000 /*initially in the middle of nowhere. */
|
||||
x=xHome; y=yHome /*start ant's walk in middle of nowhere*/
|
||||
$.=1; $.0=4 ; $.2=2; $.3=3; $.4=4 /* 1≡north 2≡east 3≡south 4≡west.*/
|
||||
minX=x; minY=y; maxX=x; maxY=y /*initialize the min/max values for X,Y*/
|
||||
@.=0 /*the universe (walk field) is white.*/
|
||||
do #=1 until (maxX-minY>sw)|(maxY-minY>sd) /*is the path out─of─bounds for screen?*/
|
||||
black=@.x.y; @.x.y= \@.x.y /*invert (flip) ant's cell color code.*/
|
||||
if black then dir=dir - 1 /*if cell color was black, turn left.*/
|
||||
else dir=dir + 1 /* " " " " white, turn right.*/
|
||||
dir=$.dir /*$ array handles/adjusts under & over.*/
|
||||
select /*ant walks the direction it's facing. */
|
||||
when dir==1 then y= y + 1 /*is ant walking north? Then go up. */
|
||||
when dir==2 then x= x + 1 /* " " " east? " " right.*/
|
||||
when dir==3 then y= y - 1 /* " " " south? " " down. */
|
||||
when dir==4 then x= x - 1 /* " " " west? " " left. */
|
||||
end /*select*/ /*the DIRection is always normalized.*/
|
||||
minX=min(minX, x); maxX=max(maxX, x) /*find the minimum and maximum of X. */
|
||||
minY=min(minY, y); maxY=max(maxY, y) /* " " " " " " Y. */
|
||||
end /*steps*/ /* [↑] ant walks hither and thither. */
|
||||
/*finished walking, it's out─of─bounds.*/
|
||||
say center(" Langton's ant walked " # ' steps ', sw, "─")
|
||||
@.xHome.yHome='█' /*show the ant's initial starting point*/
|
||||
@.x.y= '∙' /*show where the ant went out─of─bounds*/
|
||||
/* [↓] show Langton's ant's trail. */
|
||||
do y=maxY to minY by -1; _= /*display a single row of cells. */
|
||||
do x=minX to maxX; _=_ || @.x.y /*build a cell row for the display. */
|
||||
end /*x*/ /* [↓] strip trailing blanks from line*/
|
||||
_=strip( translate(_, char, 10), 'T') /*color the cells: black or white. */
|
||||
if _\=='' then say _ /*display line (strip trailing blanks).*/
|
||||
end /*y*/ /*stick a fork in it, we're all done. */
|
||||
1
Task/Langtons-ant/REXX/langtons-ant-2.rexx
Normal file
1
Task/Langtons-ant/REXX/langtons-ant-2.rexx
Normal file
|
|
@ -0,0 +1 @@
|
|||
when dir==4 then x= x - 1 /* " " " west? " " left. */
|
||||
1
Task/Langtons-ant/REXX/langtons-ant-3.rexx
Normal file
1
Task/Langtons-ant/REXX/langtons-ant-3.rexx
Normal file
|
|
@ -0,0 +1 @@
|
|||
otherwise x= x - 1 /* " " " west? " " left. */
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
/*REXX program implements Langton's ant walk and displays the ant's path (finite field).*/
|
||||
parse arg dir char seed . /*obtain optional arguments from the CL*/
|
||||
if datatype(seed, 'W') then call random ,,seed /*Integer? Then use it as a RANDOM SEED*/
|
||||
if dir=='' | dir=="," then dir=random(1, 4) /*ant is facing a random direction, */
|
||||
if char=='' | char=="," then char= '#' /*binary colors: 0≡white, 1≡black. */
|
||||
parse value scrSize() with sd sw . /*obtain the terminal's depth and width*/
|
||||
sd=sd-6; sw=sw-1 /*adjust for terminal's useable area. */
|
||||
x=1000000 ; y=1000000 /*start ant's walk in middle of nowhere*/
|
||||
$.=1; $.0=4 ; $.2=2; $.3=3; $.4=4 /* 1≡north 2≡east 3≡south 4≡west.*/
|
||||
@.=0 ; minx=x; miny=y; maxx=x; maxy=y /*define stem array, default: all white*/
|
||||
/* [↓] ant walks hither and thither. */
|
||||
do steps=1 until (maxx-miny>sw) | (maxy-miny>sd) /*'til the ant is out─of─bounds.*/
|
||||
black=@.x.y; @.x.y= \ @.x.y /*invert (flip) ant's cell color code.*/
|
||||
if black then dir=dir-1 /*if cell color was black, turn left.*/
|
||||
else dir=dir+1 /* " " " " white, turn right.*/
|
||||
dir=$.dir /*possibly adjust for under/over. */
|
||||
select /*ant walks the direction it's facing. */
|
||||
when dir==1 then y= y + 1 /*is ant walking north? Then go up. */
|
||||
when dir==2 then x= x + 1 /* " " " east? " " right.*/
|
||||
when dir==3 then y= y - 1 /* " " " south? " " down. */
|
||||
when dir==4 then x= x - 1 /* " " " west? " " left. */
|
||||
end /*select*/
|
||||
minx=min(minx, x); maxx=max(maxx, x) /*find the minimum and maximum of X. */
|
||||
miny=min(miny, y); maxy=max(maxy, y) /* " " " " " " Y. */
|
||||
end /*steps*/
|
||||
/*finished walking, it's out-of-bounds.*/
|
||||
say center(" Langton's ant walked " steps ' steps. ', 79, "─")
|
||||
@.1000000.1000000='█' /*show the ant's initial starting point*/
|
||||
@.x.y= '∙' /*show where the ant went out-of-bounds*/
|
||||
/* [↓] show Langton's ant's trail. */
|
||||
do y=maxy to miny by -1; _= /*display a single row of cells. */
|
||||
do x=minx to maxx; _=_ || @.x.y /*build a cell row for the display. */
|
||||
end /*x*/
|
||||
_=strip( translate(_, char, 10), 'T') /*color the cells: black or white. */
|
||||
if _\=='' then say _ /*display line (strip trailing blanks).*/
|
||||
end /*y*/ /*stick a fork in it, we're all done. */
|
||||
|
|
@ -4,20 +4,20 @@ define size = 100
|
|||
enum |White, Black|
|
||||
var plane = size.of { size.of (White) }
|
||||
|
||||
var (x, y) = @|([size/2 -> int]*2)
|
||||
var (x, y) = ([size >> 1] * 2)...
|
||||
var dir = dirs.len.irand
|
||||
|
||||
var moves = 0
|
||||
loop {
|
||||
(x >= 0) && (y >= 0) && (x < size) && (y < size) || break
|
||||
|
||||
given(plane[x][y]) {
|
||||
given (plane[x][y]) {
|
||||
when (White) { dir--; plane[x][y] = Black }
|
||||
when (Black) { dir++; plane[x][y] = White }
|
||||
}
|
||||
|
||||
++moves
|
||||
[\x, \y]:dirs[dir %= dirs.len] -> each {|a,b| *a += b }
|
||||
[[\x, \y], dirs[dir %= dirs.len]].zip {|a,b| *a += b }
|
||||
}
|
||||
|
||||
say "Out of bounds after #{moves} moves at (#{x}, #{y})"
|
||||
|
|
|
|||
59
Task/Langtons-ant/VBA/langtons-ant.vba
Normal file
59
Task/Langtons-ant/VBA/langtons-ant.vba
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
Option Explicit
|
||||
|
||||
Sub Ant()
|
||||
Dim TablDatas(1 To 200, 1 To 256) As String, sDir As String, sFile As String, Str As String
|
||||
Dim ColA As Integer, LigA As Long, ColF As Integer, LigF As Long, i As Long, j As Integer, Num As Long
|
||||
Dim Top As Boolean, Left As Boolean, Bottom As Boolean, Right As Boolean
|
||||
|
||||
'init variables
|
||||
Top = True
|
||||
LigF = 80
|
||||
ColF = 50
|
||||
For i = 1 To 200
|
||||
For j = 1 To 256
|
||||
TablDatas(i, j) = " "
|
||||
Next
|
||||
Next
|
||||
'directory
|
||||
sDir = "C:\Users\yourname\Desktop\"
|
||||
'name txt file
|
||||
sFile = "Langton_Ant.txt"
|
||||
|
||||
'start
|
||||
For i = 1 To 15000
|
||||
LigA = LigF
|
||||
ColA = ColF
|
||||
If LigA = 1 Or ColA = 1 Or ColA = 256 Or LigA = 200 Then GoTo Fin
|
||||
If TablDatas(LigA, ColA) = " " Then
|
||||
TablDatas(LigA, ColA) = "#"
|
||||
Select Case True
|
||||
Case Top: Top = False: Left = True: LigF = LigA: ColF = ColA - 1
|
||||
Case Left: Left = False: Bottom = True: LigF = LigA + 1: ColF = ColA
|
||||
Case Bottom: Bottom = False: Right = True: LigF = LigA: ColF = ColA + 1
|
||||
Case Right: Right = False: Top = True: LigF = LigA - 1: ColF = ColA
|
||||
End Select
|
||||
Else
|
||||
TablDatas(LigA, ColA) = " "
|
||||
Select Case True
|
||||
Case Top: Top = False: Right = True: LigF = LigA: ColF = ColA + 1
|
||||
Case Left: Left = False: Top = True: LigF = LigA - 1: ColF = ColA
|
||||
Case Bottom: Bottom = False: Left = True: LigF = LigA: ColF = ColA - 1
|
||||
Case Right: Right = False: Bottom = True: LigF = LigA + 1: ColF = ColA
|
||||
End Select
|
||||
End If
|
||||
Next i
|
||||
'result in txt file
|
||||
Num = FreeFile
|
||||
Open sDir & sFile For Output As #Num
|
||||
For i = 1 To UBound(TablDatas, 1)
|
||||
Str = vbNullString
|
||||
For j = 1 To UBound(TablDatas, 2)
|
||||
Str = Str & TablDatas(i, j)
|
||||
Next j
|
||||
Print #1, Str
|
||||
Next i
|
||||
Close #Num
|
||||
Exit Sub
|
||||
Fin:
|
||||
MsgBox "Stop ! The ant is over limits."
|
||||
End Sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue