/* Library Books problem (Logic4Fun) in Picat. http://l4f.cecs.anu.edu.au/puzzles/beginner/library-books """ Recently the library send out nasty letters to students who had overdue books on loan. Three students, Arthur, Belinda and Clarissa, had exactly 25 overdue books between them, though I’m glad to say no one individual exceeded last year’s record number of 14. They are (not necessarily respectively) majoring in English, French and German. The student of French has three more overdue books than Arthur, and Clarissa has two more overdue books than the person majoring in English. Who is studying what, and how many books do they need to return to the library? """ 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 ?=> N = 3, People = [Arthur,_Belinda,Clarissa], People = 1..N, PeopleS = ["Arthur","Belinda","Clarissa"], Lang = [English,French,_German], Lang = 1..N, LangS = ["English","French","German"], % decision variables Major = new_list(N), Major :: 1..N, MajorInv = new_list(N), MajorInv :: 1..N, % "... though I’m glad to say no one individual exceeded last year’s record number of 14." Returned = new_list(N), Returned :: 1..14, all_different(Major), % "Three students, Arthur, Belinda and Clarissa, had exactly 25 overdue % books between them." sum(Returned) #= 25, % "The student of French has three more overdue books than Arthur," % Returned[Major[French]] #= Returned[Arthur] + 3, ReturnedArthur3 #= Returned[Arthur] + 3, element(Major[French],Returned,ReturnedArthur3), Major[French] #!= Arthur, % "and Clarissa has two more overdue books than the person majoring in English." % Returned[Clarissa] #= Returned[Major[English]] + 2, element(Major[English],Returned,ReturnedMajorEnglish), Returned[Clarissa] #= ReturnedMajorEnglish + 2, Major[English] #!= Clarissa, % for easier presentation assignment(Major,MajorInv), Vars = Major ++ Returned ++ MajorInv, solve(Vars), println(major=Major), println(majorInv=MajorInv), println(returned=Returned), nl, foreach(I in 1..N) printf("%w majors in %w and returned %d books\n",PeopleS[I],LangS[MajorInv[I]],Returned[I]) end, nl, fail, nl. go => true.