Created
July 13, 2012 21:54
-
-
Save acoster/3107794 to your computer and use it in GitHub Desktop.
Crappy python solver for a Set game (e.g., http://www.nytimes.com/ref/crosswords/setpuzzle.html/ )
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
class Colour: | |
red = 'r' | |
green = 'g' | |
purple = 'p' | |
class Style: | |
solid = 'sl' | |
striped = 'st' | |
open = 'o' | |
class Shape: | |
squiggly = 's' | |
diamond = 'd' | |
ellipse = 'e' | |
class Card: | |
__colours = ('r', 'g', 'p') | |
__styles = ('sl', 'st', 'o') | |
__shapes = ('s', 'd', 'e') | |
__numbers = (1, 2, 3) | |
def __init__(self, colour, style, shape, number): | |
assert(colour in Card.__colours) | |
assert(style in Card.__styles) | |
assert(number in Card.__numbers) | |
assert(shape in Card.__shapes) | |
self.style = style | |
self.shape = shape | |
self.number = number | |
self.colour = colour | |
def get_final_card(self, other): | |
style = "" | |
shape = "" | |
number = "" | |
colour = "" | |
if other.colour == self.colour: | |
colour = self.colour | |
else: | |
colour = [i for i in Card.__colours if i not in (self.colour, other.colour)][0] | |
if other.style == self.style: | |
style = self.style | |
else: | |
style = [i for i in Card.__styles if i not in (self.style, other.style)][0] | |
if other.number == self.number: | |
number = self.number | |
else: | |
number = [i for i in Card.__numbers if i not in (self.number, other.number)][0] | |
if other.shape == self.shape: | |
shape = self.shape | |
else: | |
shape = [i for i in Card.__shapes if i not in (self.shape, other.shape)][0] | |
return Card(colour, style, shape, number) | |
def __eq__(self, other): | |
return self.colour == other.colour and self.style == other.style and \ | |
self.number == other.number and self.shape == other.shape | |
def __lt__(self, other): | |
if self.number != other.number: | |
return self.number < other.number | |
if self.colour != other.colour: | |
return self.colour < other.colour | |
if self.style != other.style: | |
return self.style < other.style | |
if self.shape != other.shape: | |
return self.style < other.shape | |
return False | |
def __repr__(self): | |
return "Card('%s', '%s', '%s', %d)" % (self.colour, self.style, self.shape, self.number) | |
def solve(cards): | |
solutions = [] | |
for idx, c in enumerate(cards[:-1]): | |
for c2 in cards[idx + 1:]: | |
tmp = c.get_final_card(c2) | |
if tmp in cards: | |
solution = [c, c2, tmp] | |
solution.sort() | |
if solution not in solutions: | |
solutions.append(solution) | |
return solutions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment