Free polyominoes enumeration: Difference between revisions

m (use wiki markup for links)
Line 1,572:
Number of free polyominoes of ranks 1 to 10:
1 1 2 5 12 35 108 369 1285 4655
</pre>
 
=={{header|Perl}}==
Only shows the polyominoes up to rank 5.
<lang perl>#!/usr/bin/perl
 
use strict;
use warnings;
 
my @new = "#\n";
 
for my $N ( 2 .. 10 )
{
@new = find( @new );
my %allbest;
$allbest{best($_)}++ for @new;
my @show = @new = sort keys %allbest;
printf "rank: %2d count: %d\n\n", $N, scalar @show;
if( @show <= 12 )
{
my $fmt = join '', map({ /\n/; '%' . ($+[0] + 1) . 's' } @show), "\n";
grep $_, @show and printf $fmt, map s/(.*)\n// && $1, @show for 0 .. $N;
print "\n";
}
}
 
sub bare
{
local $_ = shift;
s/^ *\n//gm;
s/^ //gm until /^#/m;
s/ $//gm until /#$/m;
$_;
}
 
sub transpose
{
local $_ = shift;
my $t = '';
$t .= "\n" while s/^./ $t .= $&; '' /gem;
$t;
}
 
sub rotate
{
local $_ = shift;
my $t = '';
$t .= "\n" while s/.$/ $t .= $&; '' /gem;
$t;
}
 
sub best
{
my %all = (shift, 1);
for my $p (keys %all)
{
$all{ my $tmp = rotate $p }++;
$all{ rotate $tmp }++;
}
$all{ transpose $_ }++ for keys %all;
$all{ s/(.+)/reverse $1/ger }++ for keys %all; # mirror
(sort keys %all)[-1];
}
 
sub find
{
my @before = @_;
my %new;
for my $p ( @before )
{
local $_ = $p;
s/^/ /gm;
s/\n/ \n/g;
my $line = s/\n.*/\n/sr =~ tr/\n/ /cr;
$_ = $line . $_ . $line;
my $n = -1 + length $line;
my $gap = qr/.{$n}/s;
$new{ bare "$`#$'" }++ while / (?=#)/g;
$new{ bare "$`#$'" }++ while / (?=$gap#)/g;
$new{ bare "$`#$'" }++ while /(?<=#) /g;
$new{ bare "$`#$'" }++ while /(?<=#$gap) /g;
}
keys %new;
}</lang>
{{out}}
<pre>
rank: 2 count: 1
 
##
 
rank: 3 count: 2
 
## ###
#
 
rank: 4 count: 5
 
## ## ### ### ####
## ## # #
 
rank: 5 count: 12
 
# ## ## ## ### ### ### ### ### #### #### #####
### # ## ## # # # # ## ## # #
# ## # # # #
 
rank: 6 count: 35
 
rank: 7 count: 108
 
rank: 8 count: 369
 
rank: 9 count: 1285
 
rank: 10 count: 4655
 
</pre>
 
Anonymous user