This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,28 @@
dealraw(cards)=vector(cards,i,vector(4,j,1<<random(3)));
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]]);
check(D,sets)={
my(S=List());
for(i=1,#D-2,for(j=i+1,#D-1,for(k=j+1,#D,
for(x=1,4,
if(howmany(D[i][x],D[j][x],D[k][x])==2,next(2))
);
listput(S,[i,j,k]);
if(#S>sets,return(0))
)));
if(#S==sets,Vec(S),0)
};
deal(cards,sets)={
my(v,s);
until(s,
s=check(v=dealraw(cards),sets)
);
v=apply(name,v);
for(i=1,cards,print(v[i]));
for(i=1,sets,
print("Set #"i);
for(j=1,3,print(" "v[s[i][j]]))
)
};
deal(9,4)
deal(12,6)

View file

@ -0,0 +1,38 @@
COLORS = %i(red green purple) #use [:red, :green, :purple] in Ruby < 2.0
SYMBOLS = %i(oval squiggle diamond)
NUMBERS = %i(one two three)
SHADINGS = %i(solid open striped)
FEATURES = [COLORS, SYMBOLS, NUMBERS, SHADINGS]
@hand_size = 9
@num_sets_goal = 4
#create an enumerator which deals all combinations of @hand_size cards
@dealer = FEATURES[0].product(*FEATURES[1..-1]).shuffle.combination(@hand_size)
def get_all_sets(hand)
hand.combination(3).select do |candidate|
grouped_features = candidate.flatten.group_by{|f| f}
grouped_features.values.none?{|v| v.size == 2}
end
end
def get_puzzle_and_answer
sets = []
until sets.size == @num_sets_goal do
hand = @dealer.next
sets = get_all_sets(hand)
end
[hand, sets]
end
def print_cards(cards)
cards.each{|card| puts card.join(", ")}
puts
end
puzzle, sets = get_puzzle_and_answer
puts "Dealt #{puzzle.size} cards:"
print_cards(puzzle)
puts "Containing #{sets.size} sets:"
sets.each{|set| print_cards(set)}