September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,20 @@
begin
string(20) state;
string(20) nextState;
integer generation;
generation := 0;
state := "_###_##_#_#_#_#__#__";
while begin
write( i_w := 1, s_w := 1, "Generation ", generation, state );
nextState := "____________________";
for cPos := 1 until 18 do begin
string(3) curr;
curr := state( cPos - 1 // 3 );
nextState( cPos // 1 ) := if curr = "_##" or curr = "#_#" or curr = "##_" then "#" else "_"
end for_cPos ;
( state not = nextState )
end do begin
state := nextState;
generation := generation + 1
end while_not_finished
end.

View file

@ -0,0 +1,19 @@
10 LET N$="01110110101010100100"
20 LET G=1
30 PRINT N$
40 LET O$=N$
50 LET N$=""
60 PRINT AT 0,28;G
70 LET N=0
80 FOR I=1 TO LEN O$
90 IF I=1 THEN GOTO 120
100 LET N=VAL O$(I-1)
110 IF I=LEN O$ THEN GOTO 130
120 LET N=N+VAL O$(I+1)
130 IF N=0 THEN LET N$=N$+"0"
140 IF N=1 THEN LET N$=N$+O$(I)
150 IF N=2 THEN LET N$=N$+STR$ NOT VAL O$(I)
160 PRINT AT 0,I-1;N$(I)
170 NEXT I
180 LET G=G+1
190 IF N$<>O$ THEN GOTO 40

View file

@ -1,11 +0,0 @@
> coffee cellular_automata.coffee
.###.##.#.#.#.#..#..
.#.#####.#.#.#......
..##...##.#.#.......
..##...###.#........
..##...#.##.........
..##....###.........
..##....#.#.........
..##.....#..........
..##................
equilibrium achieved

View file

@ -1,32 +0,0 @@
# We could cheat and count the bits, but let's keep this general.
# . = dead, # = alive, middle cells survives iff one of the configurations
# below is satisified.
survival_scenarios = [
'.##' # happy neighbors
'#.#' # birth
'##.' # happy neighbors
]
b2c = (b) -> if b then '#' else '.'
cell_next_gen = (left_alive, me_alive, right_alive) ->
fingerprint = b2c(left_alive) + b2c(me_alive) + b2c(right_alive)
fingerprint in survival_scenarios
cells_for_next_gen = (cells) ->
# This function assumes a finite array, i.e. cells can't be born outside
# the original array.
(cell_next_gen(cells[i-1], cells[i], cells[i+1]) for i in [0...cells.length])
display = (cells) ->
(b2c(is_alive) for is_alive in cells).join ''
simulate = (cells) ->
while true
console.log display cells
new_cells = cells_for_next_gen cells
break if display(cells) == display(new_cells)
cells = new_cells
console.log "equilibrium achieved"
simulate (c == '#' for c in ".###.##.#.#.#.#..#..")

View file

@ -1,24 +1,32 @@
module Life1D where
import Data.List (unfoldr)
import System.Random (newStdGen, randomRs)
import Control.Monad (liftM2, ap)
import Data.List
import System.Random
import Control.Monad
import Control.Arrow
bnd :: [Char] -> Char
bnd :: String -> Char
bnd bs =
case bs of
"_##" -> '#'
"#_#" -> '#'
"##_" -> '#'
_ -> '_'
case bs of
"_##" -> '#'
"#_#" -> '#'
"##_" -> '#'
_ -> '_'
donxt xs = unfoldr(\xs -> case xs of [_,_] -> Nothing ;
_ -> Just (bnd $ take 3 xs, drop 1 xs)) $ '_':xs++"_"
donxt :: String -> String
donxt xs =
unfoldr
(\xs ->
case xs of
[_, _] -> Nothing
_ -> Just (bnd $ take 3 xs, drop 1 xs)) $
'_' : xs ++ "_"
lahmahgaan xs = init.until (liftM2 (==) last (last. init)) (ap (++)(return. donxt. last)) $ [xs, donxt xs]
lahmahgaan :: String -> [String]
lahmahgaan xs =
init .
until (liftM2 (==) last (last . init)) (ap (++) (return . donxt . last)) $
[xs, donxt xs]
main :: IO ()
main = do
g <- newStdGen
let oersoep = map ("_#"!!). take 36 $ randomRs(0,1) g
mapM_ print . lahmahgaan $ oersoep
g <- newStdGen
let oersoep = map ("_#" !!) . take 36 $ randomRs (0, 1) g
mapM_ print . lahmahgaan $ oersoep

View file

@ -1,16 +1,15 @@
' [RC] 'One-dimensional cellular automata'
global rule$, state$
' does not wrap so fails for some rules
rule$ ="00010110" ' Rule 22 decimal
state$ ="0011101101010101001000"
for j =1 to 20
print state$
oldState$ =state$
state$ ="0"
for k =2 to 32
for k =2 to len( oldState$) -1
NHood$ =mid$( oldState$, k -1, 3) ' pick 3 char neighbourhood and turn binary string to decimal
vNHood =0
for kk =3 to 1 step -1
@ -19,8 +18,8 @@ for j =1 to 20
' .... & use it to index into rule$ to find appropriate new value
state$ =state$ +mid$( rule$, vNHood +1, 1)
next k
state$ =state$ +"0"
print state$
next j
end

View file

@ -1,13 +1 @@
###.##.#.#.#.#..#
#.#####.#.#.#....
.##...##.#.#.....
.##...###.#......
.##...#.##.......
.##....###.......
.##....#.#.......
.##.....#........
.##..............
.##..............
.##..............
.##..............
.##..............
CellularAutomaton[2^^01101000 (* == 104 *), {{1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1}, 0}, 12];

View file

@ -0,0 +1,13 @@
###.##.#.#.#.#..#
#.#####.#.#.#....
.##...##.#.#.....
.##...###.#......
.##...#.##.......
.##....###.......
.##....#.#.......
.##.....#........
.##..............
.##..............
.##..............
.##..............
.##..............

View file

@ -1,68 +0,0 @@
import math
randomize()
type
TBoolArray = array[0..30, bool] # an array that is indexed with 0..10
TSymbols = tuple[on: char , off: char]
const
num_turns = 20
symbols:TSymbols = ('#','_')
proc `==` (x:TBoolArray,y:TBoolArray): bool =
if len(x) != len(y):
return False
for i in 0..(len(x)-1):
if x[i] != y[i]:
return False
return True
proc count_neighbours(map:TBoolArray , tile:int):int =
result = 0
if tile != len(map)-1 and map[tile+1]:
result += 1
if tile != 0 and map[tile-1]:
result += 1
proc print_map(map:TBoolArray, symbols:TSymbols) =
for i in map:
if i:
write(stdout,symbols[0])
else:
write(stdout,symbols[1])
write(stdout,"\n")
proc random_map(): TBoolArray =
var map = [False,False,False,False,False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,False]
for i in 0..(len(map)-1):
map[i] = bool(random(2))
return map
proc fixed_map(): TBoolArray =
var map = [False,True,True,True,False,True,True,False,True,False,True,
False,True,False,True,False,False,True,False,False,False,False,
False,False,False,False,False,False,False,False,False]
return map
#make the map
var map:TBoolArray
#map = random_map() # uncomment for random start
map = fixed_map()
print_map(map,symbols)
for i in 0..num_turns:
var new_map = map
for j in 0..(len(map)-1):
if map[j]:
if count_neighbours(map, j) == 2 or
count_neighbours(map, j) == 0:
new_map[j] = False
else:
if count_neighbours(map, j) == 2:
new_map[j] = True
if new_map == map:
print_map(map,symbols)
break
map = new_map
print_map(map,symbols)

View file

@ -1,26 +0,0 @@
const
s_init: string = "_###_##_#_#_#_#__#__"
arrLen: int = 20
var q0: string = s_init & repeatChar(arrLen-20,'_')
var q1: string = q0
proc life(s:string): char =
var str: string = s
if len(normalize(str)) == 2: # normalize eliminates underscores
return '#'
return '_'
proc evolve(q: string): string =
result = repeatChar(arrLen,'_')
#result[0] = '_'
for i in 1 .. q.len-1:
result[i] = life(substr(q & '_',i-1,i+1))
echo(q1)
q1 = evolve(q0)
echo(q1)
while q1 != q0:
q0 = q1
q1 = evolve(q0)
echo(q1)

View file

@ -0,0 +1,73 @@
program Test;
{$IFDEF FPC}{$MODE DELPHI}{$ELSE}{$APPTYPE}{$ENDIF}
uses
sysutils;
const
cCHAR: array[0..1] of char = ('_','#');
type
TRow = array of byte;
function ConvertToRow(const s:string):tRow;
var
i : NativeInt;
Begin
i := length(s);
setlength(Result,length(s));
For i := i downto 0 do
result[i-1]:= ORD(s[i]=cChar[1]);
end;
function OutRow(const row:tRow):string;
//create output string
var
i: NativeInt;
Begin
i := length(row);
setlength(result,i);
For i := i downto 1 do
result[i]:= cChar[row[i-1]];
end;
procedure NextRow(row:pByteArray;MaxIdx:NativeInt);
//compute next row in place by the using a small storage for the
//2 values, that would otherwise be overridden
var
leftValue,Value: NativeInt;
i,trpCnt: NativeInt;
Begin
leftValue := 0;
trpCnt := row[0]+row[1];
i := 0;
while i < MaxIdx do
Begin
Value := row[i];
//the rule for survive : PopCnt == 2
row[i] := ORD(trpCnt= 2);
//reduce popcnt of element before
dec(trpCnt,leftValue);
//goto next element
inc(i);
leftValue := Value;
//increment popcnt by right element
inc(trpCnt,row[i+1]);
//move to next position in ring buffer
end;
row[MaxIdx] := ORD(trpCnt= 2);
end;
const
TestString: string=' ### ## # # # # # ';
var
s: string;
row:tRow;
i: NativeInt;
begin
s := Teststring;
row:= ConvertToRow(s);
For i := 0 to 9 do
Begin
writeln(OutRow(row));
NextRow(@row[0],High(row));
end;
end.

View file

@ -33,7 +33,28 @@ compute_next([1, 1, 1 | R], [0 | R1]) :-
% the last four possibilies =>
% we consider that there is à 0 after the end
compute_next([0, 0], [0]).
complang jq># The 1-d cellular automaton:
def next:
# Conveniently, jq treats null as 0 when it comes to addition
# so there is no need to fiddle with the boundaries
. as $old
| reduce range(0; length) as $i
([];
($old[$i-1] + $old[$i+1]) as $s
| if $s == 0 then .[$i] = 0
elif $s == 1 then .[$i] = (if $old[$i] == 1 then 1 else 0 end)
else .[$i] = (if $old[$i] == 1 then 0 else 1 end)
end);
# pretty-print an array:
def pp: reduce .[] as $i (""; . + (if $i == 0 then " " else "*" end));
# continue until quiescence:
def go: recurse(. as $prev | next | if . == $prev then empty else . end) | pp;
# Example:
[0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0] | goute_next([0, 0], [0]).
compute_next([1, 0], [0]).

View file

@ -0,0 +1,30 @@
import <Utilities/Conversion.sl>;
main(args(2)) :=
run(args[1], stringToInt(args[2])) when size(args) = 2
else
"Usage error: exec <initialCells> <generations>";
stringToCells(string(1))[i] := 0 when string[i] = '_' else 1;
cellsToString(cells(1))[i] := '#' when cells[i] = 1 else '_';
run(cellsString(1), generations) :=
runHelper(stringToCells(cellsString), generations, cellsString);
runHelper(cells(1), generations, result(1)) :=
let
nextCells := step(cells);
in
result when generations = 0
else
runHelper(nextCells, generations - 1,
result ++ "\n" ++ cellsToString(nextCells));
step(cells(1))[i] :=
let
left := cells[i-1] when i > 1 else 0;
right := cells[i + 1] when i < size(cells) else 0;
in
1 when (left + cells[i] + right) = 2
else
0;

View file

@ -1,10 +1,10 @@
def (uca l) # new datatype: unidim CA
def (uca l) # new datatype: Uni-dimensional Cellular Automaton
(tag uca (list l len.l))
def (len l) :case (isa uca l) # how to compute its length
rep.l.1
defcoerce uca list # converting it to list
defcoerce uca list # how to convert it to a list
(fn(_) rep._.0)
def (pr l) :case (isa uca l) # how to print it

View file

@ -0,0 +1,5 @@
fcn life1D(line){
right:=line[1,*] + False; // shift left, False fill
left :=T(False).extend(line[0,-1]); // shift right
left.zip(line,right).apply(fcn(hood){ hood.sum(0)==2 });
}

View file

@ -0,0 +1,3 @@
chars:=T("_","#");
cells:="_###_##_#_#_#_#__#__".split("").apply('==("#")); //-->L(False,True,True,True,False...)
do(10){ cells.apply(chars.get).concat().println(); cells=life1D(cells); }

View file

@ -0,0 +1,7 @@
fcn life1D(line){
right:=line[1,*] + "_"; // shift left, "_" fill
left :="_" + line[0,-1]; // shift right
Utils.Helpers.zipWith(
fcn(a,b,c){ (String(a,b,c) - "_") == "##" and "#" or "_" },
left,line,right).concat();
}

View file

@ -0,0 +1,2 @@
cells:="_###_##_#_#_#_#__#__";
do(10){ cells.println(); cells=life1D(cells); }