Set puzzle: Difference between revisions
m
→{{header|Wren}}: Minor tidy
(→{{header|Picat}}: Added subsection) |
m (→{{header|Wren}}: Minor tidy) |
||
(10 intermediate revisions by 5 users not shown) | |||
Line 75:
:purple, three, oval, open
<br><br>
=={{header|Acornsoft Lisp}}==
<syntaxhighlight lang="lisp">
(setq numbers '(one two three))
(setq shadings '(solid open striped))
(setq colours '(red green purple))
(setq symbols '(oval squiggle diamond))
(defun play ((n-cards . 9))
(find-enough-sets n-cards (quotient n-cards 2)))
(defun find-enough-sets (n-cards enough (deal) (sets))
(loop
(setq deal (random-sample n-cards (deck)))
(setq sets (find-sets deal))
(while (lessp (length sets) enough)
(show-cards deal)
(printc)
(show-sets sets))))
(defun show-cards (cards)
(printc (length cards) '! cards)
(map printc cards))
(defun show-sets (sets)
(printc (length sets) '! sets)
(map '(lambda (set)
(printc)
(map printc set))
sets))
(defun find-sets (deal)
(keep-if is-set (combinations 3 deal)))
(defun is-set (cards)
(every feature-makes-set (transpose cards)))
(defun feature-makes-set (feature-values)
(or (all-same feature-values)
(all-different feature-values)))
(defun combinations (n items)
(cond
((zerop n) '(()))
((null items) '())
(t (append
(mapc '(lambda (c) (cons (car items) c))
(combinations (sub1 n) (cdr items)))
(combinations n (cdr items))))))
'( Making a deck )
(defun deck ()
' ( The deck has to be made only once )
(cond ((get 'deck 'cards))
(t (put 'deck 'cards (make-deck)))))
(defun make-deck ()
(add-feature numbers
(add-feature shadings
(add-feature colours
(add-feature symbols
(list '()))))))
(defun add-feature (values deck)
(flatmap '(lambda (value)
(mapc '(lambda (card) (cons value card))
deck))
values))
'( Utilities )
(defun all-same (values)
(every '(lambda (v) (eq v (car values)))
values))
(defun all-different (values)
(every '(lambda (v) (onep (count v values)))
values))
(defun count (v values (n . 0))
(loop (until (null values) n)
(cond ((eq (car values) v) (setq n (add1 n))))
(setq values (cdr values))))
(defun every (test suspects)
(or (null suspects)
(and (test (car suspects))
(every test (cdr suspects)))))
(defun transpose (rows)
(apply mapc (cons list rows)))
(defun reverse (list (result . ()))
(map '(lambda (e) (setq result (cons e result)))
list)
result)
(defun append (a b)
(reverse (reverse a) b))
(defun flatmap (_f_ _list_)
(cond ((null _list_) '())
(t (append (_f_ (car _list_))
(flatmap _f_ (cdr _list_))))))
(defun keep-if (_p_ _items_ (_to_keep_))
(map '(lambda (_i_)
(cond ((_p_ _i_)
(setq _to_keep_ (cons _i_ _to_keep_)))))
_items_)
(reverse _to_keep_))
</syntaxhighlight>
{{Out}}
Calling <code>(play)</code> will output:
<pre>
9 cards
(three open red oval)
(three solid green diamond)
(two solid red squiggle)
(one open red oval)
(two striped green oval)
(one striped red diamond)
(three solid purple oval)
(one solid purple oval)
(three solid purple diamond)
4 sets
(three open red oval)
(two solid red squiggle)
(one striped red diamond)
(three open red oval)
(two striped green oval)
(one solid purple oval)
(three solid green diamond)
(two solid red squiggle)
(one solid purple oval)
(one open red oval)
(two striped green oval)
(three solid purple oval)
</pre>
=={{header|Ada}}==
Line 80 ⟶ 229:
We start with the specification of a package "Set_Puzzle.
<
type Three is range 1..3;
Line 97 ⟶ 246:
-- calls Do_Something once for each set it finds.
end Set_Puzzle;</
Now we implement the package "Set_Puzzle".
<
package body Set_Puzzle is
Line 178 ⟶ 327:
begin
Rand.Reset(R);
end Set_Puzzle;</
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.
<
procedure Puzzle is
Line 231 ⟶ 380:
Print_Sets(Cards); -- print the sets
end Puzzle;</
{{out}}
Line 263 ⟶ 412:
=={{header|AutoHotkey}}==
<
Loop, 81
deck .= ToBase(A_Index-1, 3)+1111 ","
Line 366 ⟶ 515:
c%j% += 1
}
}</
{{out|Sample output}}
<pre>Dealt 9 cards:
Line 446 ⟶ 595:
=={{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.
<
#include <stdlib.h>
Line 540 ⟶ 689:
return 0;
}</
=={{header|C sharp}}==
{{works with|C sharp|8}}
<
using System.Collections.Generic;
using static System.Linq.Enumerable;
Line 628 ⟶ 777:
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);
}</
{{out}}
<pre style="height:30ex;overflow:scroll">
Line 673 ⟶ 822:
=={{header|C++}}==
{{trans|Java}}
<
#include <time.h>
#include <algorithm>
Line 805 ⟶ 954:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 880 ⟶ 1,029:
=={{header|Ceylon}}==
Add import ceylon.random "1.3.3" to your module.ceylon file
<
Random,
DefaultRandom
Line 1,019 ⟶ 1,168:
}
}</
=={{header|Common Lisp}}==
{{trans|Acornsoft Lisp}}
<syntaxhighlight lang="lisp">
(defparameter numbers '(one two three))
(defparameter shadings '(solid open striped))
(defparameter colours '(red green purple))
(defparameter symbols '(oval squiggle diamond))
(defun play (&optional (n-cards 9))
(find-enough-sets n-cards (floor n-cards 2)))
(defun find-enough-sets (n-cards enough)
(loop
(let* ((deal (random-sample n-cards (deck)))
(sets (find-sets deal)))
(when (>= (length sets) enough)
(show-cards deal)
(show-sets sets)
(return t)))))
(defun show-cards (cards)
(format t "~D cards~%~{~(~{~10S~}~)~%~}~%"
(length cards) cards))
(defun show-sets (sets)
(format t "~D sets~2%~:{~(~@{~{~8S~}~%~}~)~%~}"
(length sets) sets))
(defun find-sets (deal)
(remove-if-not #'is-set (combinations 3 deal)))
(defun is-set (cards)
(every #'feature-makes-set (transpose cards)))
(defun feature-makes-set (feature-values)
(or (all-same feature-values)
(all-different feature-values)))
(defun combinations (n items)
(cond
((zerop n) '(()))
((null items) '())
(t (append
(mapcar (lambda (c) (cons (car items) c))
(combinations (1- n) (cdr items)))
(combinations n (cdr items))))))
;;; Making a deck
(defun deck ()
;; The deck has to be made only once
(or (get 'deck 'cards)
(setf (get 'deck 'cards) (make-deck))))
(defun make-deck ()
(add-feature numbers
(add-feature shadings
(add-feature colours
(add-feature symbols
(list '()))))))
(defun add-feature (values deck)
(mapcan (lambda (value)
(mapcar (lambda (card) (cons value card))
deck))
values))
;;; Utilities
(defun random-sample (n items)
(let ((len (length items))
(taken '()))
(dotimes (_ n)
(loop
(let ((i (random len)))
(unless (find i taken)
(setq taken (cons i taken))
(return)))))
(mapcar (lambda (i) (nth i items)) taken)))
(defun all-same (values)
(every #'eql values (rest values)))
(defun all-different (values)
(every (lambda (v) (= (count v values) 1))
values))
(defun transpose (rows)
(apply #'mapcar #'list rows))
</syntaxhighlight>
{{Out}}
Calling <code>(play 12)</code> will output:
<pre>
12 cards
two open red oval
three solid red squiggle
one striped red oval
three solid green squiggle
three solid green diamond
three solid red oval
one open purple squiggle
two solid red diamond
three open red squiggle
two striped green diamond
two striped red squiggle
three solid purple oval
6 sets
two open red oval
one striped red oval
three solid red oval
two open red oval
two solid red diamond
two striped red squiggle
three solid red squiggle
three solid green diamond
three solid purple oval
one striped red oval
two solid red diamond
three open red squiggle
three solid green squiggle
one open purple squiggle
two striped red squiggle
three solid red oval
one open purple squiggle
two striped green diamond
</pre>
=={{header|D}}==
===Basic Version===
<
std.exception, std.range, std.algorithm;
Line 1,118 ⟶ 1,405:
writefln("\nFOUND %d SET%s:", len, len == 1 ? "" : "S");
writefln("%(%(%s\n%)\n\n%)", sets);
}</
{{out|Sample output}}
<pre>DEALT 9 CARDS:
Line 1,150 ⟶ 1,437:
===Short Version===
This requires the third solution module of the Combinations Task.
<
import std.stdio, std.algorithm, std.range, std.random, combinations3;
Line 1,168 ⟶ 1,455:
writefln("Dealt %d cards:\n%(%-(%8s %)\n%)\n", draw.length, draw);
writefln("Containing:\n%(%(%-(%8s %)\n%)\n\n%)", sets);
}</
{{out}}
<pre>Dealt 9 cards:
Line 1,197 ⟶ 1,484:
purple one squiggle striped
green three diamond striped</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
attr$[][] &= [ "one " "two " "three" ]
attr$[][] &= [ "solid " "striped" "open " ]
attr$[][] &= [ "red " "green " "purple" ]
attr$[][] &= [ "diamond" "oval" "squiggle" ]
#
subr init
for card = 0 to 80
pack[] &= card
.
.
proc card2attr card . attr[] .
attr[] = [ ]
for i to 4
attr[] &= card mod 3 + 1
card = card div 3
.
.
proc printcards cards[] . .
for card in cards[]
card2attr card attr[]
for i to 4
write attr$[i][attr[i]] & " "
.
print ""
.
print ""
.
proc getsets . cards[] set[] .
set[] = [ ]
for i to len cards[]
card2attr cards[i] a[]
for j = i + 1 to len cards[]
card2attr cards[j] b[]
for k = j + 1 to len cards[]
card2attr cards[k] c[]
ok = 1
for at to 4
s = a[at] + b[at] + c[at]
if s <> 3 and s <> 6 and s <> 9
ok = 0
.
.
if ok = 1
set[] &= cards[i]
set[] &= cards[j]
set[] &= cards[k]
.
.
.
.
.
proc run ncards nsets . .
#
repeat
init
cards[] = [ ]
for i to ncards
ind = random len pack[]
cards[] &= pack[ind]
pack[ind] = pack[len pack[]]
len pack[] -1
.
getsets cards[] set[]
until len set[] = 3 * nsets
.
print "Cards:"
printcards cards[]
print "Sets:"
for i = 1 step 3 to 3 * nsets - 2
printcards [ set[i] set[i + 1] set[i + 2] ]
.
.
run 9 4
print " --------------------------"
run 12 6
</syntaxhighlight>
=={{header|EchoLisp}}==
<
(require 'list)
Line 1,250 ⟶ 1,616:
))
</syntaxhighlight>
{{out}}
<pre>
Line 1,311 ⟶ 1,677:
=={{header|Elixir}}==
{{trans|Ruby}}
<
def set_puzzle(deal, goal) do
{puzzle, sets} = get_puzzle_and_answer(deal, goal, produce_deck)
Line 1,364 ⟶ 1,730:
RC.set_puzzle(9, 4)
RC.set_puzzle(12, 6)</
{{out}}
Line 1,438 ⟶ 1,804:
=={{header|Erlang}}==
Until a better solution is found this is one.
<syntaxhighlight lang="erlang">
-module( set ).
Line 1,522 ⟶ 1,888:
make_sets_acc( true, Set, Sets ) -> [Set | Sets];
make_sets_acc( false, _Set, Sets ) -> Sets.
</syntaxhighlight>
{{out}}
<pre>
Line 1,582 ⟶ 1,948:
=={{header|F Sharp|F#}}==
<
type Number = One | Two | Three
Line 1,663 ⟶ 2,029:
solve 12 6
playGame()</
Output:
<pre>
Line 1,734 ⟶ 2,100:
=={{header|Factor}}==
<
fry grouping io kernel literals math.combinatorics math.matrices
prettyprint qw random sequences sets ;
Line 1,775 ⟶ 2,141:
12 6 set-puzzle ;
MAIN: main</
{{out}}
<pre style="height:65ex">
Line 1,847 ⟶ 2,213:
=={{header|Go}}==
<
import (
Line 1,920 ⟶ 2,286:
fmt.Printf(" %s\n %s\n %s\n",s[0],s[1],s[2])
}
}</
{{out}}
<pre>
Line 1,992 ⟶ 2,358:
=={{header|Haskell}}==
<
(State, evalState, replicateM, runState, state)
import System.Random (StdGen, newStdGen, randomR)
Line 2,094 ⟶ 2,460:
main = do
showSolutions 9 4
showSolutions 12 6</
{{out}}
<pre style="font-size:80%">Showing hand of 9 cards with 4 solutions.
Line 2,167 ⟶ 2,533:
=={{header|J}}==
'''Solution:'''
<
Number=: ;:'one two three'
Line 2,190 ⟶ 2,556:
echo LF,'Found ',(":target),' Sets:'
echo sayCards sort"2 getSets Hand
)</
'''Example:'''
<
Dealt 9 Cards:
one, red, solid, oval
Line 2,220 ⟶ 2,586:
three, red, open, oval
three, green, solid, oval
three, purple, striped, oval </
=={{header|Java}}==
<
public class SetPuzzle {
Line 2,353 ⟶ 2,719:
return tot == 0;
}
}</
<pre>GIVEN 12 CARDS:
Line 2,397 ⟶ 2,763:
=={{header|Julia}}==
Plays one basic game and one advanced game.
<
function SetGameTM(basic = true)
Line 2,440 ⟶ 2,806:
SetGameTM()
SetGameTM(false)
</
Dealt 9 cards:
("green", "one", "oval", "open")
Line 2,510 ⟶ 2,876:
=={{header|Kotlin}}==
<
import java.util.Collections.shuffle
Line 2,598 ⟶ 2,964:
println()
playGame(Degree.ADVANCED)
}</
Sample output:
Line 2,678 ⟶ 3,044:
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).
<
symbols = {"0", "\[TildeTilde]", "\[Diamond]"};
numbers = {1, 2, 3};
Line 2,693 ⟶ 3,059:
count = Count[Subsets[cards, {3}], _?validSetQ]];
cards];
Row[{Style[#2, #1], #3, #4}] & @@@ deal[{9, 4}]</
=={{header|Nim}}==
<
type
Line 2,777 ⟶ 3,143:
playGame(Basic)
echo()
playGame(Advanced)</
{{out}}
Line 2,853 ⟶ 3,219:
=={{header|PARI/GP}}==
<
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]]);
Line 2,880 ⟶ 3,246:
};
deal(9,4)
deal(12,6)</
{{output}}
<pre>green, diamond, one, open
Line 2,950 ⟶ 3,316:
It's actually slightly simplified, since generating Enum classes
and objects would be overkill for this particular task.
<
use strict;
use warnings;
Line 3,029 ⟶ 3,395:
__END__
</syntaxhighlight>
{{out}}
<pre>Drew 12 cards:
Line 3,059 ⟶ 3,425:
=={{header|Phix}}==
Converts cards 1..81 (that idea from C) to 1/2/4 [/7] (that idea from Perl) but inverts the validation
<!--<
<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>
Line 3,127 ⟶ 3,493:
<span style="color: #000080;font-style:italic;">--play(12,6)
--play(9,6)</span>
<!--</
{{out}}
<pre>
Line 3,161 ⟶ 3,527:
=={{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.
<
import cp.
Line 3,334 ⟶ 3,700:
],
SetLen = 3,
Wanted = 4.</
Line 3,403 ⟶ 3,769:
===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.
<
NumCards = 18,
NumWanted = 9,
Line 3,485 ⟶ 3,851:
%
generate_instance2(NumCards,_NumSets,_SetLen, Cards) =>
Cards = random_deal(NumCards).</
{{out}}
Line 3,537 ⟶ 3,903:
=={{header|Prolog}}==
<
card_sets(N, Cards, Sets),
!,
Line 3,581 ⟶ 3,947:
match([A|T1],[B|T2],[C|T3]) :-
dif(A,B), dif(B,C), dif(A,C),
match(T1,T2,T3).</
{{out}}
<pre>
Line 3,659 ⟶ 4,025:
=={{header|Python}}==
<
from itertools import product, combinations
Line 3,705 ⟶ 4,071:
break
printit(deal, sets)
</syntaxhighlight>
<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 ⟶ 4,105:
===Short Version===
{{trans|D}}
<
from itertools import product, combinations
Line 3,759 ⟶ 4,125:
pprint.pprint(draw)
print "\nContaining %d sets:" % len(sets)
pprint.pprint(sets)</
{{out}}
<pre>Dealt 9 cards:
Line 3,785 ⟶ 4,151:
('green', 'three', 'oval', 'open'),
('purple', 'three', 'squiggle', 'solid'))]</pre>
=={{header|Quackery}}==
<code>cards</code>, <code>sets</code>, and <code>echocard</code> are defined at [[Set, the card game#Quackery]].
<syntaxhighlight lang="Quackery"> [ temp put
[ dup cards dup
sets dup size
temp share != while
2drop again ]
swap
say "Cards:" cr
witheach echocard
cr
say "Sets:" cr
witheach
[ witheach echocard cr ]
drop
temp release ] is task ( n n --> )
basic task
cr
advanced task</syntaxhighlight>
{{out}}
<pre>BASIC
Cards:
three striped purple ovals
two solid green squiggles
two open green squiggles
one open purple squiggle
three striped red squiggles
two striped red ovals
one solid purple squiggle
two solid red diamonds
two open purple diamonds
Sets:
two open green squiggles
three striped red squiggles
one solid purple squiggle
two solid green squiggles
two striped red ovals
two open purple diamonds
two solid green squiggles
one open purple squiggle
three striped red squiggles
three striped purple ovals
one solid purple squiggle
two open purple diamonds
ADVANCED
Cards:
one open green oval
two solid purple squiggles
one striped red diamond
three solid purple diamonds
two solid green squiggles
two open purple squiggles
two open green ovals
two striped red squiggles
two open green diamonds
two striped red ovals
three open purple ovals
one open red oval
Sets:
two open green ovals
three open purple ovals
one open red oval
two solid green squiggles
two open purple squiggles
two striped red squiggles
one striped red diamond
two solid green squiggles
three open purple ovals
one striped red diamond
three solid purple diamonds
two open green diamonds
two solid purple squiggles
two open green diamonds
two striped red ovals
one open green oval
three solid purple diamonds
two striped red squiggles
</pre>
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
Line 3,824 ⟶ 4,288:
(deal 9 4)
(deal 12 6)
</syntaxhighlight>
=={{header|Raku}}==
Line 3,830 ⟶ 4,294:
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.)
<syntaxhighlight lang="raku"
enum Count (one => 0o100, two => 0o200, three => 0o400);
enum Shape (oval => 0o10, squiggle => 0o20, diamond => 0o40);
Line 3,857 ⟶ 4,321:
show-cards @cards;
}
}</
{{out}}
<pre>Drew 9 cards:
Line 3,897 ⟶ 4,361:
The particular set of cards dealt (show below) used Regina 3.9.3 under a Windows/XP environment.
<
parse arg game seed . /*get optional # cards to deal and seed*/
if game=='' | game=="," then game= 9 /*Not specified? Then use the default.*/
Line 4,014 ⟶ 4,478:
/*──────────────────────────────────────────────────────────────────────────────────────*/
sey: if \tell then return /*¬ tell? Then suppress the output. */
if arg(2)==. then say; say arg(1); if arg(3)==. then say; return</
'''output''' when using the default input:
<pre>
Line 4,065 ⟶ 4,529:
=={{header|Ruby}}==
Brute force.
<
SYMBOLS = %i(oval squiggle diamond)
NUMBERS = %i(one two three)
Line 4,100 ⟶ 4,564:
set_puzzle(9)
set_puzzle(12)</
{{out}}
<pre>
Line 4,171 ⟶ 4,635:
</pre>
=={{header|Rust}}==
<
use itertools::Itertools;
use rand::Rng;
Line 4,272 ⟶ 4,736:
}
</syntaxhighlight>
{{out}}
<pre>
Line 4,316 ⟶ 4,780:
=={{header|Tailspin}}==
Dealing cards at random to the size of the desired hand, then trying again if the desired set count is not achieved.
<
def deck: [ { by 1..3 -> (colour: $),
by 1..3 -> (symbol: $),
Line 4,348 ⟶ 4,812:
def nCards: $(1);
def nSets: $(2);
{sets: []} -> #
when <{sets: <[]($nSets..)>}> do $ !
otherwise
Line 4,356 ⟶ 4,820:
templates formatCard
def colours: colour´1:['red', 'green', 'purple'];
def symbols: symbol´1:['oval', 'squiggle', 'diamond'];
def numbers: number´1:['one', 'two', 'three'];
def shadings: shading´1:['solid', 'open', 'striped'];
$ -> '$colours($.colour);-$symbols($.symbol);-$numbers($.number);-$shadings($.shading);' !
end formatCard
Line 4,372 ⟶ 4,836:
end formatSets
[9,4] -> setPuzzle -> formatSets -> !OUT::write</syntaxhighlight>
{{out}}
<pre>
Line 4,395 ⟶ 4,858:
Twelve cards with six sets
<
[12,6] -> setPuzzle -> formatSets -> !OUT::write
</syntaxhighlight>
{{out}}
<pre>
Line 4,426 ⟶ 4,889:
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.
<
proc random n {expr {int(rand() * $n)}}
Line 4,507 ⟶ 4,970:
}
return [list $hand $sets]
}</
Demonstrating:
<
proc PrettyHand {hand {separator \n}} {
set Co {. red green . purple}
Line 4,537 ⟶ 5,000:
PrettyOutput [SetPuzzle 9 4]
puts "=== ADVANCED PUZZLE ======"
PrettyOutput [SetPuzzle 12 6]</
{{out|Sample output}}
<pre>
Line 4,615 ⟶ 5,078:
{{libheader|Wren-math}}
{{libheader|Wren-sort}}
<
import "./trait" for Comparable
import "./fmt" for Fmt
import "./str" for Str
import "./math" for Nums
import "./sort" for Sort
import "random" for Random
Line 4,725 ⟶ 5,188:
playGame.call(Degree.BASIC)
System.print()
playGame.call(Degree.ADVANCED)</
{{out}}
Line 4,805 ⟶ 5,268:
=={{header|zkl}}==
{{trans|D}}
<
var [const] UH=Utils.Helpers; // baked in stash of goodies
deck:=Walker.cproduct("red green purple".split(), // Cartesian product of 4 lists of lists
Line 4,826 ⟶ 5,289:
draw.pump(Void,fcn(card){ println(("%8s "*4).fmt(card.xplode())) });
println("\nContaining:");
sets.pump(Void,fcn(card){ println((("%8s "*4 + "\n")*3).fmt(card.xplode())) });</
{{out}}
<pre>
|