/* Zebra puzzle brute force approach in Picat. Another brute force variant using permutation/2. Note: The problem formulation is from http://hakank.org/picat/zebra.pi """ 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? """ Solution: 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 This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. main => go. go ?=> Result = zebraPuzzle(), foreach(Row in Result) println(Row) end, nl, fail, nl. go => true. % Is house h1 right of house h2? isRightOf(H1,H2) => H1 == H2 + 1. % Is house h1 next to house h2? nextTo(H1,H2) => abs(H1 - H2) == 1. % Compute the set of all solutions to the zebraPuzzle. zebraPuzzle = Result => permutation(1..5, Nat), Nat = [English, Spaniard, Japanese, Italian, Norwegian], Norwegian == 1, permutation(1..5, Color), Color = [Red, Green, White, Yellow, Blue], Green == White + 1, abs(Norwegian - Blue) == 1, English == Red, permutation(1..5, Profession), Profession = [Painter, Sculptor, Diplomat, Violinist, Doctor], Japanese == Painter, Diplomat == Yellow, permutation(1..5, Pet), Pet = [Dog, Snails, Fox, Horse, Zebra], abs(Fox-Doctor) == 1, abs(Horse - Diplomat) == 1, Spaniard == Dog, Sculptor == Snails, permutation(1..5, Drink), Drink = [Tea, Coffee, Milk, Juice, Water], Milk == 3, Italian == Tea, Green == Coffee, Violinist == Juice, Result = ['nationalities'=Nat, 'color '=Color, 'profession '=Profession, 'pet '=Pet, 'drink '=Drink, 'zebra '=Zebra, 'water '=Water].