Fun puzzle. Here is my kind of hybrid answer. 4 nested loops + some regex to weed out straight lines.
use strict;
use warnings;
my $data = {
A => [qw/B C D E F I G J/],
B => [qw/A C D E F H J I/],
C => [qw/A B F I D E/],
D => [qw/A C B G J E/],
E => [qw/A D C B G H I J/],
F => [qw/A C B I H J/],
G => [qw/D A E H I J/],
H => [qw/F B I J G E/],
I => [qw/B F C A H G E J/],
J => [qw/A B F H I E/],
};
my $line = qr/[ACFI]{3}|[ADGJ]{3}|[BCDE]{3}|[BFHJ]{3}|[EGHI]{3}/;
my $triangles = {};
for my $p1 (keys %$data) {
for my $p2 (@{$data->{$p1}}) {
for my $p3 (@{$data->{$p2}}) {
next if $p3 eq $p1;
for my $p4 (@{$data->{$p3}}) {
if ($p4 eq $p1) {
my $tri = join( "", sort @{[$p1,$p2, $p3]});
$triangles->{$tri}++ unless $tri =~ $line;
}
}
}
}
}
print $_,"\n" for sort keys %$triangles;
print 0+keys %$triangles;
___________
Eric Hodges
$_='y==QAe=e?y==QG@>@?iy==QVq?f?=a@iG?=QQ=Q?9';
s/(.)/ord($1)-50/eigs;tr/6123457/- \/|\\\_\n/;print;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.