Luke
Luke

Reputation: 37

Advice on solving zebra type puzzle

I need your help in solving the following problem:

There're 3 girls (Ann,Susan,Alice) who need to choose what color shoes and dress to wear. There're 3 possible colors for shoes and dresses: white, blue and green.

Main conditions:

My code satisfies only 2 conditions; I'm kinda having hard times meeting conditions on the same color for Susan and whereas other girls need to have different color garments.

Here's what I come up with:

PREDICATES
   girl(symbol)
   shoes(symbol,symbol)
   skirt(symbol,symbol)
   hates(symbol,symbol)
   will_wear(symbol, symbol, symbol)


CLAUSES
   will_wear(X,Y,Z):-
      girl(X),
      shoes(X,Y),
      skirt(X,Z),
      not(hates(X,Y)),
      not(hates(X,Z)).

   girl(ann).
   girl(susan).
   girl(alice).

   hates(ann,white).

   skirt(_,white).
   skirt(_,blue).
   skirt(_,green).

   shoes(alice,white).
   shoes(_,blue).
   shoes(_,green).

GOAL
   will_wear(Name,Shoes,Dress).

Code above works fine, but gives too many solutions. Plus, I couldn't come up with any logical solution for the condition for Susan to wear shoes and dress of same color.

Thanks.

Upvotes: 0

Views: 317

Answers (2)

configurator
configurator

Reputation: 41630

If I understand the conditions correctly, they're not what Shurane answered.

This will make sure that a girl wears a dress and shoes with the same colour:

same_color(Girl) :-
    shoes(Girl, Color),
    dress(Girl, Color).

I'll leave the different colour one as an exercise, but hint that to say two things are not the same you say A \= B. Please leave a comment if you have a hard time with different_color - and tell me what you've tried.

Upvotes: 1

Ehtesh Choudhury
Ehtesh Choudhury

Reputation: 7790

Off the top of my head, I'm thinking something along these lines:

only_wears(Girl,Color):-
    shoes(Girl, Color),
    skirt(Girl, Color).

different_shoes(F, S):-
    shoes(F,F_color),
    shoes(S,S_color),
    not(equals(F_color,S_color)).

different_skirts(F, S):-
    skirt(F,F_color),
    skirt(S,S_color),
    not(equals(F_color,S_color)).

I do wonder if there's a way to pass clauses to other clauses, because different_shoes and different_skirts are identical in structure.

You would initialize it like so:

only_wears(ann, white).
different_shoes(alice, ann).
different_skirt(alice, ann).

Upvotes: 1

Related Questions