Percolation/Mean cluster density: Difference between revisions

Content added Content deleted
m (J: extra credit)
(β†’β€Ž{{header|Perl 6}}: Add Perl 6 example)
Line 622: Line 622:
16 16 16 0 17 17 17 0 0 15 0 15 0 0 0
16 16 16 0 17 17 17 0 0 15 0 15 0 0 0
16 16 16 0 0 0 17 17 0 15 15 0 0 18 0</lang>
16 16 16 0 0 0 17 17 0 15 15 0 0 18 0</lang>

=={{header|Perl 6}}==
{{works with|Rakudo|2017.02}}

<lang perl6>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 -> $π˜”xπ˜• {
my $π˜’ = ( [+] perctest($π˜”xπ˜•) xx $trials ) / $trials;
say "𝘱 = 0.5, trials = $trials, π˜”,π˜• = $π˜”xπ˜•, π˜’ = $π˜’";
}

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]
}
}
</lang>
{{out}}
<pre>. . 1 . 2 . . 3 . . . 4 . . .
2 2 . . 2 2 2 . 5 5 . 4 4 4 4
2 2 2 2 2 . 2 . 5 . . 4 . . 4
2 . 2 . 2 2 . . . . 4 4 4 . 4
. . . . . 2 . . . . 4 4 4 . .
6 6 6 6 . . 7 7 . . 4 . 4 4 .
6 . 6 . . . . . 4 4 4 4 4 . .
6 6 6 . . . 8 8 . 4 4 4 . . 4
6 . . . 9 . . . . . . 4 4 . 4
. 10 . 11 . . 12 12 . 4 . . 4 4 4
11 . 11 11 11 11 . 12 . 4 . 4 4 4 .
11 11 11 11 11 11 11 . . 4 4 . . 4 .
11 11 11 11 . 11 . . . 4 4 4 4 4 .
11 11 11 . 11 11 11 . . 4 4 4 4 . 13
. 11 11 . 11 11 . . . . 4 4 . 14 .
𝘱 = 0.5, π˜”,π˜• = 15, π˜’ = 0.062222

𝘱 = 0.5, trials = 5, π˜”,π˜• = 10, π˜’ = 0.114
𝘱 = 0.5, trials = 5, π˜”,π˜• = 30, π˜’ = 0.082444
𝘱 = 0.5, trials = 5, π˜”,π˜• = 100, π˜’ = 0.06862
𝘱 = 0.5, trials = 5, π˜”,π˜• = 300, π˜’ = 0.066889
𝘱 = 0.5, trials = 5, π˜”,π˜• = 1000, π˜’ = 0.0659358
</pre>


=={{header|Python}}==
=={{header|Python}}==