in reply to Solver for the game "Set" matches three times

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        

Replies are listed 'Best First'.
Re^2: Solver for the game "Set" matches three times (octal)
by Anonymous Monk on Jul 16, 2008 at 23:01 UTC
    This might sound stupid, but what does      return ($a|$b|$c) eq ($a^$b^$c); do?

      For each bit, ($a^$b^$c) returns

      • 1 if all three cards share the value represented by that bit (e.g. all three cards are green),
      • 0 if exactly two of three cards share the value represented by that bit (e.g. two of the three cards are green, and the one others isn't),
      • 1 if exactly one of three cards share the value represented by that bit (e.g. one of the three cards is green, and the two others aren't),
      • 0 if none of the three cards share the value represented by that bit (e.g. none of the cards are green).

      We want

      • 1 if all three cards share the value represented by that bit (e.g. all three cards are green),
      • 0 if exactly two of three cards share the value represented by that bit (e.g. two of the three cards are green, and the one others isn't),
      • 1 if exactly one of three cards share the value represented by that bit (e.g. one of the three cards is green, and the two others aren't),
      • 1 if none of the three cards share the value represented by that bit (e.g. none of the cards are green).

      ($a|$b|$c) eq serves that purpose and to "and" the results of every bit into a single boolean value.