Created
September 3, 2014 16:27
-
-
Save anonymous/af439deeb3aa8745afe7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
import argparse | |
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | |
class Card: | |
def __init__(self, symbols): | |
self.symbols = frozenset(symbols) | |
self.name = "".join(symbols) | |
def __str__(self): | |
return self.name | |
def __hash__(self): | |
return hash(self.symbols) | |
def matches(self, other): | |
"""Returns true if this card has exactly one symbole in common with other.""" | |
return len(self.symbols.intersection(other.symbols)) == 1 | |
def neighbors(self, candidates): | |
"""Finds cards in candidates that have exactly one matched symbol to this card.""" | |
return [candidate for candidate in candidates if candidate.matches(self)] | |
def matches_all(self, others): | |
for other in others: | |
if not other.matches(self): | |
return False | |
return True | |
def format_cards(cards, sep = "\n"): | |
return sep.join([str(card) for card in cards]) | |
class Deck: | |
def __init__(self, n, k): | |
self.n = n | |
self.k = k | |
self.symbols = letters[:n] | |
def solve(self): | |
combinations = itertools.combinations(self.symbols, self.k) | |
possible_cards = [Card(subset) for subset in combinations] | |
solution = set() | |
for candidate in possible_cards: | |
if candidate.matches_all(solution): | |
solution.add(candidate) | |
return solution | |
def __str__(self): | |
return "Deck({}, {})".format(self.n, self.k) | |
def print_solution(deck): | |
print("Solving {}: ".format(deck)), | |
solution = deck.solve() | |
print(len(solution)) | |
def parse_args(): | |
parser = argparse.ArgumentParser(description='Outputs a spotit graph.') | |
parser.add_argument('-n', '--alphabet-size', dest='N', type=int, required=True) | |
parser.add_argument('-k', '--symbols-per-card', dest='K', type=int, required=True) | |
return parser.parse_args() | |
if __name__ == "__main__": | |
n = 22 | |
while n < 40: | |
deck = Deck(n, 8) | |
print("Solving {}...".format(deck)), | |
solution = deck.solve() | |
l = len(solution) | |
print(l) | |
if l == 55: | |
print(format_cards(solution)) | |
break | |
n += 1 | |
# deck = Deck(args.N, args.K) | |
# print_solution(deck) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment