/* Zebra puzzle in Picat. Lewis Carrol's classical puzzle with five houses and a zebra: Five men with different nationalities live in the first five houses of a street. They practise five distinct professions, and each of them has a favourite animal and a favourite drink, all of them different. The five houses are painted in different colours. The Englishman lives in a red house. The Spaniard owns a dog. The Japanese is a painter. The Italian drinks tea. The Norwegian lives in the first house on the left. The owner of the green house drinks coffee. The green house is on the right of the white one. The sculptor breeds snails. The diplomat lives in the yellow house. Milk is drunk in the middle house. The Norwegian's house is next to the blue one. The violinist drinks fruit juice. The fox is in a house next to that of the doctor. The horse is in a house next to that of the diplomat. Who owns a Zebra, and who drinks water? The solution: The japanese owns the zebra The norwegian drinks water nationalities = [3,4,5,2,1] color = [3,5,4,1,2] profession = [5,3,1,4,2] pet = [4,3,1,2,5] drink = [2,5,3,4,1] zebra = 5 water = 1 nat = [Norwegian,Italian,English,Spaniard,Japanese] color = [Yellow,Blue,Red,White,Green] profession = [Diplomat,Doctor,Sculptor,Violinist,Painter] pet = [Fox,Horse,Snails,Dog,Zebra] drink = [Water,Tea,Milk,Juice,Coffee] Model created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> Nat = [English, Spaniard, Japanese, Italian, Norwegian], Color = [Red, Green, White, Yellow, Blue], Profession = [Painter, Sculptor, Diplomat, Violinist, Doctor], Pet = [Dog, Snails, Fox, Horse, Zebra], Drink = [Tea, Coffee, Milk, Juice, Water], NatS = ["English", "Spaniard", "Japanese", "Italian", "Norwegian"], ColorS = ["Red", "Green", "White", "Yellow", "Blue"], ProfessionS = ["Painter", "Sculptor", "Diplomat", "Violinist", "Doctor"], PetS = ["Dog", "Snails", "Fox", "Horse", "Zebra"], DrinkS = ["Tea", "Coffee", "Milk", "Juice", "Water"], Nat :: 1..5, Color :: 1..5, Profession :: 1..5, Pet :: 1..5, Drink :: 1..5, all_different(Nat), all_different(Color), all_different(Profession), all_different(Pet), all_different(Drink), English #= Red, Spaniard #= Dog, Japanese #= Painter, Italian #= Tea, Norwegian #= 1, Green #= Coffee, Green #= White + 1, Sculptor #= Snails, Diplomat #= Yellow, Milk #= 3, abs(Norwegian - Blue) #= 1, Violinist #= Juice, abs(Fox-Doctor) #= 1, abs(Horse - Diplomat) #= 1, solve(Nat ++ Color ++ Profession ++ Pet ++ Drink), NatNames = [English=english, Spaniard=spaniard, Japanese=japanese, Italian=italian, Norwegian=norwegian], member((Zebra=ZebraNat), NatNames), member((Water=WaterNat), NatNames), printf("The %w owns the zebra\n", ZebraNat), printf("The %w drinks water\n", WaterNat), println(nationalities=Nat), println('color '=Color), println('profession '=Profession), println('pet '=Pet), println('drink '=Drink), println('zebra '=Zebra), println('water '=Water), nl, % The solution in a little more clear way. % Note the use of assignment/2 (inverse/2) to % get the value of the I'th variable. assignment(Nat,NatInv), println('nat '= [NatS[I] : I in NatInv]), assignment(Color,ColorInv), println('color '= [ColorS[I] : I in ColorInv]), assignment(Profession,ProfessionInv), println('profession '= [ProfessionS[I] : I in ProfessionInv]), assignment(Pet,PetInv), println('pet '= [PetS[I] : I in PetInv]), assignment(Drink,DrinkInv), println('drink '= [DrinkS[I] : I in DrinkInv]), nl, fail, nl. go => true.