186 lines
6.6 KiB
Plaintext
186 lines
6.6 KiB
Plaintext
Enum attr
|
|
Colour = 1
|
|
Nationality
|
|
Beverage
|
|
Smoke
|
|
Pet
|
|
End Enum
|
|
|
|
Enum Drinks_
|
|
Beer = 1
|
|
Coffee
|
|
Milk
|
|
Tea
|
|
Water
|
|
End Enum
|
|
|
|
Enum nations
|
|
Danish = 1
|
|
English
|
|
German
|
|
Norwegian
|
|
Swedish
|
|
End Enum
|
|
|
|
Enum colors
|
|
Blue = 1
|
|
Green
|
|
Red
|
|
White
|
|
Yellow
|
|
End Enum
|
|
|
|
Enum tobaccos
|
|
Blend = 1
|
|
BlueMaster
|
|
Dunhill
|
|
PallMall
|
|
Prince
|
|
End Enum
|
|
|
|
Enum animals
|
|
Bird = 1
|
|
Cat
|
|
Dog
|
|
Horse
|
|
Zebra
|
|
End Enum
|
|
|
|
Const factorial5 = 120
|
|
Dim Shared As String permutation(120), perm(5)
|
|
Dim Shared As String Colours(5), Nationalities(5), Drinks(5), Smokes(5), Pets(5)
|
|
Dim Shared As Integer index
|
|
|
|
Sub generate(n As Integer, A() As Integer)
|
|
Dim As Integer i
|
|
If n = 1 Then
|
|
Dim tmp As String = ""
|
|
For i = 1 To 5
|
|
tmp &= Str(A(i)) & " "
|
|
Next i
|
|
permutation(index) = tmp
|
|
index += 1
|
|
Else
|
|
For i = 1 To n
|
|
generate(n - 1, A())
|
|
If n Mod 2 = 0 Then
|
|
Swap A(i), A(n)
|
|
Else
|
|
Swap A(1), A(n)
|
|
End If
|
|
Next i
|
|
End If
|
|
End Sub
|
|
|
|
Function house(i As Integer, nombre As Integer) As Integer
|
|
For x As Integer = 1 To 5
|
|
If Val(Mid(perm(i), x * 2 - 1, 1)) = nombre Then Return x
|
|
Next x
|
|
Return 0
|
|
End Function
|
|
|
|
Function left_of(h1 As Integer, h2 As Integer) As Boolean
|
|
Return (h1 - h2) = -1
|
|
End Function
|
|
|
|
Function next_to(h1 As Integer, h2 As Integer) As Boolean
|
|
Return Abs(h1 - h2) = 1
|
|
End Function
|
|
|
|
Sub print_house(i As Integer)
|
|
Print Using "####: "; i;
|
|
Print Using "\ \ \ \ \ \ \ \ \ \"; _
|
|
Colours(Val(Mid(perm(Colour), i * 2 - 1, 1))); _
|
|
Nationalities(Val(Mid(perm(Nationality), i * 2 - 1, 1))); _
|
|
Drinks(Val(Mid(perm(Beverage), i * 2 - 1, 1))); _
|
|
Smokes(Val(Mid(perm(Smoke), i * 2 - 1, 1))); _
|
|
Pets(Val(Mid(perm(Pet), i * 2 - 1, 1)))
|
|
End Sub
|
|
|
|
Sub Zebra_puzzle()
|
|
Colours(1) = "blue": Colours(2) = "green": Colours(3) = "red": Colours(4) = "white": Colours(5) = "yellow"
|
|
Nationalities(1) = "Dane": Nationalities(2) = "English": Nationalities(3) = "German": Nationalities(4) = "Norwegian": Nationalities(5) = "Swede"
|
|
Drinks(1) = "beer": Drinks(2) = "coffee": Drinks(3) = "milk": Drinks(4) = "tea": Drinks(5) = "water"
|
|
Smokes(1) = "Blend": Smokes(2) = "Blue Master": Smokes(3) = "Dunhill": Smokes(4) = "Pall Mall": Smokes(5) = "Prince"
|
|
Pets(1) = "birds": Pets(2) = "cats": Pets(3) = "dog": Pets(4) = "horse": Pets(5) = "zebra"
|
|
|
|
Dim As String solperms(120, 5)
|
|
Dim As Integer solutions, i, c, n, d, s, p, j
|
|
Dim As Integer b(5)
|
|
For i = 1 To 5: b(i) = i: Next
|
|
'There are five houses.
|
|
index = 0
|
|
generate(5, b())
|
|
For c = 0 To factorial5 - 1
|
|
perm(Colour) = permutation(c)
|
|
'The green house is immediately to the left of the white house.
|
|
If left_of(house(Colour, Green), house(Colour, White)) Then
|
|
For n = 0 To factorial5 - 1
|
|
perm(Nationality) = permutation(n)
|
|
'The Norwegian lives in the first house.
|
|
'The English man lives in the red house.
|
|
'The Norwegian lives next to the blue house.
|
|
If house(Nationality, Norwegian) = 1 _
|
|
And house(Nationality, English) = house(Colour, Red) _
|
|
And next_to(house(Nationality, Norwegian), house(Colour, Blue)) Then
|
|
For d = 0 To factorial5 - 1
|
|
perm(Beverage) = permutation(d)
|
|
'The Dane drinks tea.
|
|
'They drink coffee in the green house.
|
|
'In the middle house they drink milk.
|
|
If house(Nationality, Danish) = house(Beverage, Tea) _
|
|
And house(Beverage, Coffee) = house(Colour, Green) _
|
|
And house(Beverage, Milk) = 3 Then
|
|
For s = 0 To factorial5 - 1
|
|
perm(Smoke) = permutation(s)
|
|
'In the yellow house they smoke Dunhill.
|
|
'The German smokes Prince.
|
|
'The man who smokes Blue Master drinks beer.
|
|
'They Drink water in a house next to the house where they smoke Blend.
|
|
If house(Colour, Yellow) = house(Smoke, Dunhill) _
|
|
And house(Nationality, German) = house(Smoke, Prince) _
|
|
And house(Smoke, BlueMaster) = house(Beverage, Beer) _
|
|
And next_to(house(Beverage, Water), house(Smoke, Blend)) Then
|
|
For p = 0 To factorial5 - 1
|
|
perm(Pet) = permutation(p)
|
|
'The Swede has a dog.
|
|
'The man who smokes Pall Mall has birds.
|
|
'The man who smokes Blend lives in the house next to the house with cats.
|
|
'In a house next to the house where they have a horse, they smoke Dunhill.
|
|
If house(Nationality, Swedish) = house(Pet, Dog) _
|
|
And house(Smoke, PallMall) = house(Pet, Bird) _
|
|
And next_to(house(Smoke, Blend), house(Pet, Cat)) _
|
|
And next_to(house(Pet, Horse), house(Smoke, Dunhill)) Then
|
|
For i = 1 To 5
|
|
print_house(i)
|
|
Next i
|
|
Print
|
|
solutions += 1
|
|
For j = 1 To 5
|
|
solperms(solutions - 1, j - 1) = perm(j)
|
|
Next j
|
|
End If
|
|
Next p
|
|
End If
|
|
Next s
|
|
End If
|
|
Next d
|
|
End If
|
|
Next n
|
|
End If
|
|
Next c
|
|
|
|
Print solutions & " solution" & Iif(solutions > 1, "s", "") & " found"
|
|
For i As Integer = 0 To solutions - 1
|
|
For j As Integer = 1 To 5
|
|
perm(j) = solperms(i, j - 1)
|
|
Next j
|
|
Print "The " & Nationalities(Val(Mid(perm(Nationality), house(Pet, Zebra) * 2 - 1, 1))) & " owns the Zebra"
|
|
Next i
|
|
End Sub
|
|
Print "House Colour Nation Drink Smoke Animal"
|
|
|
|
Zebra_puzzle()
|
|
|
|
Sleep
|