/* Office puzzle in Picat. From https://stackoverflow.com/questions/60817726/prolog-office-puzzle """ I'm attempting to do a Prolog assignment for school, and basically it's trying to find out who has what office. The question is: Hunter, Laura, Jim, Sally, and Jack work in the same building with five adjacent offices. Hunter doesn’t work in the 5th office and Laura doesn’t work in the first office. Jim doesn’t work in the first or last office, and he is not in an office adjacent to Jack or Laura. Sally works in some office higher than Laura’s. Who works in what offices? """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> puzzle(P), Sorted = $[K-V : [K,V] in sort([[K,V] : $K-V in P],2)], println(P), println(Sorted), fail, nl. go => true. puzzle(P) :- findWhosOffice(P,Z), solve(Z). findWhosOffice(P, Z) :- P = $[hunter-Hunter, jack-Jack, jim-Jim, laura-Laura,sally-Sally], Z = [Hunter, Laura, Jim, Sally, Jack], Z :: 1..5, all_different(Z), Hunter #\= 5, Laura #\= 1, Jim #\= 1, Jim #\= 5, abs(Jim-Jack) #\=1, abs(Jim-Laura) #\=1, Sally #> Laura.