The riddle as described here is as follows:
Einsteins' Riddle
- In a street there are five houses, painted five different colours.
- In each house lives a person of different nationality
- These five homeowners each drink a different kind of beverage, smoke a different brand of cigar and keep a different pet.
HINTS
- The Brit lives in a red house.
- The Swede keeps dogs as pets.
- The Dane drinks tea.
- The Green house is next to, and on the left of the White house.
- The owner of the Green house drinks coffee.
- The person who smokes Pall Mall rears birds.
- The owner of the Yellow house smokes Dunhill.
- The man living in the centre house drinks milk.
- The Norwegian lives in the first house.
- The man who smokes Blends lives next to the one who keeps cats.
- The man who keeps horses lives next to the man who smokes Dunhill.
- The man who smokes Blue Master drinks beer.
- The German smokes Prince.
- The Norwegian lives next to the blue house.
- The man who smokes Blends has a neighbour who drinks water.
from constraint import *
problem = Problem()
def left_of(a,b):
return a < b
def right_of(a,b):
return not left_of(a,b)
def next_to(a,b):
return abs(a - b) == 1
colors = ["yellow","red","green","blue","white"]
nats = ["norwegian","dane","brit","german","swede"]
bevs = ["water","tea","milk","coffee","beer"]
smokes = ["dunhill","blends","pall_mall", "prince", "bluemaster"]
pets = ["cat","horse","bird","fish","dog"]
house_numbers = [1,2,3,4,5]
problem.addVariables(colors, house_numbers)
problem.addVariables(nats, house_numbers)
problem.addVariables(bevs, house_numbers)
problem.addVariables(smokes, house_numbers)
problem.addVariables(pets, house_numbers)
problem.addConstraint(AllDifferentConstraint(),colors)
problem.addConstraint(AllDifferentConstraint(),nats)
problem.addConstraint(AllDifferentConstraint(),bevs)
problem.addConstraint(AllDifferentConstraint(),smokes)
problem.addConstraint(AllDifferentConstraint(),pets)
#specific constraints from the riddle
problem.addConstraint(lambda brit, red: brit == red, ("brit", "red"))
problem.addConstraint(lambda swede, dog: swede == dog, ("swede", "dog"))
problem.addConstraint(lambda dane, tea: dane == tea, ("dane", "tea"))
problem.addConstraint(lambda green, white: next_to(green, white) and \
left_of(green, white),("green", "white"))
problem.addConstraint(lambda green, coffee: green == coffee,
("green", "coffee"))
problem.addConstraint(lambda pall_mall, bird: pall_mall == bird,
("pall_mall", "bird"))
problem.addConstraint(lambda yellow, dunhill: yellow == dunhill,
("yellow", "dunhill"))
problem.addConstraint(lambda milk: milk == 3, ["milk"])
problem.addConstraint(lambda norwegian: norwegian == 1, ["norwegian"])
problem.addConstraint(lambda blends, cat: next_to(blends, cat),
("blends", "cat"))
problem.addConstraint(lambda horse, dunhill: next_to(horse, dunhill),
("horse", "dunhill"))
problem.addConstraint(lambda bluemaster, beer: bluemaster == beer,
("bluemaster", "beer"))
problem.addConstraint(lambda german, prince: german == prince,
("german","prince"))
problem.addConstraint(lambda norwegian, blue: next_to(norwegian, blue),
("norwegian", "blue"))
problem.addConstraint(lambda blends, water: next_to(blends, water),
("blends", "water"))
# now get a solution
solution = problem.getSolution()
# get in the form we want
answer = {}
for i in range(1,6):
answer[i] = {}
for k,v in solution.items():
if k in colors:
answer[v]['color'] = k
elif k in nats:
answer[v]['nationality'] = k
elif k in bevs:
answer[v]['beverage'] = k
elif k in smokes:
answer[v]['smokes'] = k
elif k in pets:
answer[v]['pet'] = k
print "**** Solution ****"
for i in range(1,6):
print i, answer[i]['color'],answer[i]['nationality'],\
answer[i]['beverage'],answer[i]['smokes'], answer[i]['pet']
for i in range(1,6):
if answer[i]['pet'] == 'fish':
owner_of_fish = answer[i]['nationality']
break
print "Answer to riddle question: who has the fish for a pet? - the", \
owner_of_fish
# end of script -------------------------------------------------------