Arch friends puzzle
I can’t claim any credit for this example, but I was given this homework assignment many years at school at ASU to solve this puzzle using Answer Set Programing with clingo. After some consternation, I finally wrote nearing the same code and I got the same resulting answer as this example below. After we turned in our homework, my teacher gave us this solution file which is the code showing below.
Since there doesn’t seem to be a lot of content online about ASP, I thought I would share this content. Last I looked, I couldn’t find this online anymore, so I thought it was worth posting.
% Run by: clingo Solution_Assignment1.lp
%________________________________________________________________________
% Arch friends puzzle (Dell Logic Puzzles) in ASP.
%
% Problem formulation from
% http://brownbuffalo.sourceforge.net/ArchFriendsClues.html
% """
% Title: Arch Friends
% Author: Mark T. Zegarelli
% Publication: Dell Logic Puzzles
% Issue: April, 1998
% Page: 7
% Stars: 1
%
% Harriet, upon returning from the mall, is happily describing her four shoe
% purchases to her friend Aurora. Aurora just loves the four different kinds
% of shoes that Harriet bought (ecru espadrilles, fuchsia flats, purple pumps,
% and suede sandals), but Harriet can't recall at which different store (Foot
% Farm, Heels in a Handcart, The Shoe Palace, or Tootsies) she got each pair.
% Can you help these two figure out the order in which Harriet bought each
% pair of shoes, and where she bought each?
%
% 1. Harriet bought fuchsia flats at Heels in a Handcart.
% 2. The store she visited just after buying her purple pumps was not Tootsies.
% 3. The Foot Farm was Harriet's second stop.
% 4. Two stops after leaving The Shoe Place, Harriet bought her suede sandals.
%
% Determine: Order - Shoes - Store
% """
% Answer:
% 1, purple pumps, The Shoe Place
% 2, ecru espadrilles, Foot Farm
% 3, suede sandals, Tooties
% 4, fuchsia flats, Heels in a Handcart
% Declaration
% --------------------
shoes(ecru_espadrilles;fuchsia_flats;purple_pumps;suede_sandals).
store(foot_farm;heels_in_a_handcart;the_shoe_palace;tootsies).
order(1..4).
% Generation
% --------------------
% For each order, there is only one combination of buy(Order, Shoe, Store).
1 { buy(Order, Shoes, Store) : shoes(Shoes) : store(Store)} 1 :- order(Order).
% For each pair of shoes, there is only one combination of buy(Order, Shoes, Store).
1 { buy(Order, Shoes, Store) : order(Order) : store(Store)} 1 :- shoes(Shoes).
% For each shop, there is only one combination of buy(Order, Shoes, Store).
1 { buy(Order, Shoes, Store) : order(Order) : shoes(Shoes)} 1 :- store(Store).
% Elimination
% --------------------
% 1. Harriet bought fuchsia flats at Heels in a Handcart.
c1 :-
buy(Order, fuchsia_flats, heels_in_a_handcart),
order(Order).
:- not c1.
% 2. The store she visited just after buying her purple pumps was not Tootsies.
c2 :-
buy(Order1, purple_pumps, Store1),
buy(Order1+1, Shoes2, Store2),
tootsies != Store2,
order(Order1),
shoes(Shoes2),
store(Store1),
store(Store2).
:- not c2.
% 3. The Foot Farm was Harriet's second stop.
c3 :-
buy(2, Shoes, foot_farm),
shoes(Shoes).
:- not c3.
% 4. Two stops after leaving The Shoe Place, Harriet bought her suede sandals.
c4 :-
buy(Order1, Shoes1, the_shoe_palace),
buy(Order1+2, suede_sandals, Store2),
order(Order1),
shoes(Shoes1),
store(Store2).
:- not c4.
#hide.
#show buy(Order, Shoes, Store).
Using version 3.0.3 of clingo, it still says it is satisfiable and gives me the answer to the puzzle.
My hope and intention for posting this is to help anyone wanting to learn ASP to see another example of how to use this language. I hope you found it useful and I hope my teacher isn’t still using it as a starting example. ;)