Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Solution for:Sets Puzzle

The object of the game is to identify the 6 'Sets', of three cards each, from 12 cards laid out on the screen. Each card has a variation of the following four features:

  • 1.COLOR: Each card is red, green, or purple.
  • 2.SYMBOL: Each card contains ovals, squiggles, or diamonds.
  • 3.NUMBER: Each card has one, two, or three symbols.
  • 4.SHADING: Each card is solid, open, or striped.
A 'Set' consists of three cards in which each feature is EITHER the same on each card OR is different on each card. That is to say, any feature in the 'Set' of three cards is either common to all three cards or is different on each card.

Update 1:
Approach:I tried manually but didn't get all the 6 sets, so I decided to write a code for the same. The first idea came to my mind to create a unique ID for each card. ID should consists an entry for the feature. Instead of putting all the features into hash to read in program, I decided to generate the IDs as I get the data. So

Red    Diamond    2    open
will have ID: 2121, with each digit representing the feature in order. Similarly,
 Purple    Diamond    1    open 
will have ID: 3131.

Interesting part was to decide the match: I noticed that if I pick any two of the ID and third matching ID should be unique. Example, 1112 and 2213, then the third ID must be 3311. (12=>3 (different), 12=>3(different), 11=>1(unique), 23=>1(different)) If it is there in the given cards, we have found the set. Coming with calculation of 3rd ID was intersting, as I noticed that digits in the sum of each matching IDs (first,second and third) are even divisor of '3'. This algorithm, I found very interesting.

Thanks to halley and demerphq in chatterbox for interesting suggestions.

use strict; use warnings; my ($Card,$Lookup,$Feature,$Order); my (@card_ids,$order); # Generate IDs for the cards # And save the order for the cards while(<DATA>){ chomp; my $card = $_; my @fields = split /\t+/; my $type = 0; my $card_id = ''; foreach (@fields){ $type += 1; unless(defined $Lookup->{$type}{$_}){ $Feature->{$type} += 1; #Add the feature $Lookup->{$type}{$_} = $Feature->{$type}; } $card_id .= $Lookup->{$type}{$_}; } push @card_ids, $card_id; # print "$card_id\n"; $order++; $Order->{$card_id} = $order; $Card->{$order} =$card; } foreach ( @card_ids){ print "$_\t", $Order->{$_},"\n"; } # Get the matching cards @card_ids = sort @card_ids; foreach my $id_1 (@card_ids){ foreach my $id_2 (@card_ids){ next unless $id_1 < $id_2; group: foreach my $id_3 (@card_ids){ next unless $id_2 < $id_3; my $sum = $id_1 + $id_2 + $id_3; foreach (split //,$sum){ next group unless $_ % 3 == 0; #The Alogrithm } print join "\n", map {$Order->{$_}."\t". $Card->{$Order->{$_}}} + ($id_1,$id_2,$id_3); print "\n",'x' x 60,"\n"; } } }
__DATA__
Green	Diamond		3	open
Red	Squiggle	2	open
Red	Oval		2	solid
Purple	Diamond		2	open
Red	Squiggle	1	open
Green	Oval		3	solid
Green	Squiggle	1	open
Purple	Diamond		1	open
Red	Squiggle	2	stripped
Red	Diamond		3	stripped
Green	Oval		2	open
Red	Diamond		2	open

Output:
=======================================
1	Green	Diamond		3	open
7	Green	Squiggle	1	open
11	Green	Oval		2	open
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1	Green	Diamond		3	open
12	Red	Diamond		2	open
8	Purple	Diamond		1	open
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
6	Green	Oval		3	solid
9	Red	Squiggle	2	stripped
8	Purple	Diamond		1	open
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
11	Green	Oval		2	open
2	Red	Squiggle	2	open
4	Purple	Diamond		2	open
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
10	Red	Diamond		3	stripped
5	Red	Squiggle	1	open
3	Red	Oval		2	solid
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
12	Red	Diamond		2	open
9	Red	Squiggle	2	stripped
3	Red	Oval		2	solid
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
artist

In reply to Sets Puzzle by artist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-26 07:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found