Percolation/Site percolation: Difference between revisions

Content deleted Content added
→‎{{header|D}}: Marked incomplete
m correct D code to match requirements
Line 214:
 
=={{header|D}}==
{{incomplete|D|"p going from 0.0 to 1.0 in 0.1 increments" (not 0.02 increments)}}
 
{{trans|Python}}
<lang d>import std.stdio, std.random, std.array, std.datetime;
 
enum size_t nCols = 15,
nRows = 15,
nSteps = 5011, // Probability granularity: 0.0, 0.1 ... 1.0
nTries = 20_000100; // Simulation tries.
 
alias BaseType = char;
enum Cell : BaseType { empty = ' ',
Line 229 ⟶ 227:
visited = '.' }
alias Grid = Cell[nCols][nRows];
 
void initialize(ref Grid grid, in double probability,
ref Xorshift rng) {
Line 239 ⟶ 237:
}
}
 
void show(in ref Grid grid) {
immutable static line = '+' ~ "-".replicate(nCols) ~ "+";
Line 247 ⟶ 245:
line.writeln;
}
 
bool percolate(ref Grid grid) pure nothrow {
bool walk(in size_t r, in size_t c) nothrow {
enum bottom = nRows - 1;
grid[r][c] = Cell.visited;
 
if (r < bottom && grid[r + 1][c] == Cell.empty) { // Down.
if (walk(r + 1, c))
Line 258 ⟶ 256:
} else if (r == bottom)
return true;
 
if (c && grid[r][c - 1] == Cell.empty) // Left.
if (walk(r, c - 1))
return true;
 
if (c < nCols - 1 && grid[r][c + 1] == Cell.empty) // Right.
if (walk(r, c + 1))
return true;
 
if (r && grid[r - 1][c] == Cell.empty) // Up.
if (walk(r - 1, c))
return true;
 
return false;
}
 
enum startR = 0;
foreach (immutable c; 0 .. nCols)
Line 281 ⟶ 279:
return false;
}
 
void main() {
static struct Counter {
Line 287 ⟶ 285:
size_t count;
}
 
StopWatch sw;
sw.start;
 
enum probabilityStep = 1.0 / (nSteps - 1);
Counter[nSteps] counters;
foreach (immutable i, ref co; counters)
co.prob = i * probabilityStep;
 
Grid grid;
bool sampleShown = false;
auto rng = Xorshift(unpredictableSeed);
 
foreach (ref co; counters) {
foreach (immutable _; 0 .. nTries) {
Line 316 ⟶ 314:
}
sw.stop;
 
writefln("\nFraction of %d tries that percolate through:", nTries);
foreach (const co; counters)
writefln("%1.3f1f %1.3f", co.prob, co.count / cast(double)nTries);
 
writefln("\nSimulations and grid printing performed" ~
" in %32.2f3f seconds.", sw.peek.msecs / 1000.0);
}</lang>
{{out}}
<pre>Percolating sample (15x15, probability = 0.3050):
+---------------+
|.####....#. #### # |
|# ###... #.#.# # # ##|
|## ###.#. ####....# #|
|# ### ...# .# .#. #|
| ..# # ..#..##. ## ## |
|## # #..#.##.##..# #|
|# ....# ###. #. # |
|##.# ## .##.... # |
| ....##### . .##.## |
| #.# ###. .# # ### |
| #.##.##. # #### |
|#####....## .## ##|
| #### .##.## ## #|
| # ###.. .# ## # |
| ## ### .### ### #|
+---------------+
 
Fraction of 20000100 tries that percolate through:
0.0000 0.000
0.0201 0.000
0.0402 0.000
0.0603 0.000
0.0804 0.000
0.1005 0.000090
0.1206 0.000570
0.1407 0.000950
0.1608 01.000
0.1809 01.000
1.0.200 01.000
0.220 0.000
0.240 0.000
0.260 0.000
0.280 0.000
0.300 0.000
0.320 0.000
0.340 0.000
0.360 0.001
0.380 0.002
0.400 0.004
0.420 0.007
0.440 0.015
0.460 0.029
0.480 0.054
0.500 0.094
0.520 0.152
0.540 0.232
0.560 0.331
0.580 0.445
0.600 0.565
0.620 0.677
0.640 0.782
0.660 0.860
0.680 0.918
0.700 0.959
0.720 0.978
0.740 0.992
0.760 0.997
0.780 0.999
0.800 1.000
0.820 1.000
0.840 1.000
0.860 1.000
0.880 1.000
0.900 1.000
0.920 1.000
0.940 1.000
0.960 1.000
0.980 1.000
 
Simulations and grid printing performed in 50.28046 seconds.</pre>
</pre>
 
=={{header|Python}}==