/*
   A puzzle found on the web, attributed to Robin Snyder:

1. There are 3 boys and 3 girls.  Each boy dances with a different girl.

2. One boy is dressed in red, one in green, one in blue.

3. One girl is dressed in red, one in green, one in blue.

4. The boy in red dances with the girl in green.

5. No boy dances with a girl who was dressed in the same color as he.

6. Which boy danced with the girl dressed in red?

*/

/*
 * Solution:
 *
 * A configuration of who dances with whom will be represented in the form:
 *    [[red, Girl1], [green, Girl2], [blue, Girl3]]
 */

solve(Configuration) :-

  [[red, Girl1], [green, Girl2], [blue, Girl3]] = Configuration,  % clues 1 and 2

  perm([red, green, blue], [Girl1, Girl2, Girl3]),		  % clues 1 and 3

  member([red, green], Configuration),				  % clue 4

  \+ member([X, X], Configuration),				  % clue 5

  true.		% to permit ease in fiddling with constraints above.


boyWhoDancesWithGirlInRed(Boy) :-

  solve(Configuration), 
  member([Boy, red], Configuration).


/* member(A, L) is true iff A is a member of L. */

member(A, [A | _]).
member(A, [_ | X]) :- member(A, X).


/* perm(L, M) is true iff list M is a permutation of list L. */

perm([], []).
perm(L, [A | M]) :-
    remove(A, L, R),
    perm(R, M).


/* 
 * remove(A, L, M) is true iff A is a member of list L and M is the
 * residue when A is removed from L.
 */

remove(A, [A | L], L).
remove(A, [B | L], [B | M]) :- remove(A, L, M).


:- nl,
   write('The boy in '),
   boyWhoDancesWithGirlInRed(X),
   write(X),
   write(' dances with the girl in red.'),
   nl,
   nl.


   