2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,101 +1,73 @@
import psyco; psyco.full()
from logpy import *
from logpy.core import lall
import time
class Content: elems= """Beer Coffee Milk Tea Water
Danish English German Norwegian Swedish
Blue Green Red White Yellow
Blend BlueMaster Dunhill PallMall Prince
Bird Cat Dog Horse Zebra""".split()
class Test: elems= "Drink Person Color Smoke Pet".split()
class House: elems= "One Two Three Four Five".split()
def lefto(q, p, list):
# give me q such that q is left of p in list
# zip(list, list[1:]) gives a list of 2-tuples of neighboring combinations
# which can then be pattern-matched against the query
return membero((q,p), zip(list, list[1:]))
for c in (Content, Test, House):
c.values = range(len(c.elems))
for i, e in enumerate(c.elems):
exec "%s.%s = %d" % (c.__name__, e, i)
def nexto(q, p, list):
# give me q such that q is next to p in list
# match lefto(q, p) OR lefto(p, q)
# requirement of vector args instead of tuples doesn't seem to be documented
return conde([lefto(q, p, list)], [lefto(p, q, list)])
def finalChecks(M):
def diff(a, b, ca, cb):
for h1 in House.values:
for h2 in House.values:
if M[ca][h1] == a and M[cb][h2] == b:
return h1 - h2
assert False
houses = var()
return abs(diff(Content.Norwegian, Content.Blue,
Test.Person, Test.Color)) == 1 and \
diff(Content.Green, Content.White,
Test.Color, Test.Color) == -1 and \
abs(diff(Content.Horse, Content.Dunhill,
Test.Pet, Test.Smoke)) == 1 and \
abs(diff(Content.Water, Content.Blend,
Test.Drink, Test.Smoke)) == 1 and \
abs(diff(Content.Blend, Content.Cat,
Test.Smoke, Test.Pet)) == 1
zebraRules = lall(
# there are 5 houses
(eq, (var(), var(), var(), var(), var()), houses),
# the Englishman's house is red
(membero, ('Englishman', var(), var(), var(), 'red'), houses),
# the Swede has a dog
(membero, ('Swede', var(), var(), 'dog', var()), houses),
# the Dane drinks tea
(membero, ('Dane', var(), 'tea', var(), var()), houses),
# the Green house is left of the White house
(lefto, (var(), var(), var(), var(), 'green'),
(var(), var(), var(), var(), 'white'), houses),
# coffee is the drink of the green house
(membero, (var(), var(), 'coffee', var(), 'green'), houses),
# the Pall Mall smoker has birds
(membero, (var(), 'Pall Mall', var(), 'birds', var()), houses),
# the yellow house smokes Dunhills
(membero, (var(), 'Dunhill', var(), var(), 'yellow'), houses),
# the middle house drinks milk
(eq, (var(), var(), (var(), var(), 'milk', var(), var()), var(), var()), houses),
# the Norwegian is the first house
(eq, (('Norwegian', var(), var(), var(), var()), var(), var(), var(), var()), houses),
# the Blend smoker is in the house next to the house with cats
(nexto, (var(), 'Blend', var(), var(), var()),
(var(), var(), var(), 'cats', var()), houses),
# the Dunhill smoker is next to the house where they have a horse
(nexto, (var(), 'Dunhill', var(), var(), var()),
(var(), var(), var(), 'horse', var()), houses),
# the Blue Master smoker drinks beer
(membero, (var(), 'Blue Master', 'beer', var(), var()), houses),
# the German smokes Prince
(membero, ('German', 'Prince', var(), var(), var()), houses),
# the Norwegian is next to the blue house
(nexto, ('Norwegian', var(), var(), var(), var()),
(var(), var(), var(), var(), 'blue'), houses),
# the house next to the Blend smoker drinks water
(nexto, (var(), 'Blend', var(), var(), var()),
(var(), var(), 'water', var(), var()), houses),
# one of the houses has a zebra--but whose?
(membero, (var(), var(), var(), 'zebra', var()), houses)
)
def constrained(M, atest):
if atest == Test.Drink:
return M[Test.Drink][House.Three] == Content.Milk
elif atest == Test.Person:
for h in House.values:
if ((M[Test.Person][h] == Content.Norwegian and
h != House.One) or
(M[Test.Person][h] == Content.Danish and
M[Test.Drink][h] != Content.Tea)):
return False
return True
elif atest == Test.Color:
for h in House.values:
if ((M[Test.Person][h] == Content.English and
M[Test.Color][h] != Content.Red) or
(M[Test.Drink][h] == Content.Coffee and
M[Test.Color][h] != Content.Green)):
return False
return True
elif atest == Test.Smoke:
for h in House.values:
if ((M[Test.Color][h] == Content.Yellow and
M[Test.Smoke][h] != Content.Dunhill) or
(M[Test.Smoke][h] == Content.BlueMaster and
M[Test.Drink][h] != Content.Beer) or
(M[Test.Person][h] == Content.German and
M[Test.Smoke][h] != Content.Prince)):
return False
return True
elif atest == Test.Pet:
for h in House.values:
if ((M[Test.Person][h] == Content.Swedish and
M[Test.Pet][h] != Content.Dog) or
(M[Test.Smoke][h] == Content.PallMall and
M[Test.Pet][h] != Content.Bird)):
return False
return finalChecks(M)
t0 = time.time()
solutions = run(0, houses, zebraRules)
t1 = time.time()
dur = t1-t0
def show(M):
for h in House.values:
print "%5s:" % House.elems[h],
for t in Test.values:
print "%10s" % Content.elems[M[t][h]],
print
count = len(solutions)
zebraOwner = [house for house in solutions[0] if 'zebra' in house][0][0]
def solve(M, t, n):
if n == 1 and constrained(M, t):
if t < 4:
solve(M, Test.values[t + 1], 5)
else:
show(M)
return
for i in xrange(n):
solve(M, t, n - 1)
M[t][0 if n % 2 else i], M[t][n - 1] = \
M[t][n - 1], M[t][0 if n % 2 else i]
def main():
M = [[None] * len(Test.elems) for _ in xrange(len(House.elems))]
for t in Test.values:
for h in House.values:
M[t][h] = Content.values[t * 5 + h]
solve(M, Test.Drink, 5)
main()
print "%i solutions in %.2f seconds" % (count, dur)
print "The %s is the owner of the zebra" % zebraOwner
print "Here are all the houses:"
for line in solutions[0]:
print str(line)

View file

@ -1,89 +1,101 @@
from itertools import permutations
import psyco
psyco.full()
import psyco; psyco.full()
class Number:elems= "One Two Three Four Five".split()
class Color: elems= "Red Green Blue White Yellow".split()
class Drink: elems= "Milk Coffee Water Beer Tea".split()
class Smoke: elems= "PallMall Dunhill Blend BlueMaster Prince".split()
class Pet: elems= "Dog Cat Zebra Horse Bird".split()
class Nation:elems= "British Swedish Danish Norvegian German".split()
class Content: elems= """Beer Coffee Milk Tea Water
Danish English German Norwegian Swedish
Blue Green Red White Yellow
Blend BlueMaster Dunhill PallMall Prince
Bird Cat Dog Horse Zebra""".split()
class Test: elems= "Drink Person Color Smoke Pet".split()
class House: elems= "One Two Three Four Five".split()
for c in (Number, Color, Drink, Smoke, Pet, Nation):
for c in (Content, Test, House):
c.values = range(len(c.elems))
for i, e in enumerate(c.elems):
exec "%s.%s = %d" % (c.__name__, e, i)
def is_possible(number, color, drink, smoke, pet):
if number and number[Nation.Norvegian] != Number.One:
return False
if color and color[Nation.British] != Color.Red:
return False
if drink and drink[Nation.Danish] != Drink.Tea:
return False
if smoke and smoke[Nation.German] != Smoke.Prince:
return False
if pet and pet[Nation.Swedish] != Pet.Dog:
return False
def finalChecks(M):
def diff(a, b, ca, cb):
for h1 in House.values:
for h2 in House.values:
if M[ca][h1] == a and M[cb][h2] == b:
return h1 - h2
assert False
if not number or not color or not drink or not smoke or not pet:
return True
return abs(diff(Content.Norwegian, Content.Blue,
Test.Person, Test.Color)) == 1 and \
diff(Content.Green, Content.White,
Test.Color, Test.Color) == -1 and \
abs(diff(Content.Horse, Content.Dunhill,
Test.Pet, Test.Smoke)) == 1 and \
abs(diff(Content.Water, Content.Blend,
Test.Drink, Test.Smoke)) == 1 and \
abs(diff(Content.Blend, Content.Cat,
Test.Smoke, Test.Pet)) == 1
for i in xrange(5):
if color[i] == Color.Green and drink[i] != Drink.Coffee:
return False
if smoke[i] == Smoke.PallMall and pet[i] != Pet.Bird:
return False
if color[i] == Color.Yellow and smoke[i] != Smoke.Dunhill:
return False
if number[i] == Number.Three and drink[i] != Drink.Milk:
return False
if smoke[i] == Smoke.BlueMaster and drink[i] != Drink.Beer:
return False
if color[i] == Color.Blue and number[i] != Number.Two:
return False
def constrained(M, atest):
if atest == Test.Drink:
return M[Test.Drink][House.Three] == Content.Milk
elif atest == Test.Person:
for h in House.values:
if ((M[Test.Person][h] == Content.Norwegian and
h != House.One) or
(M[Test.Person][h] == Content.Danish and
M[Test.Drink][h] != Content.Tea)):
return False
return True
elif atest == Test.Color:
for h in House.values:
if ((M[Test.Person][h] == Content.English and
M[Test.Color][h] != Content.Red) or
(M[Test.Drink][h] == Content.Coffee and
M[Test.Color][h] != Content.Green)):
return False
return True
elif atest == Test.Smoke:
for h in House.values:
if ((M[Test.Color][h] == Content.Yellow and
M[Test.Smoke][h] != Content.Dunhill) or
(M[Test.Smoke][h] == Content.BlueMaster and
M[Test.Drink][h] != Content.Beer) or
(M[Test.Person][h] == Content.German and
M[Test.Smoke][h] != Content.Prince)):
return False
return True
elif atest == Test.Pet:
for h in House.values:
if ((M[Test.Person][h] == Content.Swedish and
M[Test.Pet][h] != Content.Dog) or
(M[Test.Smoke][h] == Content.PallMall and
M[Test.Pet][h] != Content.Bird)):
return False
return finalChecks(M)
for j in xrange(5):
if (color[i] == Color.Green and
color[j] == Color.White and
number[j] - number[i] != 1):
return False
def show(M):
for h in House.values:
print "%5s:" % House.elems[h],
for t in Test.values:
print "%10s" % Content.elems[M[t][h]],
print
diff = abs(number[i] - number[j])
if smoke[i] == Smoke.Blend and pet[j] == Pet.Cat and diff != 1:
return False
if pet[i]==Pet.Horse and smoke[j]==Smoke.Dunhill and diff != 1:
return False
if smoke[i]==Smoke.Blend and drink[j]==Drink.Water and diff!=1:
return False
def solve(M, t, n):
if n == 1 and constrained(M, t):
if t < 4:
solve(M, Test.values[t + 1], 5)
else:
show(M)
return
return True
def show_row(t, data):
print "%6s: %12s%12s%12s%12s%12s" % (
t.__name__, t.elems[data[0]],
t.elems[data[1]], t.elems[data[2]],
t.elems[data[3]], t.elems[data[4]])
for i in xrange(n):
solve(M, t, n - 1)
M[t][0 if n % 2 else i], M[t][n - 1] = \
M[t][n - 1], M[t][0 if n % 2 else i]
def main():
perms = list(permutations(range(5)))
M = [[None] * len(Test.elems) for _ in xrange(len(House.elems))]
for t in Test.values:
for h in House.values:
M[t][h] = Content.values[t * 5 + h]
for number in perms:
if is_possible(number, None, None, None, None):
for color in perms:
if is_possible(number, color, None, None, None):
for drink in perms:
if is_possible(number, color, drink, None, None):
for smoke in perms:
if is_possible(number, color, drink, smoke, None):
for pet in perms:
if is_possible(number, color, drink, smoke, pet):
print "Found a solution:"
show_row(Nation, range(5))
show_row(Number, number)
show_row(Color, color)
show_row(Drink, drink)
show_row(Smoke, smoke)
show_row(Pet, pet)
print
solve(M, Test.Drink, 5)
main()

View file

@ -0,0 +1,51 @@
from itertools import permutations
class Number:elems= "One Two Three Four Five".split()
class Color: elems= "Red Green Blue White Yellow".split()
class Drink: elems= "Milk Coffee Water Beer Tea".split()
class Smoke: elems= "PallMall Dunhill Blend BlueMaster Prince".split()
class Pet: elems= "Dog Cat Zebra Horse Bird".split()
class Nation:elems= "British Swedish Danish Norvegian German".split()
for c in (Number, Color, Drink, Smoke, Pet, Nation):
for i, e in enumerate(c.elems):
exec "%s.%s = %d" % (c.__name__, e, i)
def show_row(t, data):
print "%6s: %12s%12s%12s%12s%12s" % (
t.__name__, t.elems[data[0]],
t.elems[data[1]], t.elems[data[2]],
t.elems[data[3]], t.elems[data[4]])
def main():
perms = list(permutations(range(5)))
for number in perms:
if number[Nation.Norvegian] == Number.One: # Constraint 10
for color in perms:
if color[Nation.British] == Color.Red: # Constraint 2
if number[color.index(Color.Blue)] == Number.Two: # Constraint 15+10
if number[color.index(Color.White)] - number[color.index(Color.Green)] == 1: # Constraint 5
for drink in perms:
if drink[Nation.Danish] == Drink.Tea: # Constraint 4
if drink[color.index(Color.Green)] == Drink.Coffee: # Constraint 6
if drink[number.index(Number.Three)] == Drink.Milk: # Constraint 9
for smoke in perms:
if smoke[Nation.German] == Smoke.Prince: # Constraint 14
if drink[smoke.index(Smoke.BlueMaster)] == Drink.Beer: # Constraint 13
if smoke[color.index(Color.Yellow)] == Smoke.Dunhill: # Constraint 8
if number[smoke.index(Smoke.Blend)] - number[drink.index(Drink.Water)] in (1, -1): # Constraint 16
for pet in perms:
if pet[Nation.Swedish] == Pet.Dog: # Constraint 3
if pet[smoke.index(Smoke.PallMall)] == Pet.Bird: # Constraint 7
if number[pet.index(Pet.Horse)] - number[smoke.index(Smoke.Dunhill)] in (1, -1): # Constraint 12
if number[smoke.index(Smoke.Blend)] - number[pet.index(Pet.Cat)] in (1, -1): # Constraint 11
print "Found a solution:"
show_row(Nation, range(5))
show_row(Number, number)
show_row(Color, color)
show_row(Drink, drink)
show_row(Smoke, smoke)
show_row(Pet, pet)
print
main()