/* Zebra Icecream in Picat. From https://www.ahapuzzles.com/logic/zebra/ice-creams/ """ Ice Creams Zebra Puzzle Four boys are side by side talking about their favorite ice cream flavor. Find out who likes mint ice cream. - The oldest boy is wearing the Blue shirt. - Nicholas is next to the boy who likes Strawberry ice cream. - Harris is at the fourth position. - Miller's favorite ice cream is Chocolate. - The youngest boy is next to the boy that is wearing the Green shirt. - Young is at the third position. - The 9-year-old boy is exactly to the left of Young. - Kevin is next to the 9-year-old boy. - At the second position is the boy wearing the Black shirt. - Daniel likes Strawberry ice cream. - At the fourth position is the boy who likes Vanilla ice cream. - The 7-year-old boy is somewhere between the boy wearing the Blue shirt and the 8-year-old boy, in that order. - Nicholas is at the third position. """ If Position is fixed (i.e. Position = 1..4) then the solution is found pre-solve (no solve/1 needed). This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => N = 4, FirstNameS = [daniel,joseph,kevin,nicholas], FirstName = [Daniel,Joseph,Kevin,Nicholas], FirstName :: 1..N, LastNameS = [harris,miller,taylor,young], LastName = [Harris,Miller,Taylor,Young], LastName :: 1..N, ShirtS = [black,blue,green,red], Shirt = [Black,Blue,Green,Red], Shirt :: 1..N, IcecreamS = [chocolate,mint,strawberry,vanilla], Icecream = [Chocolate,Mint,Strawberry,Vanilla], Icecream :: 1..N, Age = new_list(N), Age :: 7..10, % Fix the positions Position = new_list(N), Position = 1..N, all_different(FirstName), all_different(LastName), all_different(Shirt), all_different(Icecream), all_different(Age), % all_different(Position), % increasing(Position), % Sort on position % - The oldest boy is wearing the Blue shirt. element(Blue,Age,10), % - Nicholas is next to the boy who likes Strawberry ice cream. element(Nicholas,Position,NicholasPos), element(Strawberry,Position,StrawberryPos), abs(StrawberryPos-NicholasPos) #= 1, % - Harris is at the fourth position. element(Harris,Position,4), % - Miller's favorite ice cream is Chocolate. Miller #= Chocolate, % - The youngest boy is next to the boy that is wearing the Green shirt. element(Age7,Age,7), element(Age7,Position,Age7Pos), element(Green,Position,GreenPos), abs(GreenPos-Age7Pos) #= 1, % - Young is at the third position. element(Young,Position,3), % - The 9-year-old boy is exactly to the left of Young. element(Age9,Age,9), element(Age9,Position,Age9Pos), element(Young,Position,YoungPos), Age9Pos + 1 #= YoungPos, % - Kevin is next to the 9-year-old boy. element(Kevin,Position,KevinPos), abs(KevinPos-Age9Pos) #= 1, % - At the second position is the boy wearing the Black shirt. element(Black,Position,2), % - Daniel likes Strawberry ice cream. Daniel #= Strawberry, % - At the fourth position is the boy who likes Vanilla ice cream. element(Vanilla,Position,4), % - The 7-year-old boy is somewhere between the boy wearing the Blue shirt and the 8-year-old boy, in that order. element(Blue,Position,BluePos), element(Age8,Age,8), element(Age8,Position,Age8Pos), BluePos #< Age7Pos, Age7Pos #< Age8Pos, % - Nicholas is at the third position. element(Nicholas,Position,3), Vars = FirstName ++ LastName ++ Shirt ++ Icecream ++ Age ++ Position, println(vars=Vars), % solve(Vars), % no need for solve/1 when Position is fixed to 1..4 println(firstName=FirstName), println('lastName '=LastName), println('shirt '=Shirt), println('icecream '=Icecream), println('age '=Age), println('position '=Position), nl, foreach(I in 1..N) nth(FN,FirstName,I), nth(LN,LastName,I), nth(Sh,Shirt,I), nth(Ic,Icecream,I), printf("%w name:%w %w shirt:%w icecream:%w age:%w\n",Position[I], FirstNameS[FN],LastNameS[LN], ShirtS[Sh], IcecreamS[Ic], Age[I]) end, nl, fail, % Ensure that it's a unique solution. nl.