This repository was archived by the owner on Apr 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzebra_puzzle.go
More file actions
175 lines (155 loc) · 3.59 KB
/
Copy pathzebra_puzzle.go
File metadata and controls
175 lines (155 loc) · 3.59 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package zebra
// https://github.com/alirezaudev
// https://linkedin.com/in/alireza-msv
type Solution struct {
DrinksWater string
OwnsZebra string
}
const (
// nationalities
englishman = 0
spaniard = 1
ukrainian = 2
norwegian = 3
japanese = 4
// colors
red = 0
green = 1
ivory = 2
yellow = 3
blue = 4
// drinks
water = 0
coffee = 1
tea = 2
milk = 3
orangeJuice = 4
// pets
dog = 0
snails = 1
fox = 2
horse = 3
zebra = 4
// smokes
oldGold = 0
kools = 1
chesterfields = 2
luckyStrike = 3
parliaments = 4
)
var nationalities = map[int]string{
englishman: "Englishman",
spaniard: "Spaniard",
ukrainian: "Ukrainian",
norwegian: "Norwegian",
japanese: "Japanese",
}
func SolvePuzzle() Solution {
indices := []int{0, 1, 2, 3, 4}
for _, nationality := range permutations(indices) {
// The Norwegian lives in the first house.
if nationality[0] != norwegian {
continue
}
// The Englishman lives in the red house.
for _, color := range permutations(indices) {
if pos(nationality, englishman) != pos(color, red) {
continue
}
// The green house is immediately to the right of the ivory house.
if pos(color, green) != pos(color, ivory)+1 {
continue
}
// The Norwegian lives next to the blue house.
if !nextTo(pos(color, blue), pos(nationality, norwegian)) {
continue
}
for _, drink := range permutations(indices) {
// Milk is drunk in the middle house.
if pos(drink, milk) != 2 {
continue
}
// The Ukrainian drinks tea.
if pos(nationality, ukrainian) != pos(drink, tea) {
continue
}
// Coffee is drunk in the green house.
if pos(drink, coffee) != pos(color, green) {
continue
}
for _, pet := range permutations(indices) {
// The Spaniard owns the dog.
if pos(nationality, spaniard) != pos(pet, dog) {
continue
}
for _, smoke := range permutations(indices) {
// The Old Gold smoker owns snails.
if pos(smoke, oldGold) != pos(pet, snails) {
continue
}
// Kools are smoked in the yellow house
if pos(smoke, kools) != pos(color, yellow) {
continue
}
// The man who smokes Chesterfields lives in the house next to the man with the fox.
if !nextTo(pos(smoke, chesterfields), pos(pet, fox)) {
continue
}
// Kools are smoked in the house next to the house where the horse is kept.
if !nextTo(pos(smoke, kools), pos(pet, horse)) {
continue
}
// The Lucky Strike smoker drinks orange juice.
if pos(smoke, luckyStrike) != pos(drink, orangeJuice) {
continue
}
// The Japanese smokes Parliaments.
if pos(nationality, japanese) != pos(smoke, parliaments) {
continue
}
return Solution{
DrinksWater: nationalities[nationality[pos(drink, water)]],
OwnsZebra: nationalities[nationality[pos(pet, zebra)]],
}
}
}
}
}
}
return Solution{}
}
func permutations(arr []int) [][]int {
var result [][]int
var generate func(int)
a := make([]int, len(arr))
copy(a, arr)
generate = func(n int) {
if n == 1 {
temp := make([]int, len(a))
copy(temp, a)
result = append(result, temp)
return
}
for i := 0; i < n; i++ {
generate(n - 1)
if n%2 == 1 {
a[0], a[n-1] = a[n-1], a[0]
} else {
a[i], a[n-1] = a[n-1], a[i]
}
}
}
generate(len(a))
return result
}
func pos(arr []int, value int) int {
for i, v := range arr {
if v == value {
return i
}
}
return -1
}
func nextTo(a, b int) bool {
return a == b+1 || a == b-1
}