This repository was archived by the owner on Mar 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzebra_puzzle.py
More file actions
144 lines (103 loc) · 3.58 KB
/
Copy pathzebra_puzzle.py
File metadata and controls
144 lines (103 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from itertools import permutations
_nations = ["Norwegian", "Ukrainian", "Englishman", "Spaniard", "Japanese"]
yellow, blue, red, ivory, green = range(5)
fox, horse, snails, dog, zebra = range(5)
water, tea, milk, orange_juice, coffee = range(5)
kools, chesterfield, old_gold, lucky_strike, parliament = range(5)
def drinks_water():
for house in solution():
if house["drink"] == water:
return house["nation"]
def owns_zebra():
for house in solution():
if house["pet"] == zebra:
return house["nation"]
def solution():
for nations in permutations(_nations):
for colors in permutations(range(5)):
for pets in permutations(range(5)):
for drinks in permutations(range(5)):
for smokes in permutations(range(5)):
houses = [{
"nation": nations[i],
"color": colors[i],
"pet": pets[i],
"drink": drinks[i],
"smoke": smokes[i],
} for i in range(5)]
if all(func(houses) for func in check_funcs):
return houses
def known1(houses):
return len(houses) == 5
def known2(houses):
return any(house["nation"] == "Englishman"
and house["color"] == red for house in houses)
def known3(houses):
return any(house["nation"] == "Spaniard"
and house["pet"] == dog for house in houses)
def known4(houses):
return any(house["drink"] == coffee
and house["color"] == green for house in houses)
def known5(houses):
return any(house["nation"] == "Ukrainian"
and house["drink"] == tea for house in houses)
def known6(houses):
return any(houses[i + 1]["color"] == green
and houses[i]["color"] == ivory for i in range(4))
def known7(houses):
return any(house["smoke"] == old_gold
and house["pet"] == snails for house in houses)
def known8(houses):
return any(house["smoke"] == kools
and house["color"] == yellow for house in houses)
def known9(houses):
return houses[2]["drink"] == milk
def known10(houses):
return houses[0]["nation"] == "Norwegian"
def known11(houses):
for i in range(5):
if houses[i]["smoke"] == chesterfield:
if i > 0 and houses[i - 1]["pet"] == fox:
return True
if i < 4 and houses[i + 1]["pet"] == fox:
return True
return False
def known12(houses):
for i in range(5):
if houses[i]["smoke"] == kools:
if i > 0 and houses[i - 1]["pet"] == horse:
return True
if i < 4 and houses[i + 1]["pet"] == horse:
return True
return False
def known13(houses):
return any(house["smoke"] == lucky_strike
and house["drink"] == orange_juice for house in houses)
def known14(houses):
return any(house["nation"] == "Japanese"
and house["smoke"] == parliament for house in houses)
def known15(houses):
for i in range(5):
if houses[i]["nation"] == "Norwegian":
if i > 0 and houses[i - 1]["color"] == blue:
return True
if i < 4 and houses[i + 1]["color"] == blue:
return True
return False
check_funcs = [
known1,
known2,
known3,
known4,
known5,
known6,
known7,
known8,
known9,
known10,
known11,
known12,
known13,
known14,
known15,
]