I'm trying to solve the following logic puzzle using Prolog:
Determine the first and last name of each driver in the race, the sponsor for each race car, and in what position each driver ended the race.
- The driver sponsored by Flash Automotive finished the race in third place. Howard, who wasn’t sponsored by Crank Motor Oil, didn’t finish the race in fifth place.
- Ryan, whose last name wasn’t Right, wasn’t sponsored by NAPA Auto Parts. Barry Straight wasn’t sponsored by Fleet Bodyworks.
- The driver sponsored by Crank Motor Oil placed one position higher than Sydney, whose last name wasn’t Element.
- Mr. Chariot, whose first name wasn’t Adam, finished in first place. Howard placed one position lower than Mr. Right.
- NAPA Auto Parts sponsored the driver who finished in fourth place, which wasn’t Barry.
- The five drivers, in no particular order, are Adam, the second place driver, the driver sponsored by Tredco Tires, Mr. Right, and Mr. Rafe.
Using a similar approach as the one found here: http://www.anselm.edu/internet/compsci/faculty_staff/mmalita/HOMEPAGE/logic/aa6.txt
I came up with the following answer:
race(Drivers) :- Drivers = [[howard,_,_,_],[barry,_,_,_],[ryan,_,_,_],
[sydney,_,_,_],[adam,_,_,_],[_,straight,_,_],[_,chariot,_,_],
[_,right,_,_],[_,element,_,_],[_,rafe,_,_],[_,_,flashautomotive,_],
[_,_,crankmotoroil,_],[_,_,napaautoparts,_],[_,_,fleetbodyworks,_],
[_,_,tredcotires,_],[_,_,_,1],[_,_,_,2],[_,_,_,3],[_,_,_,4],[_,_,_,5]],
member([barry,straight,C,D], Drivers), C \= fleetbodyworks,
C \= napaautoparts, D \= 4,
member([howard,_,G,H], Drivers), G \= crankmotoroil, H \= 5, H =:= X - 1,
member([ryan,J,K,_], Drivers), J \= right, K \= napaautoparts,
member([sydney,N,_,P], Drivers), N \= element,
member([Q,chariot,_,T], Drivers), Q \= adam, T = 1,
member([_,right,_,X], Drivers),
member([_, _, _, crankmotoroil, DD], Drivers), DD =:= P + 1,
member([EE,_,napaautoparts,HH], Drivers), HH = 4, EE \= barry,
member([_,_,flashautomotive,LL], Drivers), LL = 3.
When loading this into the Windows SWI-Prolog IDE, it gives no errors, but while debugging it produces this result:
[debug] [1] 12 ?- race(X)
| .
T Call: (13) race(_G4168)
T Fail: (13) race(_G4168)
false.
I'm obviously quite new to Prolog, but I'm wondering what might be causing this to fail and how to fix it (because the debugger as I understand how to use it is not very helpful in this instance). Also, is there a cleaner way to solve similar logic problems?
(\=)/2
! – repeat