-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.html
119 lines (111 loc) · 4.26 KB
/
output.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Zebra Puzzle Solution</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
}
h1 {
text-align: center;
color: #333;
}
pre {
background-color: #fff;
border: 1px solid #ccc;
padding: 10px;
overflow: auto;
}
</style>
</head>
<body>
<h1>Zebra Puzzle Solution</h1>
<pre id="solution"></pre>
<script>
// Define possible values for each attribute
const colors = ["red", "green", "yellow", "blue", "ivory"];
const nationalities = ["Englishman", "Spaniard", "Ukrainian", "Norwegian", "Japanese"];
const drinks = ["coffee", "tea", "milk", "orange juice", "water"];
const cigarettes = ["Kools", "Chesterfields", "Old Gold", "Lucky Strike", "Parliaments"];
const pets = ["dog", "snails", "fox", "horse", "zebra"];
// Initialize house slots to fill in
const houses = Array(5).fill(null).map(() => ({}));
function isValidAssignment(houses) {
// Constraint examples (fill with the Zebra Puzzle's full constraints)
if (houses[0].nationality && houses[0].nationality !== "Norwegian") {
return false;
}
for (let house of houses) {
if (house.nationality === "Englishman" && house.color !== "red") {
return false;
}
if (house.color === "red" && house.nationality !== "Englishman") {
return false;
}
}
// Ensure uniqueness of attributes across houses
const attributes = ["color", "nationality", "drink", "cigarette", "pet"];
for (let attr of attributes) {
const seen = new Set();
for (let house of houses) {
if (house[attr]) {
if (seen.has(house[attr])) {
return false;
}
seen.add(house[attr]);
}
}
}
return true;
}
function solveZebraPuzzle(index = 0) {
if (index === 5) {
if (isValidAssignment(houses)) {
return houses;
}
return null;
}
for (let color of colors) {
for (let nationality of nationalities) {
for (let drink of drinks) {
for (let cigarette of cigarettes) {
for (let pet of pets) {
houses[index] = {
color: color,
nationality: nationality,
drink: drink,
cigarette: cigarette,
pet: pet,
};
if (isValidAssignment(houses)) {
const result = solveZebraPuzzle(index + 1);
if (result) {
return result;
}
}
houses[index] = {}; // Reset assignment
}
}
}
}
}
return null;
}
// Run the solver and display the solution
const solution = solveZebraPuzzle();
const solutionElement = document.getElementById('solution');
if (solution) {
solution.forEach((house, index) => {
solutionElement.innerHTML += `House ${index + 1}: ${JSON.stringify(house)}\n`;
});
} else {
solutionElement.innerHTML = "No solution found.";
}
</script>
</body>
</html>