Here is a cute trick that you might want to contemplate.

If you make each card a 4-"digit" octal number, then three cards form a "set" if and only if ($a|$b|$c) equals ($a^$b^$c). You can use octal strings or octal numbers. I use octal strings below:

#!/usr/bin/perl -w use strict; my %Desc= qw( 0001 One 0002 Two 0004 Three 0010 Red 0020 Blue 0040 Green 0100 Hollow 0200 Striped 0400 Solid 1000 Diamond 2000 Squiggle 4000 Oval ); my @deck; for my $a ( 1,2,4 ) { for my $b ( 1,2,4 ) { for my $c ( 1,2,4 ) { for my $d ( 1,2,4 ) { push @deck, "$a$b$c$d"; } } } } sub isSet { my( $a, $b, $c )= @_; return ($a|$b|$c) eq ($a^$b^$c); } sub showCard { my( $card )= @_; my @desc; for my $bit ( sort keys %Desc ) { if( $bit eq ( $bit & $card ) ) { push @desc, $Desc{$bit}; } } return "@desc"; } while( 1 ) { for my $n ( reverse @deck-2..@deck ) { my $r = rand $n; @deck[$n-1,$r]= @deck[$r,$n-1]; } if( isSet( @deck[-3..-1] ) ) { print showCard( $_ ), $/ for @deck[-3..-1]; exit; } }

Update: Replaced nested maps with clearer, shorter for()s.

- tye        


In reply to Re: Solver for the game "Set" matches three times (octal) by tye
in thread Solver for the game "Set" matches three times by skrapasor

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.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.