langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,32 @@
open Graphics
type dir = North | East | South | West
let turn_left = function
| North -> West
| East -> North
| South -> East
| West -> South
let turn_right = function
| North -> East
| East -> South
| South -> West
| West -> North
let move (x, y) = function
| North -> x, y + 1
| East -> x + 1, y
| South -> x, y - 1
| West -> x - 1, y
let () =
open_graph "";
let rec loop (x, y as pos) dir =
let color = point_color x y in
set_color (if color = white then black else white);
plot x y;
let dir = (if color = white then turn_right else turn_left) dir in
if not(key_pressed()) then loop (move pos dir) dir
in
loop (size_x()/2, size_y()/2) North

View file

@ -0,0 +1,15 @@
langton()={
my(M=matrix(100,100),x=50,y=50,d=0);
while(x && y && x<=100 && y<=100,
d=(d+if(M[x,y],1,-1))%4;
M[x,y]=!M[x,y];
if(d%2,x+=d-2,y+=d-1);
);
M
};
show(M)={
my(d=sum(i=1,#M[,1],sum(j=1,#M,M[i,j])),u=vector(d),v=u,t);
for(i=1,#M[,1],for(j=1,#M,if(M[i,j],v[t++]=i;u[t]=j)));
plothraw(u,v)
};
show(langton())

View file

@ -0,0 +1,25 @@
constant @vecs = [1,0,1], [0,-1,1], [-1,0,1], [0,1,1];
constant @blocky = ' '.comb;
constant $size = 100;
enum Square <White Black>;
my @plane = [White xx $size] xx $size;
my ($x, $y) = $size/2, $size/2;
my $dir = @vecs.keys.pick;
my $moves = 0;
loop {
given @plane[$x][$y] {
when :!defined { last }
when White { $dir--; $_ = Black; }
when Black { $dir++; $_ = White; }
}
($x,$y,$moves) »+=« @vecs[$dir %= @vecs];
}
say "Out of bounds after $moves moves at ($x, $y)";
for 0,2,4 ... $size - 2 -> $y {
say join '', gather for 0,2,4 ... $size - 2 -> $x {
take @blocky[ 1 * @plane[$x][$y]
+ 2 * @plane[$x][$y+1]
+ 4 * @plane[$x+1][$y]
+ 8 * @plane[$x+1][$y+1] ];
}
}

View file

@ -0,0 +1,81 @@
/*
* we use the following conventions:
* directions 0: up, 1: right, 2: down: 3: left
*
* pixel white: true, black: false
*
* turn right: true, left: false
*
*/
// number of iteration steps per frame
// set this to 1 to see a slow animation of each
// step or to 10 or 100 for a faster animation
final int STEP=100;
int x;
int y;
int direction;
void setup() {
// 100x100 is large enough to show the
// corridor after about 10000 cycles
size(100, 100, P2D);
background(#ffffff);
x=width/2;
y=height/2;
direction=0;
}
int count=0;
void draw() {
for(int i=0;i<STEP;i++) {
count++;
boolean pix=get(x,y)!=-1;
setBool(x,y,pix);
turn(pix);
move();
if(x<0||y<0||x>=width||y>=height) {
println("finished");
noLoop();
break;
}
}
if(count%1000==0) {
println("iteration "+count);
}
}
void move() {
switch(direction) {
case 0:
y--;
break;
case 1:
x++;
break;
case 2:
y++;
break;
case 3:
x--;
break;
}
}
void turn(boolean rightleft) {
direction+=rightleft?1:-1;
if(direction==-1) direction=3;
if(direction==4) direction=0;
}
void setBool(int x, int y, boolean white) {
set(x,y,white?#ffffff:#000000);
}

View file

@ -0,0 +1,55 @@
#White = $FFFFFF
#Black = 0
#planeHeight = 100
#planeWidth = 100
#canvasID = 0
#windowID = 0
OpenWindow(#windowID, 0, 0, 150, 150, "Langton's ant", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget(#canvasID, 25, 25, #planeWidth, #planeHeight)
StartDrawing(CanvasOutput(#canvasID))
Box(0, 0, #planeWidth, #planeHeight, #White)
StopDrawing()
Define event, quit, ant.POINT, antDirection, antSteps
ant\x = #planeHeight / 2
ant\y = #planeWidth / 2
Repeat
Repeat
event = WindowEvent()
If event = #PB_Event_CloseWindow
quit = 1
event = 0
EndIf
Until event = 0
StartDrawing(CanvasOutput(#canvasID))
Select Point(ant\x, ant\y)
Case #Black
Plot(ant\x, ant\y, #White)
antDirection = (antDirection + 1) % 4 ;turn left
Case #White
Plot(ant\x, ant\y, #Black)
antDirection = (antDirection - 1 + 4) % 4 ;turn right
EndSelect
StopDrawing()
Select antDirection
Case 0 ;up
ant\y - 1
Case 1 ;left
ant\x - 1
Case 2 ;down
ant\y + 1
Case 3 ;right
ant\x + 1
EndSelect
antSteps + 1
If ant\x < 0 Or ant\x >= #planeWidth Or ant\y < 0 Or ant\y >= #planeHeight
MessageRequester("Langton's ant status", "Out of bounds after " + Str(antSteps) + " steps.")
quit = 1
EndIf
Delay(10) ;control animation speed and avoid hogging CPU
Until quit = 1

View file

@ -0,0 +1,29 @@
dim plane(100,100)
x = 50: y = 50: minY = 100
while (x>0) and (x<100) and (y>0) and (y<100)
if plane(x,y) then
nxt = nxt - 1
if nxt < 1 then nxt = 4
else
nxt = nxt + 1
if nxt > 4 then nxt = 1
end if
x = x + (nxt = 2) - (nxt = 4)
y = y + (nxt = 3) - (nxt = 1)
plane(x,y) = (plane(x,y) <> 1)
minY = min(y,minY) ' find lowest and
maxY = max(y,maxY) ' highest y to prevent printing blank lines
wend
graphic #g, 100,100
for y = minY to maxY
for x = 1 to 100
print chr$((plane(x,y)*3) + 32);
if plane(x,y) = 1 then #g "color green ; set "; x; " "; y else #g "color blue ; set "; x; " "; y
next x
print y
next y
render #g
#g "flush""

View file

@ -0,0 +1,19 @@
PROGRAM:LANT
:ClrDraw
:0→N
:Pxl-On(0,0)
:47→X
:31→Y
:90→Θ
:Repeat getKey
:If pxl-Test(Y,X)
:Then
:Θ+90→Θ
:Else
:Θ-90→Θ
:End
:Pxl-Change(Y,X)
:X+cos(Θ°)→X
:Y+sin(Θ°)→Y
:N+1→N
:End

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,37 @@
; For easier access, the direction vector is stored at the end of the heap.
push 10003 dup push 100 store
push 1 sub dup push -1 store
push 1 sub dup push -100 store
push 1 sub dup push 1 store
0: ; Initialize the grid.
push 1 sub dup push 0 store
dup push 0 swap sub jn 0
push 5050 ; Start the ant at the center.
1: ; Make sure the ant's in bounds.
dup push 100 mod jn 2
dup push 100 div jn 2
push 100 copy 1 copy 1 mod sub jz 2
push 100 copy 1 copy 1 div sub jz 2
swap copy 1 load ; Get current cell state.
push 1 add push 2 mod ; Invert it.
copy 2 copy 1 store ; Then store it back.
push 2 mul push 5 add add push 4 mod ; Determine new direction.
swap copy 1 push 10000 add load add ; Update position accordingly.
jump 1
2: ; Initialize a counter and flow into the printer.
pop dup sub
3: ; Iterate over the cells.
dup load push 32 add ochr ; Print ' ' for off, '!' for on.
push 1 add dup ; Increment the counter.
push 100 mod jz 5 ; Branch at the end of a row.
4:
dup push 10000 sub jn 3 ; Go again unless counter is 10000.
pop exit ; All done, exit clean.
5: ; Print a newline and jump back to the counter check.
push 10 ochr jump 4

View file

@ -0,0 +1,17 @@
include c:\cxpl\codes; \intrinsic 'code' declarations
int X, Y, Dir;
[SetVid($13); \set 320x200 graphic video mode
X:= 50; Y:= 50; Dir:= 0; \start in middle facing east
repeat if ReadPix(X,Y) then \(black and white are reversed)
[Dir:= Dir-1;\left\ Point(X,Y, 0\black\)]
else [Dir:= Dir+1;\right\ Point(X,Y,$F\white\)];
case Dir & 3 of
0: X:= X+1; \east
1: Y:= Y+1; \south
2: X:= X-1; \west
3: Y:= Y-1 \north
other [];
until X<0 ! X>=100 ! Y<0 ! Y>=100;
X:= ChIn(1); \wait for keystroke
SetVid(3); \restore normal text mode
]