Solve logic puzzle in Prolog Coding Help

41 views
Skip to first unread message

Lisa Huang

unread,
Apr 28, 2015, 5:22:10 PM4/28/15
to swi-p...@googlegroups.com
So I have to solve this logic puzzle using Prolog:

With spring in the air, everyone's starting to think about spring clean-up so Jeff spent all week after school and all weekend doing odd jobs around the neighborhood to earn some spending money. He tilled gardens, raked leaves, cleaned up tree limbs, pruned hedges, and even spent one afternoon babysitting! Each day he worked for a different neighbor on a different chore. He charged each neighbor a reasonable rate for his work, but sometimes he got tips too. By the end of the week, he was quite pleased with the amount that he'd earned and each of the neighbors was pleased with the quality of work that he'd done. Determine the last name of each neighbor, what chore Jeff completed for each neighbor, what day of the week Jeff worked for each neighbor, how long each task took him (2 to 4 hours), and how much he earned for each task ($20 to $36).

1. Jeff helped Mrs. Agnew till her garden but it didn't take him 4 hours. Mr. Summer didn't need help on Thursday.

2. He spent an extra hour working on Thursday than he did on Wednesday but he wasn't pruning hedges on either day.

3. On Sunday, he earned $8 more than he did when the day he was babysitting, which earned him $4 more than he earned from Mr. Compton.

4. Jeff made $20 from the job he spent 2 hours doing, which wasn’t removing tree limbs.

5. Mr. Numen, who didn't ask him to rake leaves, paid him $32 but it didn't take him 3 hours. The job on Friday for Mrs. Fields took less time than the garden tilling job.

6. The hedge pruning job took him 3-1/2 hours to complete, a half hour longer than the job he did on Thursday. Jeff made $36 from the job he spent 4 hours doing, which was on Saturday.




I have already written out the code for it, but at the end when I run the program to solve for the 25 variables it takes a very long time to run and does not print out the solution correctly. I think the writeln() function does not work correctly for some reason. Any ideas on how to fix it?
Here's the code I have so far:



neighbor(agnew, compton, fields, numen, summer).
chore(garden_tilling, raking_leaves, treelimb_removal, hedge_pruning, babysitting).
day(wednesday, thursday, friday, saturday, sunday). 
duration(two, twoandhalf, three, threeandhalf, four). 
money(twenty, twenty-four, twenty-eight, thirty-two, thirty-six). 

rule1(N1, N2, D, C, T) :- 
(N1 = agnew,  
N2 = summer,
C = garden_tilling, 
D \== thursday,
T \== four) ;
(N1 \== agnew,
C \== garden_tilling, 
N2 = summer, 
D \== thursday).

rule2(D1, D2, T1, T2, C) :- 
((D1 = thursday, 
D2 = wednesday, 
C \== hedge_pruning), 
((T1 = three, T2 = two) ; (T1 = threeandhalf, T2 = twoandhalf) ; (T1 = four, T2 = three)));
((D1 = wednesday, 
D2 = thursday, 
C \== hedge_pruning),
((T1 = two, T2 = three) ; (T1 = twoandhalf, T2 = threeandhalf) ; (T1 = three, T2 = four))).

rule3(D, M1, M2, M3, C1, N) :-
(D = sunday,
C1 = babysitting,
((M2 = thirty-six, M1 = twenty-eight, M3 = twenty-four) ; (M2 = thirty-two, M1 = twenty-four, M3 = twenty)),
N = compton);
(D \== sunday,
C1 \== babysitting,
((M2 \== thirty-six, M1 \== twenty-eight, M3 \== twenty-four) ; (M2 \== thirty-two, M1 \== twenty-four, M3 \== twenty)),
N \== compton).

rule4(M, T, C) :-
(M = twenty, 
T = two,
C \== treelimb_removal);
(M \== twenty, 
T \== two).

rule5(N1, N2, C1, C2, M, T1, T2, T3, D) :-
((N1 = numen,
C1 \== raking_leaves,
M = thirty-two,
T1 \== three);
(N1 \== numen,
M \== thirty-two)),

((D = friday,
N2 = fields,
C2 = garden_tilling,
((T3 = four, (T2 = two; T2 = twoandhalf; T2 = three; T2 = threenadhalf));
(T3 = threeandhalf, (T2 = two; T2 = twoandhalf; T2 = three));
(T3 = three, (T2 = two; T2 = twoandhalf));
(T3 = twoandhalf, T2 = two)));
D \== friday, 
N2 \== fields, 
C2 \== garden_tilling,
((T3 \== four, (T2 \== two; T2 \== twoandhalf; T2 \== three; T2 \== threenadhalf));
(T3 \== threeandhalf, (T2 \== two; T2 \== twoandhalf; T2 \== three));
(T3 \== three, (T2 \== two; T2 \== twoandhalf));
(T3 \== twoandhalf, T2 \== two))).

rule6(C, T1, T2, D1, M, T3, D2) :-
((C = hedge_pruning,
D1 = thursday,
T1 = three,
T2 = threeandhalf);
(C \== hedge_pruning,
D1 \== thursday,
T1 \== three,
T2 \== threeandhalf)),
 
((M = thirty-six, 
T3 = four,
D2 = saturday);
(M \== thirty-six, 
T3 \== four,
D2 \== saturday)).

isday(D) :- 
D = wednesday; 
D = thursday;
D = friday;
D = saturday;
D = sunday.

ischore(C) :-
C = garden_tilling;
C = raking_leaves;
C = treelimb_removal;
C = hedge_pruning;
C = babysitting.

isneighbor(N) :-
N = agnew;
N = compton;
N = fields;
N = numen;
N = summer.

istime(T) :-
T = two;
T = twoandhalf;
T = three;
T = threeandhalf;
T = four.

ismoney(M) :-
M = twenty;
M = twenty-four;
M = twenty-eight;
M = thirty-two;
M = thirty-six.

solve(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y) :-
dif(A, B),
dif(A, C),
dif(A, D),
dif(A, E),
dif(B, C),
dif(B, D),
dif(B, E),
dif(C, D),
dif(C, E),
dif(D, E),

dif(F, G),
dif(F, H),
dif(F, I),
dif(F, J),
dif(G, H),
dif(G, I),
dif(G, J),
dif(H, I),
dif(H, J),
dif(I, J),


dif(K, L),
dif(K, M),
dif(K, N),
dif(K, O),
dif(L, M),
dif(L, N),
dif(L, O),
dif(M, N),
dif(M, O),
dif(N, O),

dif(P, Q),
dif(P, R),
dif(P, S),
dif(P, T),
dif(Q, R),
dif(Q, S),
dif(Q, T),
dif(R, S),
dif(R, T),
dif(S, T),

dif(U, V),
dif(U, W),
dif(U, X),
dif(U, Y),
dif(V, W),
dif(V, X),
dif(V, Y),
dif(W, X),
dif(W, Y),
dif(X, Y),

isday(A),
isday(B),
isday(C),
isday(D),
isday(E),
ischore(F),
ischore(G),
ischore(H),
ischore(I),
ischore(J),
isneighbor(K),
isneighbor(L),
isneighbor(M),
isneighbor(N),
isneighbor(O),
istime(P),
istime(Q),
istime(R),
istime(S),
istime(T),
ismoney(U),
ismoney(V),
ismoney(W),
ismoney(X),
ismoney(Y),

rule1(K,L,A,F,P), 
rule2(B,A,R,P,G),
rule3(C,V,X,U,H,M),
rule4(U,P,F),
rule5(N,O,J,F,X,P,Q,T,D),
rule6(G,S,R,A,Y,T,E),

writeln(''),
writeln(B),
writeln(M),
writeln(J),
writeln(P),
writeln(U),

writeln(A),
writeln(K),
writeln(F),
writeln(R),
writeln(W),

writeln(D),
writeln(O),
writeln(H),
writeln(Q),
writeln(V),

writeln(E),
writeln(L),
writeln(I),
writeln(S),
writeln(X),

writeln(C),
writeln(N),
writeln(G),
writeln(T),
writeln(Y).

Reply all
Reply to author
Forward
0 new messages