|
| 1 | +/* https://rosettacode.org/wiki/Zebra_puzzle#Python */ |
| 2 | + |
| 3 | +import psyco; psyco.full() |
| 4 | + |
| 5 | +class Content: elems= """Beer Coffee Milk Tea Water |
| 6 | + Danish English German Norwegian Swedish |
| 7 | + Blue Green Red White Yellow |
| 8 | + Blend BlueMaster Dunhill PallMall Prince |
| 9 | + Bird Cat Dog Horse Zebra""".split() |
| 10 | +class Test: elems= "Drink Person Color Smoke Pet".split() |
| 11 | +class House: elems= "One Two Three Four Five".split() |
| 12 | + |
| 13 | +for c in (Content, Test, House): |
| 14 | + c.values = range(len(c.elems)) |
| 15 | + for i, e in enumerate(c.elems): |
| 16 | + exec "%s.%s = %d" % (c.__name__, e, i) |
| 17 | + |
| 18 | +def finalChecks(M): |
| 19 | + def diff(a, b, ca, cb): |
| 20 | + for h1 in House.values: |
| 21 | + for h2 in House.values: |
| 22 | + if M[ca][h1] == a and M[cb][h2] == b: |
| 23 | + return h1 - h2 |
| 24 | + assert False |
| 25 | + |
| 26 | + return abs(diff(Content.Norwegian, Content.Blue, |
| 27 | + Test.Person, Test.Color)) == 1 and \ |
| 28 | + diff(Content.Green, Content.White, |
| 29 | + Test.Color, Test.Color) == -1 and \ |
| 30 | + abs(diff(Content.Horse, Content.Dunhill, |
| 31 | + Test.Pet, Test.Smoke)) == 1 and \ |
| 32 | + abs(diff(Content.Water, Content.Blend, |
| 33 | + Test.Drink, Test.Smoke)) == 1 and \ |
| 34 | + abs(diff(Content.Blend, Content.Cat, |
| 35 | + Test.Smoke, Test.Pet)) == 1 |
| 36 | + |
| 37 | +def constrained(M, atest): |
| 38 | + if atest == Test.Drink: |
| 39 | + return M[Test.Drink][House.Three] == Content.Milk |
| 40 | + elif atest == Test.Person: |
| 41 | + for h in House.values: |
| 42 | + if ((M[Test.Person][h] == Content.Norwegian and |
| 43 | + h != House.One) or |
| 44 | + (M[Test.Person][h] == Content.Danish and |
| 45 | + M[Test.Drink][h] != Content.Tea)): |
| 46 | + return False |
| 47 | + return True |
| 48 | + elif atest == Test.Color: |
| 49 | + for h in House.values: |
| 50 | + if ((M[Test.Person][h] == Content.English and |
| 51 | + M[Test.Color][h] != Content.Red) or |
| 52 | + (M[Test.Drink][h] == Content.Coffee and |
| 53 | + M[Test.Color][h] != Content.Green)): |
| 54 | + return False |
| 55 | + return True |
| 56 | + elif atest == Test.Smoke: |
| 57 | + for h in House.values: |
| 58 | + if ((M[Test.Color][h] == Content.Yellow and |
| 59 | + M[Test.Smoke][h] != Content.Dunhill) or |
| 60 | + (M[Test.Smoke][h] == Content.BlueMaster and |
| 61 | + M[Test.Drink][h] != Content.Beer) or |
| 62 | + (M[Test.Person][h] == Content.German and |
| 63 | + M[Test.Smoke][h] != Content.Prince)): |
| 64 | + return False |
| 65 | + return True |
| 66 | + elif atest == Test.Pet: |
| 67 | + for h in House.values: |
| 68 | + if ((M[Test.Person][h] == Content.Swedish and |
| 69 | + M[Test.Pet][h] != Content.Dog) or |
| 70 | + (M[Test.Smoke][h] == Content.PallMall and |
| 71 | + M[Test.Pet][h] != Content.Bird)): |
| 72 | + return False |
| 73 | + return finalChecks(M) |
| 74 | + |
| 75 | +def show(M): |
| 76 | + for h in House.values: |
| 77 | + print "%5s:" % House.elems[h], |
| 78 | + for t in Test.values: |
| 79 | + print "%10s" % Content.elems[M[t][h]], |
| 80 | + print |
| 81 | + |
| 82 | +def solve(M, t, n): |
| 83 | + if n == 1 and constrained(M, t): |
| 84 | + if t < 4: |
| 85 | + solve(M, Test.values[t + 1], 5) |
| 86 | + else: |
| 87 | + show(M) |
| 88 | + return |
| 89 | + |
| 90 | + for i in xrange(n): |
| 91 | + solve(M, t, n - 1) |
| 92 | + M[t][0 if n % 2 else i], M[t][n - 1] = \ |
| 93 | + M[t][n - 1], M[t][0 if n % 2 else i] |
| 94 | + |
| 95 | +def main(): |
| 96 | + M = [[None] * len(Test.elems) for _ in xrange(len(House.elems))] |
| 97 | + for t in Test.values: |
| 98 | + for h in House.values: |
| 99 | + M[t][h] = Content.values[t * 5 + h] |
| 100 | + |
| 101 | + solve(M, Test.Drink, 5) |
| 102 | + |
| 103 | +main() |
0 commit comments