Abelian sandpile model: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl}}: future-proof for 5.36, use new bitwise string operators)
Line 2,659: Line 2,659:


=={{header|Perl}}==
=={{header|Perl}}==
<syntaxhighlight lang="perl">#!/usr/bin/perl
<syntaxhighlight lang="perl">use strict;

use strict; # http://www.rosettacode.org/wiki/Abelian_sandpile_model
use warnings;
use warnings;
use feature 'bitwise';


my ($high, $wide) = split ' ', qx(stty size);
my ($high, $wide) = split ' ', qx(stty size);
Line 2,669: Line 2,668:
my $pile = $mask =~ s/\177/ rand() < 0.02 ? chr 64 + rand 20 : "\0" /ger;
my $pile = $mask =~ s/\177/ rand() < 0.02 ? chr 64 + rand 20 : "\0" /ger;


for ( 1 .. 1e6 )
for (1 .. 1e6)
{
{
print "\e[H", $pile =~ tr/\0-\177/ 1-~/r, "\n$_";
print "\e[H", $pile =~ tr/\0-\177/ 1-~/r, "\n$_";
Line 2,677: Line 2,676:
for ("\0$add", "\0" x $wide . $add, substr($add, 1), substr $add, $wide)
for ("\0$add", "\0" x $wide . $add, substr($add, 1), substr $add, $wide)
{
{
$pile |= $_;
$pile |.= $_;
$pile =~ tr/\200-\377/\1-\176/; # add one to each neighbor of >=4
$pile =~ tr/\200-\377/\1-\176/; # add one to each neighbor of >=4
$pile &= $mask;
$pile &.= $mask;
}
}
select undef, undef, undef, 0.1; # comment out for full speed
select undef, undef, undef, 0.1; # comment out for full speed