Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,46 @@
// cell states
const FILLED=1; // and odd
const RWALL =2; // right wall
const BWALL =4; // bottom wall
fcn P(p,wall){ (0.0).random(1)<p and wall or 0 }
fcn makeGrid(m,n,p){
// Allocate two addition rows to avoid checking bounds.
// Bottom row is also required by drippage
grid:=Data(m*(n+2));
do(m){ grid.write(BWALL + RWALL); } // grid is topped with walls
do(n){
do(m-1){ grid.write( P(p,BWALL) + P(p,RWALL) ) }
grid.write(RWALL + P(p,BWALL)); // right border is all right wall, as is left border
}
do(m){ grid.write(0); } // for drips off the bottom of grid
grid
}
fcn show(grid,m,n){ n+=1;
println("+--"*m,"+");
foreach i in ([1..n]){ y:=i*m;
print(i==n and " " or "|"); // bottom row is special, otherwise always have left wall
foreach j in (m){ c:=grid[y + j];
print(c.bitAnd(FILLED) and "**" or " ", c.bitAnd(RWALL)and"|"or" ");
}
println();
if(i==n) return(); // nothing under the bottom row
foreach j in (m){ print((grid[y + j].bitAnd(BWALL)) and "+--" or "+ "); }
println("+");
}
}
fcn fill(grid,x,m){
if(grid[x].isOdd) return(False); // aka .bitAnd(FILLED) aka already been here
grid[x]+=FILLED;
if(x+m>=grid.len()) return(True); // success: reached bottom row
return(( not grid[x] .bitAnd(BWALL) and fill(grid,x + m,m) ) or // down
( not grid[x] .bitAnd(RWALL) and fill(grid,x + 1,m) ) or // right
( not grid[x - 1].bitAnd(RWALL) and fill(grid,x - 1,m) ) or // left
( not grid[x - m].bitAnd(BWALL) and fill(grid,x - m,m) )); // up
}
fcn percolate(grid,m){
i:=0; while(i<m and not fill(grid,i+m,m)){ i+=1; } // pour juice on top row
return(i<m); // percolated through the grid?
}

View file

@ -0,0 +1,8 @@
grid:=makeGrid(10,10,0.40);
println("Did liquid percolate: ",percolate(grid,10)); show(grid,10,10);
println("Running 10,000 tests for each case:");
foreach p in ([0.0 .. 1.0, 0.1]){
cnt:=0.0; do(10000){ cnt+=percolate(makeGrid(10,10,p),10); }
"p=%.1f: %.4f".fmt(p, cnt/10000).println();
}