/*-------------------------------------- File: chores1.ecl ------------------------------------- Title: Solving Logic Puzzles using Artificial Intelligence Author: Leon J. Mekly, Ph.D. Modified to use pure Prolog with backtracking by Joachim Schimpf Knowledge representation language: ECLiPSe CLP at https://eclipseclp.org Example: "At the farm" - rated HARD https://www.ahapuzzles.com/logic/logic-puzzles/pdf/at-the-farm.pdf Five siblings (one is Fran) live on a farm. Each must perform a chore (including milking a cow) on a specic day of the week (Mon-Fri). If each of them performs a certain task on a given day and at a specic time (either 5:00 am, 6:00 am, 7:00 am, 8:00 am, 9:00 am), from the clues provided below can you determine which sibling performed which task at which time on which day? ----------------------------------------------------------------------------------------------------*/ :- lib(listut). % for perm/2 list permutation go :- Farm = [ ['Adam', AdamDay, AdamTime, AdamChore], ['Carl', CarlDay, CarlTime, CarlChore], ['Fran', FranDay, FranTime, FranChore], ['Nan ', NanDay , NanTime , NanChore ], ['Mike', MikeDay, MikeTime, MikeChore] ], Days = [AdamDay,CarlDay,FranDay,NanDay,MikeDay], [Mon,Tue,Wed,Thu,Fri] = [1,2,3,4,5], perm(Days, [Mon,Tue,Wed,Thu,Fri]), Chores = [AdamChore,CarlChore,FranChore,NanChore,MikeChore], perm(Chores, [milk,market,corn,fences,eggs]), Times = [AdamTime,CarlTime,FranTime,NanTime,MikeTime], perm(Times, [5,6,7,8,9]), % 1. Fran's day to mend fences is neither Tuesday nor Thursday, and is % completed earlier in the week than at least one other person. member(['Fran',FranDay,_,fences],Farm), FranDay \= Tue, FranDay \= Thu, FranDay < Fri, % 2. The one who goes to market goes later in the week than Carl, and % earlier in the morning than Nan, but later in the morning than Fran. member(['Mike',MikeDay,MikeTime,market],Farm), % Mike is the one in clue 9 MikeDay > CarlDay, MikeTime < NanTime, MikeTime > FranTime, % 3. Adam does not plant the corn, or go to market, his chore is completed % before 9:00 am, but not before 6:00 am on his assigned day of Monday. AdamChore \= corn, AdamChore \= market, AdamTime < 9, AdamTime >= 6, AdamDay = Mon, % 4. No one performs a chore on Thursday at 7:00 am. member([_,Thu,ThuTime,_],Farm), ThuTime \= 7, % 5. Mike's chore is completed earlier in the day than at least 3 others, % but later in the week than at least three others. MikeTime < 7, MikeDay > Wed, % 6. One of the boys collects eggs, another boy milks Bessie, the goat. member([_,EggsDay,EggsTime,eggs],Farm), member([_,MilkDay,_,milk],Farm), % 7. The milk is collected earlier in the week than the girl who plants % the corn, which is not planted before Wednesday. member(['Nan ',CornDay,_,corn],Farm), % Nan is the girl, Fran mends fences MilkDay < CornDay, CornDay > Tue, % 8. The eggs must be collected at 7:00 am, but never on Mondays or Fridays. EggsTime = 7, EggsDay \= Mon, EggsDay \= Fri, % 9. All the other chores must be complete before Mike goes to the market. MikeChore = market, MikeDay = Fri, nl, (foreach(Sibling,Farm) do writeln(Sibling)). %-------------------------------------- EOF: chores1.ecl -----------------------------