Skip to content

Instantly share code, notes, and snippets.

@tonybaloney
Created January 5, 2025 04:45
Show Gist options
  • Save tonybaloney/818c9f4f5ae14606afc9bb8bff2c20ff to your computer and use it in GitHub Desktop.
Save tonybaloney/818c9f4f5ae14606afc9bb8bff2c20ff to your computer and use it in GitHub Desktop.
from itertools import combinations
from collections import namedtuple
Card = namedtuple("Card", ["color", "shape", "shade", "number"])
cards = [
Card("red", "diamond", "hollow", 1),
Card("green", "squiggle", "solid", 3),
Card("red", "oval", "striped", 3),
Card("red", "oval", "hollow", 2),
Card("purple", "oval", "hollow", 1),
Card("purple", "diamond", "solid", 1),
Card("purple", "squiggle", "solid", 3),
Card("red", "diamond", "solid", 3),
Card("green", "oval", "hollow", 3),
Card("red", "diamond", "hollow", 2),
Card("green", "oval", "striped", 3),
Card("red", "oval", "hollow", 1),
]
match = None
for potential in combinations(cards, 3):
if len(set(card.color for card in potential)) in (1, 3) and \
len(set(card.shape for card in potential)) in (1, 3) and \
len(set(card.shade for card in potential)) in (1, 3) and \
len(set(card.number for card in potential)) in (1, 3):
match = potential
break
if not match:
print("No set found")
else:
print("Set found: ")
for card in match:
print(card)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment