Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,29 @@
|
|||
fcn makeGrid(m,n,p){
|
||||
grid:=Data((m+1)*(n+1)); // first row and right edges are buffers
|
||||
grid.write(" "*m); grid.write("\r");
|
||||
do(n){
|
||||
do(m){ grid.write(((0.0).random(1)<p) and "+" or "."); } // cell is porous or not
|
||||
grid.write("\n");
|
||||
}
|
||||
grid
|
||||
}
|
||||
fcn ff(grid,x,m){ // walk across row looking for a porous cell
|
||||
if(grid[x]!=43) return(0); // '+' == 43 ASCII == porous
|
||||
grid[x]="#";
|
||||
return(x+m>=grid.len() or
|
||||
ff(grid,x+m,m) or ff(grid,x+1,m) or ff(grid,x-1,m) or ff(grid,x-m,m));
|
||||
}
|
||||
fcn percolate(grid,m){
|
||||
x:=m+1; i:=0; while(i<m and not ff(grid,x,m)){ x+=1; i+=1; }
|
||||
return(i<m); // percolated through the grid?
|
||||
}
|
||||
|
||||
grid:=makeGrid(15,15,0.60);
|
||||
println("Did liquid percolate: ",percolate(grid,15));
|
||||
println("15x15 grid:\n",grid.text);
|
||||
|
||||
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(15,15,p),15); }
|
||||
"p=%.1f: %.4f".fmt(p, cnt/10000).println();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue