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,66 @@
my @perc;
my $fill = 'x';
enum Direction <DeadEnd Up Right Down Left>;
my $𝘒 = perctest(15);
.fmt("%-2s").say for @perc;
say "𝘱 = 0.5, 𝘕 = 15, 𝘒 = $𝘒\n";
my $trials = 5;
for 10, 30, 100, 300, 1000 -> $𝘕 {
my $𝘒 = ( [+] perctest($𝘕) xx $trials ) / $trials;
say "𝘱 = 0.5, trials = $trials, 𝘕 = $𝘕, 𝘒 = $𝘒";
}
sub infix:<deq> ( $a, $b ) { $a.defined && ($a eq $b) }
sub perctest ( $grid ) {
generate $grid;
my $block = 1;
for ^$grid X ^$grid -> ($y, $x) {
fill( [$x, $y], $block++ ) if @perc[$y; $x] eq $fill
}
($block - 1) / $grid²;
}
sub generate ( $grid ) {
@perc = ();
@perc.push: [ ( rand < .5 ?? '.' !! $fill ) xx $grid ] for ^$grid;
}
sub fill ( @cur, $block ) {
@perc[@cur[1]; @cur[0]] = $block;
my @stack;
my $current = @cur;
loop {
if my $dir = direction( $current ) {
@stack.push: $current;
$current = move $dir, $current, $block
}
else {
return unless @stack;
$current = @stack.pop
}
}
sub direction( [$x, $y] ) {
( Down if @perc[$y + 1][$x] deq $fill ) ||
( Left if @perc[$y][$x - 1] deq $fill ) ||
( Right if @perc[$y][$x + 1] deq $fill ) ||
( Up if @perc[$y - 1][$x] deq $fill ) ||
DeadEnd
}
sub move ( $dir, @cur, $block ) {
my ( $x, $y ) = @cur;
given $dir {
when Up { @perc[--$y; $x] = $block }
when Down { @perc[++$y; $x] = $block }
when Left { @perc[$y; --$x] = $block }
when Right { @perc[$y; ++$x] = $block }
}
[$x, $y]
}
}

View file

@ -0,0 +1,29 @@
const X=-1; // the sentinal that marks an untouched cell
var C,N,NN,P;
fcn createC(n,p){
N,P=n,p; NN=N*N;
C=NN.pump(List.createLong(NN),0); // vector of ints
foreach n in (NN){ C[n]=X*(Float.random(1)<=P) } // X is the sentinal
}
fcn showCluster{
alpha:="-ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz";
foreach n in ([0..NN,N]){ C[n,N].pump(String,alpha.get).println() }
}
fcn countClusters{
clusters:=0;
foreach n in (NN){
if(X!=C[n]) continue;
fcn(n,v){
if((0<=n<NN) and C[n]==X){
C[n]=v;
self.fcn(n-N,v); self.fcn(n-1,v); self.fcn(n+1,v); self.fcn(n+N,v);
}
}(n,clusters+=1);
}
clusters
}
fcn tests(N,n,p){
k:=0.0;
foreach z in (n){ createC(N,p); k+=countClusters().toFloat()/NN; }
k/n
}

View file

@ -0,0 +1,6 @@
createC(15,0.5);
println("width=%d, p=%.1f, %d clusters:".fmt(N,P,countClusters()));
showCluster();
println("p=0.5, 5 iterations:");
w:=4; do(6){ println("%5d %9.6f".fmt(w,tests(w, 5, 0.5))); w*=4; }