RosettaCodeData/Task/Set-puzzle/Phix/set-puzzle.phix
2017-09-25 22:28:19 +02:00

66 lines
2 KiB
Text

function comb(sequence pool, integer needed, sequence res={}, integer done=0, sequence chosen={})
if needed=0 then -- got a full set
sequence {a,b,c} = chosen
if not find_any({3,5,6},flatten(sq_or_bits(sq_or_bits(a,b),c))) then
res = append(res,chosen)
end if
elsif done+needed<=length(pool) then
-- get all combinations with and without the next item:
done += 1
res = comb(pool,needed-1,res,done,append(chosen,pool[done]))
res = comb(pool,needed,res,done,chosen)
end if
return res
end function
constant m124 = {1,2,4}
function card(integer n)
--returns the nth card (n is 1..81, res is length 4 of 1/2/4)
n -= 1
sequence res = repeat(0,4)
for i=1 to 4 do
res[i] = m124[remainder(n,3)+1]
n = floor(n/3)
end for
return res
end function
constant colours = {"red", "green", 0, "purple"},
symbols = {"oval", "squiggle", 0, "diamond"},
numbers = {"one", "two", 0, "three"},
shades = {"solid", "open", 0, "striped"}
procedure print_cards(sequence hand, sequence cards)
for i=1 to length(cards) do
integer {c,m,n,g} = cards[i],
id = find(cards[i],hand)
printf(1,"%3d: %-7s %-9s %-6s %s\n",{id,colours[c],symbols[m],numbers[n],shades[g]})
end for
printf(1,"\n")
end procedure
procedure play(integer cards=9, integer sets=4)
integer deals = 1
while 1 do
sequence deck = shuffle(tagset(81))
sequence hand = deck[1..cards]
for i=1 to length(hand) do
hand[i] = card(hand[i])
end for
sequence res = comb(hand,3)
if length(res)=sets then
printf(1,"dealt %d cards (%d deals)\n",{cards,deals})
print_cards(hand,hand)
printf(1,"with %d sets\n",{sets})
for i=1 to sets do
print_cards(hand,res[i])
end for
exit
end if
deals += 1
end while
end procedure
play()
--play(12,6)
--play(9,6)