in reply to Re^2: Perl6 Contest: Test your Skills
in thread Perl6 Contest: Test your Skills

Cool. Thanks for replying. :-) A few fixes, then.

You're absolutely right on the runs. Rewrite it as such:

# Runs SPAN: for 5 .. 3 -> $span { for 1 .. 11 -> $start { if all( @cardvals{ $start .. $start + $span } ) { $score += $span [*] %cardvals{ $start .. $start + $span}; last SPAN; } } }
Since %cardvals contains the number of each card, that's the number of times the run should be multiplied by. This also correctly handles 2,3,3,4,4. The span is 3 and the multiplier is 1*2*2, which results in 12.

Update: I'm a little reduce-happy. That should really be:$score += $span *<< %cardvals{ $start .. $start + $span};

You're also absolutely right on the fifteens. There are probably several ways to solve it. There's the brute-force method you used, which is perfectly adequate. However, I think we can use junctions to solve our problem. (Junctions rock, you know.)

# Fifteens $score += 2 * all( 15 == [+]@hand{ any( 0 .. 4 ) } };
I'm not sure that's legal. I've cross-posted it to P6L for @Larry to rip apart.

Update: After japhy's comments, it should probably look something like:

# Fifteens $score += 2 * all( 15 == [+]%hand{ any( 0 .. 4 ) } )>>.<num>;
I'm trying to figure out the correct way to handle the combinations as a single item.

  • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
  • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"

Replies are listed 'Best First'.
Re^4: Perl6 Contest: Test your Skills
by japhy (Canon) on May 25, 2005 at 18:39 UTC
    I don't know entirely how things work, but I think your bottom code has some flaws. First, does all() return the number of elements in the junction in scalar context? If so, then that's ok.

    Second, is it %hand or @hand? You're missing the array sigil with hash-slice syntax.

    Third, I think any(0..4) isn't good enough. That's only good for getting individual elements, I think. You need something that returns (0 | 1 | 2 | 3 | 4 | (0,1) | (0,2) | (0,3) | (0,4) | (1,2) | (1,3) | (1,4) | (2,3) | (2,4) | (3,4) | (0,1,2) | (0,1,3) | (0,1,4) | (0,2,3) | (0,2,4) | (0,3,4) | (1,2,3) | (1,2,4) | (1,3,4) | (2,3,4) | (0,1,2,3) | (0,1,2,4) | (0,2,3,4) | (1,2,3,4)). I don't know offhand how to use any() and all() to achieve that.


    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart