Lunch Puzzle
The Puzzle
- Dell- Math Puzzles Logic Problems, Jan 2002, p 36.
Kids are notorious for trading their lunches away, and the kids at PT Elementary are no exception. Mrs. Day, the lunch lady recently watched four big-time traders at work, swapping everything their parents had packed. She had trouble keeping it all straight, but shrugged when she realized that May and her friends each finished with a sandwich, piece of fruit, and a dessert (one was a brownie). Can you untangle the " insider trading" and determine what each child ate and who brought it to school?
- Each child swapped with each friend exactly once, always trading like items.
- The child who ate the apple also gobbled up the chocolate cupcake.
- Jay traded his fruit for Ray's orange.
- One child bought a bologna sandwich and a banana to school.
- The child who ate the turkey sandwich also ate the grapes.
- The child who ate the ham cheese sandwich traded away the cookie Mom had packed.
- Ray enjoyed Faye's peanut butter sandwich.
- The child who ate Jay's sandwich also ate the candy bar.
Prolog Solution
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(_, F1), brought(_, S1), brought(_, D1)),
ate(faye, brought(_, F2), brought(_, S2), brought(_, D2)),
ate(jay, brought(_, F3), brought(_, S3), brought(_, D3)),
ate(ray, brought(_, F4), brought(_, S4), brought(_, 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]).
swapped_with_each_child([]).
swapped_with_each_child([ate(C0, brought(C1, _), brought(C2, _), brought(C3, _)) | Others]) :-
children(C),
permutation(C, [C0, C1, C2, C3]),
swapped_with_each_child(Others).
swapped_like_items([], _).
swapped_like_items([ate(C0, brought(C1, _), brought(C2, _), brought(C3, _)) | Others], AllEaten) :-
member(ate(C1, brought(C0, _), _, _), All_Eaten),
member(ate(C2, _, brought(C0, _), _), All_Eaten),
member(ate(C3, _, _, brought(C0, _)), All_Eaten),
swapped_like_items(Others, AllEaten).
% 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_with_each_child(X),
swapped_like_items(X, X).
Demo
?- ['lunchpuzzle.pro'].
true.
?- eaten(X).
X = [ate(may, brought(faye, banana), brought(jay, ham_cheese), brought(ray, brownie)),
ate(faye, brought(may, grapes), brought(jay, turkey), brought(ray, candy_bar)),
ate(jay, brought(ray, orange), brought(faye, bologna), brought(may, cookie)),
ate(ray, brought(may, apple), brought(faye, peanut_butter), brought(jay, cupcake))] .
Updated Tue April 02 2019, 09:07 by cameron.