Set puzzle: Difference between revisions

add Tailspin solution
(Added Prolog)
(add Tailspin solution)
Line 3,604:
green oval two open
red diamond two striped
</pre>
 
=={{header|Tailspin}}==
Dealing cards at random to the size of the desired hand, then trying again if the desired set count is not achieved. Unfortunately Tailspin is still very slow and the 9/4 scenario seems unlikely enough that it isn't happening anytime soon. So demonstrating by generating 9 cards with exactly 3 sets.
<lang tailspin>
def deck: [ 1..3 -> (def colour: $;
1..3 -> (def symbol: $;
1..3 -> (def number: $;
1..3 -> {colour: $colour, symbol: $symbol, number: $number, shading: $} !
) !
) !
)
];
 
templates deal
@: $deck;
[ 1..$ -> ($@deal::length -> SYS::randomInt -> ^@deal($ + 1) !)] !
end deal
 
templates isSet
def set : $;
[ $(1).colour + $(2).colour + $(3).colour, $(1).symbol + $(2).symbol + $(3).symbol,
$(1).number + $(2).number + $(3).number, $(1).shading + $(2).shading + $(3).shading ] -> #
// if it is not an array that contains an element that is not 3, 6 or 9, it is a set
<~[<~3|6|9>]> $set !
end isSet
 
templates findSets
def hand: $;
[ 1..$hand::length - 3 -> (def a: $;
$a+1..$hand::length - 3 -> (def b: $;
$b+1..$hand::length - 3 -> $hand([$a, $b, $]) !
) !
) -> isSet ] !
end findSets
 
templates setPuzzle
def nCards: $(1);
def nSets: $(2);
{} -> #
<{sets: <[]($nSets)>}> $ !
<>
def hand: $nCards -> deal;
{hand: $hand, sets: $hand -> findSets} -> #
end setPuzzle
 
templates formatCard
def colours: ['red', 'green', 'purple'];
def symbols: ['oval', 'squiggle', 'diamond'];
def numbers: ['one', 'two', 'three'];
def shadings: ['solid', 'open', 'striped'];
$ -> '$colours($.colour);-$symbols($.symbol);-$numbers($.number);-$shadings($.shading);' !
end formatCard
 
templates formatSets
$ -> 'hand:
$.hand... -> '$ -> formatCard;
';
sets:
$.sets... -> '[$... -> ' $ -> formatCard; ';]
';' !
end formatSets
 
[9,3] -> setPuzzle -> formatSets -> !OUT::write
</lang>
{{out}}
<pre>
hand:
green-diamond-three-open
purple-squiggle-one-striped
green-diamond-one-striped
purple-squiggle-three-open
purple-squiggle-two-solid
red-oval-three-open
purple-diamond-three-open
red-oval-one-solid
green-oval-three-solid
 
sets:
[ green-diamond-three-open purple-squiggle-three-open red-oval-three-open ]
[ purple-squiggle-one-striped purple-squiggle-three-open purple-squiggle-two-solid ]
[ green-diamond-one-striped purple-squiggle-two-solid red-oval-three-open ]
</pre>
 
Anonymous user