Set puzzle: Difference between revisions
Content deleted Content added
m omissions and categories |
→{{header|D}}: break off findSets search when target is overshot |
||
Line 139: | Line 139: | ||
} |
} |
||
immutable(Card)[3][] findSets(in Card[] cards |
immutable(Card)[3][] findSets(in Card[] cards, in uint target = 0) |
||
pure nothrow { |
|||
auto len = cards.length; |
auto len = cards.length; |
||
if (len >= 3) { |
if (len >= 3) { |
||
Line 146: | Line 147: | ||
for (int j = i + 1; j < len - 1; j++) |
for (int j = i + 1; j < len - 1; j++) |
||
for (int k = j + 1; k < len; k++) |
for (int k = j + 1; k < len; k++) |
||
if (validSet(cards[i], cards[j], cards[k])) |
if (validSet(cards[i], cards[j], cards[k])) { |
||
sets ~= [cards[i], cards[j], cards[k]]; |
sets ~= [cards[i], cards[j], cards[k]]; |
||
if (target != 0 && sets.length > target) |
|||
return null; |
|||
} |
|||
} |
} |
||
return assumeUnique(sets); |
return assumeUnique(sets); |
||
Line 163: | Line 167: | ||
do { |
do { |
||
cards = super.deal(numCards); |
cards = super.deal(numCards); |
||
} while (findSets(cards).length != numSets); |
} while (findSets(cards, numSets).length != numSets); |
||
return assumeUnique(cards); |
return assumeUnique(cards); |