I am trying to implement a program in clingo
that solves one of those classic riddles where you have a series of assertions of facts and constraints and you have to deduce other facts. Here goes the problem:
Five men of different nationalities live in five side-by-side houses, each of a different color; they all have different jobs, a different favourite animal and favourite beverages. We know that:
- The English man lives in the red house.
- The Spanish man's favourite animal is the dog.
- The Japanese man is a painter.
- The Italian man drinks tea.
- The Norwegian man lives in the first house from the left. (number_norw = 1)
- The person living in the green house drinks coffee.
- The green house is immediately right of the white one. (number_green = number_white + 1)
- The clerk loves cats.
- The salesman lives in the yellow house.
- Milk is the favourite drink in the center house. (number_milk = 3)
- The Norwegian's house is adjacent to the blue one. (number_norw = number_blue ± 1)
- The cook likes juice.
- The man living in the house next to the doctor's loves foxes.
- The man who loves horses lives next door to the salesman.
The assignment is to find out who likes zebras. So I set forth asserting:
% Number (the number of the house, 1 being the leftmost of the block, 5 the rightmost)
number(1..5).
% Color
color(red;green;white;yellow;blue).
% Nationality
nationality(english;spanish;japanese;italian;norwegian).
% Animal
animal(dog;cat;fox;horse;zebra).
% Job
job(painter;clerk;salesman;cook;doctor).
% Beverage
beverage(tea;coffee;milk;juice;coke).
% House
house(X, C, N, A, J, B) :-
number(X),
color(C),
nationality(N),
animal(A),
job(J),
beverage(B).
Now I'm stuck on asserting the constraints; how do I go about coding assertions 1. through 14.? I just need to understand the proper syntax, so if someone could please set me on the right track with one or two examples I can figure out the rest. Thanks.
N.B. Notice I could have inferred, from 5. and 11., that the second house is the blue one because 11. number_blue = number_norw ± 1
, 5. number_norw = 1
, and 0 is not in the range of possible numbers, but I don't want to manually add it to the constraints because I expect clingo
to figure it out itself.