Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: How many triangles does your perl script "see"?

by eric256 (Parson)
on Oct 27, 2005 at 22:43 UTC ( [id://503492]=note: print w/replies, xml ) Need Help??


in reply to How many triangles does your perl script "see"?

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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://503492]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-24 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found