Hi Chris,

I hope you don't mind.  With the idea of getting back into Perl6, I've taken 
the liberty of rewriting your code to clean it up a bit (somewhat 
successfully), and make it more "perl6ish" (somewhat unsuccessfully).  The only 
significant issue I have with my version is the terribly nested loop which I'm 
sure could be cleaned up some more.

Also, because Pugs is so slow, I've included some performance hacks in it.  It 
originally was taking about 4 minutes to run on my computer.  It now takes 
about 50 seconds.

If anyone can offer a better/cleaner version, I'd love to see it.

Cheers,
Ovid
 
-- If this message is a response to a question on a mailing list, please send 
follow up questions to the list.
 
Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/--

my %buckets = (
    'w' => {
        'count' => 4,
        'scale' => 10.5,
        'array' => [],
     },
    'x' => {
        'count' => 6,
        'scale' => 7,
        'array' => [],
     },
    'y' => {
        'count' => 12,
        'scale' => 3,
        'array' => [],
     },
    'z' => {
        'count' => 18,
        'scale' => 2,
        'array' => [],
     },
);

for %buckets.kv -> my $bucket, $arg_for {
    for 0 .. $arg_for{'count'} -> $index {
        $arg_for{'array'}.push($index * $arg_for{'scale'});
    }
}

my int @results;
my int $target = 35;

my $w_bucket = %buckets{'w'};
for 0 .. $w_bucket{'count'} -> my $i {
    say "To 4: $i";
    my $w = $w_bucket{'array'}[$i];

    last if $w > $target;

    my $x_bucket = %buckets{'x'};
    for 0 .. $x_bucket{'count'} -> my $j {
        say "  To 6: $j";
        my $x = $x_bucket{'array'}[$j];

        last if ($w, $x).sum > $target;

        my $y_bucket = %buckets{'y'};
        for 0 .. $y_bucket{'count'} -> my $k {
            my $y = $y_bucket{'array'}[$k];

            last if ($w, $x, $y).sum > $target;

            my $z_bucket = %buckets{'z'};
            for 0 .. $z_bucket{'count'} -> my $l {
                my $z = $z_bucket{'array'}[$l];

                if( $target == ($w, $x, $y, $z).sum ) {
                    @results.push( [$i, $j, $k, $l] );
                }
            }
        }
    }
}

my $counter = 0;
for @results -> my $result {
    say "$counter: " ~ $result.join(" | ");
    $counter++;
}





Reply via email to