Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,54 @@
|
|||
sysconf topleft
|
||||
global m[] .
|
||||
n = 15
|
||||
nn = n * n
|
||||
proc mkgrid p .
|
||||
m[] = [ ]
|
||||
for i to nn : if randomf < p
|
||||
m[] &= 0
|
||||
else
|
||||
m[] &= 1
|
||||
.
|
||||
.
|
||||
proc show .
|
||||
gbackground 000
|
||||
gclear
|
||||
sc = 100 / n
|
||||
for i to nn
|
||||
x = (i - 1) mod n
|
||||
y = (i - 1) div n
|
||||
if m[i] >= 1
|
||||
gcolor 999
|
||||
if m[i] = 2 : gcolor 225
|
||||
grect x * sc, y * sc, sc * 0.97, sc * 0.97
|
||||
.
|
||||
.
|
||||
.
|
||||
proc flood i .
|
||||
m[i] = 2
|
||||
if i > n and m[i - n] = 0 : flood (i - n)
|
||||
if i mod n <> 0 and m[i + 1] = 0 : flood (i + 1)
|
||||
if i <= nn - n and m[i + n] = 0 : flood (i + n)
|
||||
if i mod n <> 1 and m[i - 1] = 0 : flood (i - 1)
|
||||
.
|
||||
func test .
|
||||
for i to n : if m[i] = 0
|
||||
flood i
|
||||
.
|
||||
for i = nn - n + 1 to nn
|
||||
if m[i] = 2 : return 1
|
||||
.
|
||||
return 0
|
||||
.
|
||||
mkgrid 0.6
|
||||
sum = test
|
||||
show
|
||||
ntests = 100
|
||||
for p = 0.0 step 0.1 to 1.0
|
||||
sum = 0
|
||||
for i to ntests
|
||||
mkgrid p
|
||||
sum += test
|
||||
.
|
||||
print p & ": " & sum / ntests
|
||||
.
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
class Grid {
|
||||
constructor(rowCount, colCount, probability) {
|
||||
this.EMPTY = " ";
|
||||
this.FILLED = ".";
|
||||
this.PATH = "#";
|
||||
this.table = this.createGrid(rowCount, colCount, probability);
|
||||
}
|
||||
|
||||
createGrid(rowCount, colCount, probability) {
|
||||
const table = new Array(rowCount);
|
||||
for (let col = 0; col < rowCount; col++) {
|
||||
table[col] = new Array(colCount);
|
||||
for (let row = 0; row < colCount; row++) {
|
||||
table[col][row] = Math.random() < probability ? this.FILLED : this.EMPTY;
|
||||
}
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
percolate() {
|
||||
for (let x = 0; x < this.table[0].length; x++) {
|
||||
if (this.pathExists(x, 0)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
pathExists(x, y) {
|
||||
if (
|
||||
y < 0 ||
|
||||
x < 0 ||
|
||||
x >= this.table[0].length ||
|
||||
this.table[y][x] !== this.FILLED
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
this.table[y][x] = this.PATH;
|
||||
if (y === this.table.length - 1) {
|
||||
return true;
|
||||
}
|
||||
return (
|
||||
this.pathExists(x, y + 1) ||
|
||||
this.pathExists(x + 1, y) ||
|
||||
this.pathExists(x - 1, y) ||
|
||||
this.pathExists(x, y - 1)
|
||||
);
|
||||
}
|
||||
|
||||
display() {
|
||||
for (let col = 0; col < this.table.length; col++) {
|
||||
let rowStr = "";
|
||||
for (let row = 0; row < this.table[0].length; row++) {
|
||||
rowStr += " " + this.table[col][row];
|
||||
}
|
||||
console.log(rowStr);
|
||||
}
|
||||
console.log();
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
const rowCount = 15;
|
||||
const colCount = 15;
|
||||
const testCount = 1000;
|
||||
|
||||
const grid = new Grid(rowCount, colCount, 0.5);
|
||||
grid.percolate();
|
||||
grid.display();
|
||||
|
||||
console.log(`Proportion of ${testCount} tests that percolate through the grid:`);
|
||||
for (let probable = 0.0; probable <= 1.0; probable += 0.1) {
|
||||
let percolationCount = 0;
|
||||
for (let test = 0; test < testCount; test++) {
|
||||
const testGrid = new Grid(rowCount, colCount, probable);
|
||||
if (testGrid.percolate()) {
|
||||
percolationCount += 1;
|
||||
}
|
||||
}
|
||||
const percolationProportion = percolationCount / testCount;
|
||||
console.log(` p = ${probable.toFixed(1)}: ${percolationProportion.toFixed(4)}`);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
Loading…
Add table
Add a link
Reference in a new issue