/*-------------------------------------- File: chores1.ecl ------------------------------------- Title: Solving Logic Puzzles using Artificial Intelligence Author: Leon J. Mekly, Ph.D. Modified to use element-constraints 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(ic_symbolic). :- local domain(day(mon,tue,wed,thu,fri)). :- local domain(chore(milk,market,corn,fences,eggs)). :- local domain(time('5:00am','6:00am','7:00am','8:00am','9:00am')). go :- N = 5, [Adam, Carl, Fran, Nan, Mike] = [1, 2, 3, 4, 5], Names = ['Adam', 'Carl', 'Fran', 'Nan ', 'Mike'], length(Chores, N), Chores &:: chore, alldifferent(Chores), length(Days, N), Days &:: day, alldifferent(Days), length(Times, N), Times &:: time, alldifferent(Times), % 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. element(Fran, Chores, fences), element(Fran, Days, FranDay), 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. element(MarketIndex, Chores, market), element(MarketIndex, Days, MarketDay), element(Carl, Days, CarlDay), element(MarketIndex, Times, MarketTime), element(Nan, Times, NanTime), element(Fran, Times, FranTime), MarketDay &> CarlDay, MarketTime &< NanTime, MarketTime &> 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. element(Adam, Chores, AdamChore), AdamChore &\= corn, AdamChore &\= market, element(Adam, Days, mon), element(Adam, Times, AdamTime), AdamTime &< '9:00am', AdamTime &> '5:00am', % 4. No one performs a chore on Thursday at 7:00 am. element(ThuIndex, Days, thu), element(ThuIndex, Times, ThuTime), ThuTime &\= '7:00am', % 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. element(Mike, Times, MikeTime), element(Mike, Days, MikeDay), MikeTime &< '7:00am', MikeDay &> wed, % 6. One of the boys collects eggs, another boy milks Bessie, the goat. element(EggIndex, Chores, eggs), element(MilkIndex, Chores, milk), % 7. The milk is collected earlier in the week than the girl who plants % the corn, which is not planted before Wednesday. element(Nan, Chores, corn), element(Nan, Days, CornDay), element(MilkIndex, Days, MilkDay), MilkDay &< CornDay, CornDay &> tue, % 8. The eggs must be collected at 7:00 am, but never on Mondays or Fridays. element(EggIndex, Times, EggsTime), element(EggIndex, Days, EggsDay), EggsTime &= '7:00am', EggsDay &\= mon, EggsDay &\= fri, % 9. All the other chores must be complete before Mike goes to the market. element(Mike, Chores, market), element(Mike, Days, fri), % Print results ( foreach(Name, Names), foreach(Day, Days), foreach(Time, Times), foreach(Chore, Chores) do writeln([Name, Day, Time, Chore]) ).