Set puzzle: Difference between revisions

Content added Content deleted
(→‎{{header|Picat}}: Added subsection)
m (syntax highlighting fixup automation)
Line 80: Line 80:
We start with the specification of a package "Set_Puzzle.
We start with the specification of a package "Set_Puzzle.


<lang Ada>package Set_Puzzle is
<syntaxhighlight lang="ada">package Set_Puzzle is
type Three is range 1..3;
type Three is range 1..3;
Line 97: Line 97:
-- calls Do_Something once for each set it finds.
-- calls Do_Something once for each set it finds.
end Set_Puzzle;</lang>
end Set_Puzzle;</syntaxhighlight>


Now we implement the package "Set_Puzzle".
Now we implement the package "Set_Puzzle".


<lang Ada>with Ada.Numerics.Discrete_Random;
<syntaxhighlight lang="ada">with Ada.Numerics.Discrete_Random;


package body Set_Puzzle is
package body Set_Puzzle is
Line 178: Line 178:
begin
begin
Rand.Reset(R);
Rand.Reset(R);
end Set_Puzzle;</lang>
end Set_Puzzle;</syntaxhighlight>


Finally, we write the main program, using the above package. It reads two parameters from the command line. The first parameter describes the number of cards, the second one the number of sets. Thus, for the basic mode one has to call "puzzle 9 4", for the advanced mode "puzzle 12 6", but the program would support any other combination of parameters just as well.
Finally, we write the main program, using the above package. It reads two parameters from the command line. The first parameter describes the number of cards, the second one the number of sets. Thus, for the basic mode one has to call "puzzle 9 4", for the advanced mode "puzzle 12 6", but the program would support any other combination of parameters just as well.


<lang Ada>with Ada.Text_IO, Set_Puzzle, Ada.Command_Line;
<syntaxhighlight lang="ada">with Ada.Text_IO, Set_Puzzle, Ada.Command_Line;


procedure Puzzle is
procedure Puzzle is
Line 231: Line 231:
Print_Sets(Cards); -- print the sets
Print_Sets(Cards); -- print the sets
end Puzzle;</lang>
end Puzzle;</syntaxhighlight>


{{out}}
{{out}}
Line 263: Line 263:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang autohotkey>; Generate deck; card encoding from Raku
<syntaxhighlight lang="autohotkey">; Generate deck; card encoding from Raku
Loop, 81
Loop, 81
deck .= ToBase(A_Index-1, 3)+1111 ","
deck .= ToBase(A_Index-1, 3)+1111 ","
Line 366: Line 366:
c%j% += 1
c%j% += 1
}
}
}</lang>
}</syntaxhighlight>
{{out|Sample output}}
{{out|Sample output}}
<pre>Dealt 9 cards:
<pre>Dealt 9 cards:
Line 446: Line 446:
=={{header|C}}==
=={{header|C}}==
Brute force. Each card is a unique number in the range of [0,81]. Randomly deal a hand of cards until exactly the required number of sets are found.
Brute force. Each card is a unique number in the range of [0,81]. Randomly deal a hand of cards until exactly the required number of sets are found.
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>


Line 540: Line 540:


return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|C sharp}}==
=={{header|C sharp}}==
{{works with|C sharp|8}}
{{works with|C sharp|8}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using static System.Linq.Enumerable;
using static System.Linq.Enumerable;
Line 628: Line 628:
static bool AreSameOrDifferent(int a, int b, int c) => (a + b + c) % 3 == 0;
static bool AreSameOrDifferent(int a, int b, int c) => (a + b + c) % 3 == 0;
static IEnumerable<int> To(this int start, int end) => Range(start, end - start - 1);
static IEnumerable<int> To(this int start, int end) => Range(start, end - start - 1);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:30ex;overflow:scroll">
<pre style="height:30ex;overflow:scroll">
Line 673: Line 673:
=={{header|C++}}==
=={{header|C++}}==
{{trans|Java}}
{{trans|Java}}
<lang cpp>
<syntaxhighlight lang="cpp">
#include <time.h>
#include <time.h>
#include <algorithm>
#include <algorithm>
Line 805: Line 805:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 880: Line 880:
=={{header|Ceylon}}==
=={{header|Ceylon}}==
Add import ceylon.random "1.3.3" to your module.ceylon file
Add import ceylon.random "1.3.3" to your module.ceylon file
<lang ceylon>import ceylon.random {
<syntaxhighlight lang="ceylon">import ceylon.random {
Random,
Random,
DefaultRandom
DefaultRandom
Line 1,019: Line 1,019:
}
}


}</lang>
}</syntaxhighlight>


=={{header|D}}==
=={{header|D}}==
===Basic Version===
===Basic Version===
<lang d>import std.stdio, std.random, std.array, std.conv, std.traits,
<syntaxhighlight lang="d">import std.stdio, std.random, std.array, std.conv, std.traits,
std.exception, std.range, std.algorithm;
std.exception, std.range, std.algorithm;


Line 1,118: Line 1,118:
writefln("\nFOUND %d SET%s:", len, len == 1 ? "" : "S");
writefln("\nFOUND %d SET%s:", len, len == 1 ? "" : "S");
writefln("%(%(%s\n%)\n\n%)", sets);
writefln("%(%(%s\n%)\n\n%)", sets);
}</lang>
}</syntaxhighlight>
{{out|Sample output}}
{{out|Sample output}}
<pre>DEALT 9 CARDS:
<pre>DEALT 9 CARDS:
Line 1,150: Line 1,150:
===Short Version===
===Short Version===
This requires the third solution module of the Combinations Task.
This requires the third solution module of the Combinations Task.
<lang d>void main() {
<syntaxhighlight lang="d">void main() {
import std.stdio, std.algorithm, std.range, std.random, combinations3;
import std.stdio, std.algorithm, std.range, std.random, combinations3;


Line 1,168: Line 1,168:
writefln("Dealt %d cards:\n%(%-(%8s %)\n%)\n", draw.length, draw);
writefln("Dealt %d cards:\n%(%-(%8s %)\n%)\n", draw.length, draw);
writefln("Containing:\n%(%(%-(%8s %)\n%)\n\n%)", sets);
writefln("Containing:\n%(%(%-(%8s %)\n%)\n\n%)", sets);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Dealt 9 cards:
<pre>Dealt 9 cards:
Line 1,199: Line 1,199:


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
<lang scheme>
<syntaxhighlight lang="scheme">
(require 'list)
(require 'list)


Line 1,250: Line 1,250:
))
))


</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,311: Line 1,311:
=={{header|Elixir}}==
=={{header|Elixir}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang elixir>defmodule RC do
<syntaxhighlight lang="elixir">defmodule RC do
def set_puzzle(deal, goal) do
def set_puzzle(deal, goal) do
{puzzle, sets} = get_puzzle_and_answer(deal, goal, produce_deck)
{puzzle, sets} = get_puzzle_and_answer(deal, goal, produce_deck)
Line 1,364: Line 1,364:


RC.set_puzzle(9, 4)
RC.set_puzzle(9, 4)
RC.set_puzzle(12, 6)</lang>
RC.set_puzzle(12, 6)</syntaxhighlight>


{{out}}
{{out}}
Line 1,438: Line 1,438:
=={{header|Erlang}}==
=={{header|Erlang}}==
Until a better solution is found this is one.
Until a better solution is found this is one.
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( set ).
-module( set ).


Line 1,522: Line 1,522:
make_sets_acc( true, Set, Sets ) -> [Set | Sets];
make_sets_acc( true, Set, Sets ) -> [Set | Sets];
make_sets_acc( false, _Set, Sets ) -> Sets.
make_sets_acc( false, _Set, Sets ) -> Sets.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,582: Line 1,582:


=={{header|F Sharp|F#}}==
=={{header|F Sharp|F#}}==
<lang fsharp>open System
<syntaxhighlight lang="fsharp">open System


type Number = One | Two | Three
type Number = One | Two | Three
Line 1,663: Line 1,663:
solve 12 6
solve 12 6


playGame()</lang>
playGame()</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 1,734: Line 1,734:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: arrays backtrack combinators.short-circuit formatting
<syntaxhighlight lang="factor">USING: arrays backtrack combinators.short-circuit formatting
fry grouping io kernel literals math.combinatorics math.matrices
fry grouping io kernel literals math.combinatorics math.matrices
prettyprint qw random sequences sets ;
prettyprint qw random sequences sets ;
Line 1,775: Line 1,775:
12 6 set-puzzle ;
12 6 set-puzzle ;


MAIN: main</lang>
MAIN: main</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:65ex">
<pre style="height:65ex">
Line 1,847: Line 1,847:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,920: Line 1,920:
fmt.Printf(" %s\n %s\n %s\n",s[0],s[1],s[2])
fmt.Printf(" %s\n %s\n %s\n",s[0],s[1],s[2])
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,992: Line 1,992:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Control.Monad.State
<syntaxhighlight lang="haskell">import Control.Monad.State
(State, evalState, replicateM, runState, state)
(State, evalState, replicateM, runState, state)
import System.Random (StdGen, newStdGen, randomR)
import System.Random (StdGen, newStdGen, randomR)
Line 2,094: Line 2,094:
main = do
main = do
showSolutions 9 4
showSolutions 9 4
showSolutions 12 6</lang>
showSolutions 12 6</syntaxhighlight>
{{out}}
{{out}}
<pre style="font-size:80%">Showing hand of 9 cards with 4 solutions.
<pre style="font-size:80%">Showing hand of 9 cards with 4 solutions.
Line 2,167: Line 2,167:
=={{header|J}}==
=={{header|J}}==
'''Solution:'''
'''Solution:'''
<lang j>require 'stats/base'
<syntaxhighlight lang="j">require 'stats/base'


Number=: ;:'one two three'
Number=: ;:'one two three'
Line 2,190: Line 2,190:
echo LF,'Found ',(":target),' Sets:'
echo LF,'Found ',(":target),' Sets:'
echo sayCards sort"2 getSets Hand
echo sayCards sort"2 getSets Hand
)</lang>
)</syntaxhighlight>


'''Example:'''
'''Example:'''
<lang j> set_puzzle 9
<syntaxhighlight lang="j"> set_puzzle 9
Dealt 9 Cards:
Dealt 9 Cards:
one, red, solid, oval
one, red, solid, oval
Line 2,220: Line 2,220:
three, red, open, oval
three, red, open, oval
three, green, solid, oval
three, green, solid, oval
three, purple, striped, oval </lang>
three, purple, striped, oval </syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
<lang java>import java.util.*;
<syntaxhighlight lang="java">import java.util.*;


public class SetPuzzle {
public class SetPuzzle {
Line 2,353: Line 2,353:
return tot == 0;
return tot == 0;
}
}
}</lang>
}</syntaxhighlight>
<pre>GIVEN 12 CARDS:
<pre>GIVEN 12 CARDS:


Line 2,397: Line 2,397:
=={{header|Julia}}==
=={{header|Julia}}==
Plays one basic game and one advanced game.
Plays one basic game and one advanced game.
<lang julia>using Random, IterTools, Combinatorics
<syntaxhighlight lang="julia">using Random, IterTools, Combinatorics


function SetGameTM(basic = true)
function SetGameTM(basic = true)
Line 2,440: Line 2,440:
SetGameTM()
SetGameTM()
SetGameTM(false)
SetGameTM(false)
</lang> {{output}} <pre>
</syntaxhighlight> {{output}} <pre>
Dealt 9 cards:
Dealt 9 cards:
("green", "one", "oval", "open")
("green", "one", "oval", "open")
Line 2,510: Line 2,510:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.1.3
<syntaxhighlight lang="scala">// version 1.1.3


import java.util.Collections.shuffle
import java.util.Collections.shuffle
Line 2,598: Line 2,598:
println()
println()
playGame(Degree.ADVANCED)
playGame(Degree.ADVANCED)
}</lang>
}</syntaxhighlight>


Sample output:
Sample output:
Line 2,678: Line 2,678:
A simple brute force approach. This code highlights two things: 1) a few of Mathematica's "higher-level" functions such as Tuples and Subsets and 2) the straightforwardness enabled by the language's "dynamic typing" (more precisely, its symbolic semantics) and its usage of lists for everything (in this particular example, the fact that functions such as Tuples and Entropy can be used on lists with arbitrary content).
A simple brute force approach. This code highlights two things: 1) a few of Mathematica's "higher-level" functions such as Tuples and Subsets and 2) the straightforwardness enabled by the language's "dynamic typing" (more precisely, its symbolic semantics) and its usage of lists for everything (in this particular example, the fact that functions such as Tuples and Entropy can be used on lists with arbitrary content).


<lang Mathematica>colors = {Red, Green, Purple};
<syntaxhighlight lang="mathematica">colors = {Red, Green, Purple};
symbols = {"0", "\[TildeTilde]", "\[Diamond]"};
symbols = {"0", "\[TildeTilde]", "\[Diamond]"};
numbers = {1, 2, 3};
numbers = {1, 2, 3};
Line 2,693: Line 2,693:
count = Count[Subsets[cards, {3}], _?validSetQ]];
count = Count[Subsets[cards, {3}], _?validSetQ]];
cards];
cards];
Row[{Style[#2, #1], #3, #4}] & @@@ deal[{9, 4}]</lang>
Row[{Style[#2, #1], #3, #4}] & @@@ deal[{9, 4}]</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import algorithm, math, random, sequtils, strformat, strutils
<syntaxhighlight lang="nim">import algorithm, math, random, sequtils, strformat, strutils


type
type
Line 2,777: Line 2,777:
playGame(Basic)
playGame(Basic)
echo()
echo()
playGame(Advanced)</lang>
playGame(Advanced)</syntaxhighlight>


{{out}}
{{out}}
Line 2,853: Line 2,853:


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang parigp>dealraw(cards)=vector(cards,i,vector(4,j,1<<random(3)));
<syntaxhighlight lang="parigp">dealraw(cards)=vector(cards,i,vector(4,j,1<<random(3)));
howmany(a,b,c)=hammingweight(bitor(a,bitor(b,c)));
howmany(a,b,c)=hammingweight(bitor(a,bitor(b,c)));
name(v)=Str(["red","green",0,"purple"][v[1]],", ",["oval","squiggle",0,"diamond"][v[2]],", ",["one","two",0,"three"][v[3]],", ",["solid","open",0,"striped"][v[4]]);
name(v)=Str(["red","green",0,"purple"][v[1]],", ",["oval","squiggle",0,"diamond"][v[2]],", ",["one","two",0,"three"][v[3]],", ",["solid","open",0,"striped"][v[4]]);
Line 2,880: Line 2,880:
};
};
deal(9,4)
deal(9,4)
deal(12,6)</lang>
deal(12,6)</syntaxhighlight>
{{output}}
{{output}}
<pre>green, diamond, one, open
<pre>green, diamond, one, open
Line 2,950: Line 2,950:
It's actually slightly simplified, since generating Enum classes
It's actually slightly simplified, since generating Enum classes
and objects would be overkill for this particular task.
and objects would be overkill for this particular task.
<lang perl>#!perl
<syntaxhighlight lang="perl">#!perl
use strict;
use strict;
use warnings;
use warnings;
Line 3,029: Line 3,029:


__END__
__END__
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>Drew 12 cards:
<pre>Drew 12 cards:
Line 3,059: Line 3,059:
=={{header|Phix}}==
=={{header|Phix}}==
Converts cards 1..81 (that idea from C) to 1/2/4 [/7] (that idea from Perl) but inverts the validation
Converts cards 1..81 (that idea from C) to 1/2/4 [/7] (that idea from Perl) but inverts the validation
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">comb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">pool</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">needed</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">={},</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">done</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">chosen</span><span style="color: #0000FF;">={})</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">comb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">pool</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">needed</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">={},</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">done</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">chosen</span><span style="color: #0000FF;">={})</span>
Line 3,127: Line 3,127:
<span style="color: #000080;font-style:italic;">--play(12,6)
<span style="color: #000080;font-style:italic;">--play(12,6)
--play(9,6)</span>
--play(9,6)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 3,161: Line 3,161:
=={{header|Picat}}==
=={{header|Picat}}==
The problem generator check that it problem has exactly one solution, so that step can take a little time (some seconds). <code>fail/0</code> is used to check for unicity of the solution.
The problem generator check that it problem has exactly one solution, so that step can take a little time (some seconds). <code>fail/0</code> is used to check for unicity of the solution.
<lang Picat>import util.
<syntaxhighlight lang="picat">import util.
import cp.
import cp.


Line 3,334: Line 3,334:
],
],
SetLen = 3,
SetLen = 3,
Wanted = 4.</lang>
Wanted = 4.</syntaxhighlight>




Line 3,403: Line 3,403:
===Constraint model===
===Constraint model===
Here is the additional code for a '''constraint model'''. Note that the constraint solver only handles integers so the features must be converted to integers. To simplify, the random instance generator does not check for unicity of the problem instance, so it can have (and often have) a lot of solutions.
Here is the additional code for a '''constraint model'''. Note that the constraint solver only handles integers so the features must be converted to integers. To simplify, the random instance generator does not check for unicity of the problem instance, so it can have (and often have) a lot of solutions.
<lang Picat>go4 =>
<syntaxhighlight lang="picat">go4 =>
NumCards = 18,
NumCards = 18,
NumWanted = 9,
NumWanted = 9,
Line 3,485: Line 3,485:
%
%
generate_instance2(NumCards,_NumSets,_SetLen, Cards) =>
generate_instance2(NumCards,_NumSets,_SetLen, Cards) =>
Cards = random_deal(NumCards).</lang>
Cards = random_deal(NumCards).</syntaxhighlight>


{{out}}
{{out}}
Line 3,537: Line 3,537:
=={{header|Prolog}}==
=={{header|Prolog}}==


<lang Prolog>do_it(N) :-
<syntaxhighlight lang="prolog">do_it(N) :-
card_sets(N, Cards, Sets),
card_sets(N, Cards, Sets),
!,
!,
Line 3,581: Line 3,581:
match([A|T1],[B|T2],[C|T3]) :-
match([A|T1],[B|T2],[C|T3]) :-
dif(A,B), dif(B,C), dif(A,C),
dif(A,B), dif(B,C), dif(A,C),
match(T1,T2,T3).</lang>
match(T1,T2,T3).</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,659: Line 3,659:


=={{header|Python}}==
=={{header|Python}}==
<lang python>#!/usr/bin/python
<syntaxhighlight lang="python">#!/usr/bin/python
from itertools import product, combinations
from itertools import product, combinations
Line 3,705: Line 3,705:
break
break
printit(deal, sets)
printit(deal, sets)
</syntaxhighlight>
</lang>
<small>Note: You could remove the inner square brackets of the <code>'if all( [...] )'</code> part of the sets computation in the getsets function. It is a remnant of Python 2.7 debugging which gives access to the name <code>feature</code>. The code works on Python 3.X too, but not that access.</small>
<small>Note: You could remove the inner square brackets of the <code>'if all( [...] )'</code> part of the sets computation in the getsets function. It is a remnant of Python 2.7 debugging which gives access to the name <code>feature</code>. The code works on Python 3.X too, but not that access.</small>


Line 3,739: Line 3,739:
===Short Version===
===Short Version===
{{trans|D}}
{{trans|D}}
<lang python>import random, pprint
<syntaxhighlight lang="python">import random, pprint
from itertools import product, combinations
from itertools import product, combinations


Line 3,759: Line 3,759:
pprint.pprint(draw)
pprint.pprint(draw)
print "\nContaining %d sets:" % len(sets)
print "\nContaining %d sets:" % len(sets)
pprint.pprint(sets)</lang>
pprint.pprint(sets)</syntaxhighlight>
{{out}}
{{out}}
<pre>Dealt 9 cards:
<pre>Dealt 9 cards:
Line 3,787: Line 3,787:


=={{header|Racket}}==
=={{header|Racket}}==
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
#lang racket


Line 3,824: Line 3,824:
(deal 9 4)
(deal 9 4)
(deal 12 6)
(deal 12 6)
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
Line 3,830: Line 3,830:


The trick here is to allocate three different bits for each enum, with the result that the cards of a matching set OR together to produce a 4-digit octal number that contains only the digits 1, 2, 4, or 7. This OR is done by funny looking <tt>[+|]</tt>, which is the reduction form of <tt>+|</tt>, which is the numeric bitwise OR. (Because Raku stole the bare <tt>|</tt> operator for composing junctions instead.)
The trick here is to allocate three different bits for each enum, with the result that the cards of a matching set OR together to produce a 4-digit octal number that contains only the digits 1, 2, 4, or 7. This OR is done by funny looking <tt>[+|]</tt>, which is the reduction form of <tt>+|</tt>, which is the numeric bitwise OR. (Because Raku stole the bare <tt>|</tt> operator for composing junctions instead.)
<lang perl6>enum Color (red => 0o1000, green => 0o2000, purple => 0o4000);
<syntaxhighlight lang="raku" line>enum Color (red => 0o1000, green => 0o2000, purple => 0o4000);
enum Count (one => 0o100, two => 0o200, three => 0o400);
enum Count (one => 0o100, two => 0o200, three => 0o400);
enum Shape (oval => 0o10, squiggle => 0o20, diamond => 0o40);
enum Shape (oval => 0o10, squiggle => 0o20, diamond => 0o40);
Line 3,857: Line 3,857:
show-cards @cards;
show-cards @cards;
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Drew 9 cards:
<pre>Drew 9 cards:
Line 3,897: Line 3,897:


The particular set of cards dealt (show below) used Regina 3.9.3 under a Windows/XP environment.
The particular set of cards dealt (show below) used Regina 3.9.3 under a Windows/XP environment.
<lang rexx>/*REXX program finds and displays "sets" (solutions) for the SET puzzle (game). */
<syntaxhighlight lang="rexx">/*REXX program finds and displays "sets" (solutions) for the SET puzzle (game). */
parse arg game seed . /*get optional # cards to deal and seed*/
parse arg game seed . /*get optional # cards to deal and seed*/
if game=='' | game=="," then game= 9 /*Not specified? Then use the default.*/
if game=='' | game=="," then game= 9 /*Not specified? Then use the default.*/
Line 4,014: Line 4,014:
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
sey: if \tell then return /*¬ tell? Then suppress the output. */
sey: if \tell then return /*¬ tell? Then suppress the output. */
if arg(2)==. then say; say arg(1); if arg(3)==. then say; return</lang>
if arg(2)==. then say; say arg(1); if arg(3)==. then say; return</syntaxhighlight>
'''output''' when using the default input:
'''output''' when using the default input:
<pre>
<pre>
Line 4,065: Line 4,065:
=={{header|Ruby}}==
=={{header|Ruby}}==
Brute force.
Brute force.
<lang ruby>COLORS = %i(red green purple) #use [:red, :green, :purple] in Ruby < 2.0
<syntaxhighlight lang="ruby">COLORS = %i(red green purple) #use [:red, :green, :purple] in Ruby < 2.0
SYMBOLS = %i(oval squiggle diamond)
SYMBOLS = %i(oval squiggle diamond)
NUMBERS = %i(one two three)
NUMBERS = %i(one two three)
Line 4,100: Line 4,100:


set_puzzle(9)
set_puzzle(9)
set_puzzle(12)</lang>
set_puzzle(12)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,171: Line 4,171:
</pre>
</pre>
=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>
<syntaxhighlight lang="rust">
use itertools::Itertools;
use itertools::Itertools;
use rand::Rng;
use rand::Rng;
Line 4,272: Line 4,272:
}
}


</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 4,316: Line 4,316:
=={{header|Tailspin}}==
=={{header|Tailspin}}==
Dealing cards at random to the size of the desired hand, then trying again if the desired set count is not achieved.
Dealing cards at random to the size of the desired hand, then trying again if the desired set count is not achieved.
<lang tailspin>
<syntaxhighlight lang="tailspin">
def deck: [ { by 1..3 -> (colour: $),
def deck: [ { by 1..3 -> (colour: $),
by 1..3 -> (symbol: $),
by 1..3 -> (symbol: $),
Line 4,373: Line 4,373:


[9,4] -> setPuzzle -> formatSets -> !OUT::write
[9,4] -> setPuzzle -> formatSets -> !OUT::write
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 4,395: Line 4,395:


Twelve cards with six sets
Twelve cards with six sets
<lang tailspin>
<syntaxhighlight lang="tailspin">
[12,6] -> setPuzzle -> formatSets -> !OUT::write
[12,6] -> setPuzzle -> formatSets -> !OUT::write
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 4,426: Line 4,426:
The principle behind this code is that the space of possible solutions is a substantial proportion of the space of possible hands, so picking a random hand and verifying that it is a solution, repeating until that verification succeeds, is a much quicker way to find a solution than a systematic search.
The principle behind this code is that the space of possible solutions is a substantial proportion of the space of possible hands, so picking a random hand and verifying that it is a solution, repeating until that verification succeeds, is a much quicker way to find a solution than a systematic search.
It also makes the code substantially simpler.
It also makes the code substantially simpler.
<lang tcl># Generate random integer uniformly on range [0..$n-1]
<syntaxhighlight lang="tcl"># Generate random integer uniformly on range [0..$n-1]
proc random n {expr {int(rand() * $n)}}
proc random n {expr {int(rand() * $n)}}


Line 4,507: Line 4,507:
}
}
return [list $hand $sets]
return [list $hand $sets]
}</lang>
}</syntaxhighlight>
Demonstrating:
Demonstrating:
<lang tcl># Render a hand (or any list) of cards (the "."s are just placeholders).
<syntaxhighlight lang="tcl"># Render a hand (or any list) of cards (the "."s are just placeholders).
proc PrettyHand {hand {separator \n}} {
proc PrettyHand {hand {separator \n}} {
set Co {. red green . purple}
set Co {. red green . purple}
Line 4,537: Line 4,537:
PrettyOutput [SetPuzzle 9 4]
PrettyOutput [SetPuzzle 9 4]
puts "=== ADVANCED PUZZLE ======"
puts "=== ADVANCED PUZZLE ======"
PrettyOutput [SetPuzzle 12 6]</lang>
PrettyOutput [SetPuzzle 12 6]</syntaxhighlight>
{{out|Sample output}}
{{out|Sample output}}
<pre>
<pre>
Line 4,615: Line 4,615:
{{libheader|Wren-math}}
{{libheader|Wren-math}}
{{libheader|Wren-sort}}
{{libheader|Wren-sort}}
<lang ecmascript>import "/dynamic" for Enum
<syntaxhighlight lang="ecmascript">import "/dynamic" for Enum
import "/trait" for Comparable
import "/trait" for Comparable
import "/fmt" for Fmt
import "/fmt" for Fmt
Line 4,725: Line 4,725:
playGame.call(Degree.BASIC)
playGame.call(Degree.BASIC)
System.print()
System.print()
playGame.call(Degree.ADVANCED)</lang>
playGame.call(Degree.ADVANCED)</syntaxhighlight>


{{out}}
{{out}}
Line 4,805: Line 4,805:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|D}}
{{trans|D}}
<lang zkl>const nDraw=9, nGoal=(nDraw/2); // Basic
<syntaxhighlight lang="zkl">const nDraw=9, nGoal=(nDraw/2); // Basic
var [const] UH=Utils.Helpers; // baked in stash of goodies
var [const] UH=Utils.Helpers; // baked in stash of goodies
deck:=Walker.cproduct("red green purple".split(), // Cartesian product of 4 lists of lists
deck:=Walker.cproduct("red green purple".split(), // Cartesian product of 4 lists of lists
Line 4,826: Line 4,826:
draw.pump(Void,fcn(card){ println(("%8s "*4).fmt(card.xplode())) });
draw.pump(Void,fcn(card){ println(("%8s "*4).fmt(card.xplode())) });
println("\nContaining:");
println("\nContaining:");
sets.pump(Void,fcn(card){ println((("%8s "*4 + "\n")*3).fmt(card.xplode())) });</lang>
sets.pump(Void,fcn(card){ println((("%8s "*4 + "\n")*3).fmt(card.xplode())) });</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>