laziness, impatience, and hubris PerlMonks

Sets Puzzle

by artist (Parson)
 on Jul 08, 2003 at 17:51 UTC ( #272360=CUFP: 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

Replies are listed 'Best First'.
Re: Sets Puzzle
by halley (Prior) on Jul 08, 2003 at 18:44 UTC

I love the game. (It's just called "Set.") It's available in better game and toy stores around the USA. I think it came out in about 1993.

As a code review, I'd make two minor bits of feedback. Update: artist has revised the article in consideration of these tips.

(1) Single-letter identifiers are very hard to search for, and very easy to accidentally recycle. Try to use natural noun-like words instead of single letter symbols. \$count may be a count but what is \$C? What is \$p, \$q and \$r, and why would I want their \$sum?

(2) With such obtuse variable names, comments are even more important. You've got no comments at all (except for some debugging aids you've disabled). Strategy in comments, tactics in code. Write your algorithm in plain English in comments, and then translate that algorithm into specific code and data with Perl syntax.

As it is, the __DATA__ section and the writeup are far more descriptive than anything else in the code. Without these, it's a black box.

--
[ e d @ h a l l e y . c c ]

Create A New User
Node Status?
node history
Node Type: CUFP [id://272360]
Approved by sschneid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2020-10-28 22:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favourite web site is:

Results (264 votes). Check out past polls.

Notices?