P is for Practical PerlMonks

### comment on

 Need Help??
Here is a possible implementation of CheckSolution (for 3x3 only -- generalization is possible but it is left as an exercise for the reader):
```sub CheckSolution
{
my \$pNum = shift;

my @Sol=();

# Do some easy pair checks first.
my @digmap;
foreach my \$n (@\$pNum) {
my @digits = split //,\$n;
foreach (0..\$#digits) {
push @{\$digmap[\$_]->{\$digits[\$_]}},\$n;
}
}
# Two or more with same first digit.
my \$ref0 = \$digmap[0];
my @lst0 = grep { scalar(@{\$ref0->{\$_}}) > 1 } keys %\$ref0;
return @Sol if (! scalar(@lst0));

# Two or more with same middle digit.
my \$ref1 = \$digmap[1];
my @lst1 = grep { scalar(@{\$ref1->{\$_}}) > 1 } keys %\$ref1;
return @Sol if (! scalar(@lst1));

# Two or more with same last digit
my \$ref2 = \$digmap[2];
my @lst2 = grep { scalar(@{\$ref2->{\$_}}) > 1 } keys %\$ref2;
return @Sol if (! scalar(@lst2));

#print "Passed pair test\n";

# Use these lists for candidates for the three row positions.
my @uppers = map { @{\$ref0->{\$_}} } @lst0;
my @centers = map { @{\$ref1->{\$_}} } @lst1;
my @bottoms = map { @{\$ref2->{\$_}} } @lst2;

my (\$upper, \$center, \$bottom);
my (\$a, \$b, \$c, \$d, \$e, \$f, \$g, \$h, \$i);

# hash to simplify checking for duplicates.
my %wholeset = map { \$_ => 1 } @\$pNum;
my (%t1, %t2, %t3);
foreach \$upper (@uppers) {
%t1 = %wholeset;
\$t1{\$upper} = 0;
foreach \$center (@centers) {
next if (! \$t1{\$center});
%t2 = %t1;
\$t2{\$center} = 0;
foreach \$bottom (@bottoms) {
next if (! \$t2{\$bottom});
%t3 = %t2;
\$t3{\$bottom} = 0;

(\$a, \$b, \$c) = split //,\$upper;
(\$d, \$e, \$f) = split //,\$center;
(\$g, \$h, \$i) = split //,\$bottom;

\$adg = \$a . \$d . \$g;

\$beh = \$b . \$e . \$h;
next if (! \$t3{\$beh});
\$t3{\$beh} = 0;

\$cfi = \$c . \$f . \$i;
next if (! \$t3{\$cfi});

# Solution found - return it.
return (\$upper, \$center, \$bottom);
} # bottom
} # center
} #upper
return @Sol;
}
I tried this and it found the 132-792-660 (divisor 44) case in about 4 seconds.

In reply to Re: Re: Matrix Formation by tall_man
in thread Matrix Formation by artist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2020-11-27 18:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?