/* Who is Helen's husband in Picat. From Adrian Groza "Modelling Puzzles in First Order Logic" """ Puzzle 79. Who is Helen's husband? Yesterday evening, Helen and her husband invited their neighbours (two couples) for a dinner at home. The six of them sat at a round table. Helen tells you the following: "Victor sat on the left of the woman who sat on the left of the man who sat on the left of Anna. Esther sat on the left of the man who sat on the left of the woman who sat on the left of the man who sat on the left of the woman who sat on the left of my hus- band. Jim sat on the left of the woman who sat on the left of Roger. I did not sit beside my husband". The question: What is the name of Helen's husband? (taken from http:// www.puzzlesite.nl) """ Helen : pos 3 Anna : pos 5 Esther: pos 1 Victor: pos 2 Jim : pos 4 Roger : pos 6 husband_pos = 6 seating = [Esther,Victor,Helen,Jim,Anna,Roger] husband = Roger Thankfully it worked without having to use a lot of mod/2. Groza's order: Anna,Roger,Esther,Victor,Helen,Jim Husband: Roger 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 = 6, % three couples People = [Helen,Anna,Esther,Victor,Jim,Roger], People :: 1..N, Names = ["Helen","Anna","Esther","Victor","Jim","Roger"], % The positions of each person Positions = [HelenPos,AnnaPos,EstherPos,VictorPos,JimPos,RogerPos], Positions :: 1..N, assignment(People,Positions), % people <-> positions HusbandPos :: 1..N, all_different(People), % Yesterday evening, Helen and her husband invited their neighbours (two couples) for % a dinner at home. % The six of them sat at a round table. Helen tells you the following: % Victor sat on the left of the woman who sat on the left of the man who sat on the left % of Anna. % ... Victor,Woman,Man,Anna,... VictorPos #= AnnaPos - 3, % Esther sat on the left of the man who sat on the left of the woman who sat % on the left of the man who sat on the left of the woman who sat on the left of my hus- % band. % .. Esther,Man,Woman,Man,Woman,Husband,... EstherPos #= HusbandPos - 5, % Jim sat on the left of the woman who sat on the left of Roger. % ...Jim,Woman,Roger,... JimPos #= RogerPos - 2, % I did not sit beside my husband % not Husband,Helen or Helen,Husband abs(HelenPos-HusbandPos) #> 1, % The question: What is the name of Helen's husband? Vars = People ++ Positions ++ [HusbandPos], solve(Vars), % println(positions=People=Positions), Seating = new_list(N), foreach(I in 1..N) nth(P,People,I), printf("%-6s: pos %d\n",Names[I],P), Seating[P] = Names[I] end, println(husband_pos=HusbandPos), println(seating=Seating), println(husband=Names[HusbandPos]), nl, fail, nl. go => true.