Skip to content

Instantly share code, notes, and snippets.

@jakab922
Last active August 11, 2016 21:19
Show Gist options
  • Save jakab922/f8e4aefc16f9af6627ac9fa83a6a8c70 to your computer and use it in GitHub Desktop.
Save jakab922/f8e4aefc16f9af6627ac9fa83a6a8c70 to your computer and use it in GitHub Desktop.
""" Solver logic for https://boardgamegeek.com/boardgame/1198/set """
from random import shuffle
from itertools import product, combinations
revd = [
["one", "two", "three"],
["red", "green", "blue"],
["empty", "half", "full"],
["ellipse", "rectangle", "wavy"]]
def show_card(card):
return "(%s)" % ", ".join(map(lambda x: revd[x[0]][x[1]], enumerate(card)))
def has_set(cards):
for c1, c2, c3 in combinations(cards, 3):
if all([(c1[i] + c2[i] + c3[i]) % 3 == 0 for i in xrange(4)]):
return True, [c1, c2, c3]
return False, []
all_cards = list(product(range(3), repeat=4))
shuffle(all_cards)
cards = all_cards[:9]
print "cards: %s" % map(show_card, cards)
has, set_cards = has_set(cards)
if has:
print "There is a set in the cards with: %s" % map(show_card, set_cards)
else:
print "There is no set in the cards"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment