Not logged in. Login

A4Soln

Prolog Solutions

Note that these solutions will not be covered on the final exam.

Puzzle 1

students([fariba, qin, mary, paul]).

better_result(X, Y, [X, Y, _, _]).
better_result(X, Y, [X, _, Y, _]).
better_result(X, Y, [X, _, _, Y]).
better_result(X, Y, [_, X, Y, _]).
better_result(X, Y, [_, X, _, Y]).
better_result(X, Y, [_, _, X, Y]).

first([X, _, _, _], X).
third([_, _, X, _], X).

race_results(BikeResults, SwimResults) :-
    students(S),
    permutation(S, BikeResults),
    permutation(S, SwimResults),
    better_result(_, fariba, SwimResults),
    better_result(_, fariba, BikeResults),
    better_result(fariba, mary, SwimResults),
    better_result(mary, fariba, BikeResults),
    first(SwimResults, X),
    third(BikeResults, X),
    better_result(fariba, mary, SwimResults),
    better_result(mary, fariba, BikeResults),
    better_result(mary, _, SwimResults),
    better_result(mary, _, BikeResults),
    first(BikeResults, paul),
    better_result(qin, paul, SwimResults).

Using SWI-prolog:

?- race_results(Bike, Swim).
Bike = [paul, mary, qin, fariba],
Swim = [qin, fariba, mary, paul] .

Puzzle 2

lunch_items(fruit, [apple, orange, banana, grapes]).
lunch_items(sandwich, [bologna, turkey, ham_cheese, peanut_butter]).
lunch_items(dessert, [brownie, cupcake, cookie, candy_bar]).

children([may, faye, jay, ray]).

solution_structure([ate(may, brought(F1child, F1), brought(S1child, S1), brought(D1child, D1)),
                    ate(faye, brought(F2child, F2), brought(S2child, S2), brought(D2child, D2)),
                    ate(jay, brought(F3child, F3), brought(S3child, S3), brought(D3child, D3)),
                    ate(ray, brought(F4child, F4), brought(S4child, S4), brought(D4child, D4))]) :-
    lunch_items(fruit, F),
    permutation(F, [F1, F2, F3, F4]),
    lunch_items(sandwich, S),
    permutation(S, [S1, S2, S3, S4]),
    lunch_items(dessert, D),
    permutation(D, [D1, D2, D3, D4]),
    permutation([faye, jay, ray], [F1child, S1child, D1child]),
    permutation([may, jay, ray], [F2child, S2child, D2child]),
    permutation([may, faye, ray], [F3child, S3child, D3child]),
    permutation([may, faye, jay], [F4child, S4child, D4child]),
    children(C),
    permutation(C, [F1child, F2child, F3child, F4child]),
    permutation(C, [S1child, S2child, S3child, S4child]),
    permutation(C, [D1child, D2child, D3child, D4child]).

swapped_like_items([A1, A2, A3, A4]) :-
    swapped_like_items2(A1, [A2, A3, A4]),
    swapped_like_items2(A2, [A1, A3, A4]),
    swapped_like_items2(A3, [A1, A2, A4]),
    swapped_like_items2(A4, [A1, A2, A3]).

swapped_like_items2(ate(C0, brought(C1, _), brought(C2, _), brought(C3, _)), Others) :-
    member(ate(C1, brought(C0, _), _, _), Others),
    member(ate(C2, _, brought(C0, _), _), Others),
    member(ate(C3, _, _, brought(C0, _)), Others).


% The child who ate the apple also gobbled up the chocolate cupcake.
rule1(Eaten) :- 
    member(ate(_, brought(_, apple), _, brought(_, cupcake)), Eaten).

% Jay traded his fruit for Ray's orange.
rule2(Eaten) :- 
    member(ate(jay, brought(ray, orange), _, _), Eaten).

% One child brought a bologna sandwich and a banana to school.
rule3(Eaten) :- 
    member(ate(_, _, brought(C, bologna), _), Eaten),
    member(ate(_, brought(C, banana), _, _), Eaten).

% The child who ate the turkey sandwich also ate the grapes.
rule4(Eaten) :- 
    member(ate(_, brought(_, grapes), brought(_, turkey), _), Eaten).

% The child who ate the ham cheese sandwich traded away the cookie Mom had packed.
rule5(Eaten) :- 
    member(ate(C, _, brought(_, ham_cheese), _), Eaten),
    member(ate(_, _, _, brought(C, cookie)), Eaten).

% Ray enjoyed Faye's peanut butter sandwich.
rule6(Eaten) :- 
    member(ate(ray, _, brought(faye, peanut_butter), _), Eaten).

% The child who ate Jay's sandwich also ate the candy bar.
rule7(Eaten) :- 
    member(ate(_, _, brought(jay, _), brought(_, candy_bar)), Eaten).

eaten(X) :-
    solution_structure(X),
    rule1(X),
    rule2(X),
    rule3(X),
    rule4(X),
    rule5(X),
    rule6(X),
    rule7(X),
    swapped_like_items(X).

Using SWI-prolog:

?- eaten(X).         
X = [ate(may, brought(faye, grapes), brought(jay, turkey), brought(ray, candy_bar)), 
     ate(faye, brought(may, banana), brought(ray, ham_cheese), brought(jay, brownie)), 
     ate(jay, brought(ray, orange), brought(may, bologna), brought(faye, cookie)), 
     ate(ray, brought(jay, apple), brought(faye, peanut_butter), brought(may, cupcake))] .

Puzzle 3

woman(emily).
woman(hannah).
woman(iris).
man(gregory).
man(donald).

surnames([didduck, lombardi, purpuri, swaboda, wong]).
surname(X) :- surnames(S), member(X, S).

subjects([sunset, tugboat, horse, shrub, lighthouse]).
subject(X) :- subjects(S), member(X, S).

solution_structure([photographer(emily, EmilySurname, EmilySubject), 
                    photographer(hannah, HannahSurname, HannahSubject), 
                    photographer(iris, IrisSurname, IrisSubject),
                    photographer(donald, DonaldSurname, DonaldSubject),
                    photographer(gregory, GregorySurname, GregorySubject)]) :-
    surnames(S),
    permutation(S, [EmilySurname, HannahSurname, IrisSurname, DonaldSurname, GregorySurname]),
    subjects(T),
    permutation(T, [EmilySubject, HannahSubject, IrisSubject, DonaldSubject, GregorySubject]).

%   The one surnamed Didduck (who isn't Iris) photographed a tugboat.
rule1(Photographers) :- 
    member(photographer(X, didduck, tugboat), Photographers), 
    member(X, [emily, hannah, donald, gregory]).

%   One woman (who isn't surnamed Purpuri) took a photograph of a breathtaking sunset.
rule2(Photographers) :- 
    member(photographer(X, Y, sunset), Photographers), 
    woman(X),
    surname(Y),
    Y \== purpuri.

%   The one surnamed Lombardi (who isn't Hannah or Iris) used a fast shutter speed to capture a shot of a young horse at play.
rule3(Photographers) :-
    member(photographer(X, lombardi, horse), Photographers), 
    X \== hannah,
    X \== iris.
 
%   Gregory Swaboda took his photo with the camera he received for his birthday.
rule4(Photographers) :-
    member(photographer(gregory, swaboda, _), Photographers).

%   Neither Iris (who didn't take the picture of the flowering shrub) nor Hannah took a photograph of the sunset. 
rule5(Photographers) :-
    member(photographer(iris, _, X), Photographers),
    X \== shrub,
    member(photographer(Y, _, sunset), Photographers),
    Y \== iris,
    Y \== hannah.

photographers(X) :-
    solution_structure(X), rule1(X), rule2(X), rule3(X), rule4(X), rule5(X).

Using SWI-prolog:

?- photographers(X).
X = [photographer(emily, wong, sunset), 
     photographer(hannah, didduck, tugboat), 
     photographer(iris, purpuri, lighthouse), 
     photographer(donald, lombardi, horse), 
     photographer(gregory, swaboda, shrub)] .
Updated Thu Dec. 06 2018, 15:05 by cameron.