use strict; use Algorithm::Loops qw( MapCarE NestedLoops ); my @node= ( 'a'..'e', 1..5 ); # Build regex to exclude points that don't make a triangle: my $re= do { # Pairs of points that have no line between them: my @cant= MapCarE { join '', @_ } ['a'..'e'], [1..5]; push @cant, map { ( $_.(($_+2)%5), $_.(($_+3)%5) ) } 1..5; # Make regex fragments that match when a line isn't there: $_= "(?:[^$_]?[$_]){2}" for @cant; # Triplets of points that are co-linear: my @line; my $x= 'a32d'; my $y= 'a45c'; for( 1..5 ) { push @line, $x, $y; tr/abcde12345/bcdea23451/ for $x, $y; } # Make regex fragments that match have a line: $_= "[$_]{3}" for @line; "^(?:" . join( "|", @cant, @line ) . ")"; }; my $count= NestedLoops( [ [ 0 .. $#node ], ( sub { [ $_+1 .. $#node ] } ) x 2, ], sub { (1) x join('',@node[@_]) !~ /$re/o; # if( join('',@node[@_]) !~ /$re/o ) { # print "Yes @node[@_]\n"; # return 1; # } # print "No @node[@_]\n"; # return; }, ); print "$count triangles\n";