According to an e-mail source, this puzzle originated with the German Institute of Logical Thinking, Berlin, 1981. [It's possible.]
Try solving this puzzle on your own.
Now imagine writing a computer program to solve it.
Which was more difficult?
This is an example of a completely specified solution which doesn't appear to be specified at all. The constraints are such that the answer is unique, but they are stated in such a way that it is not at all obvious (to this human, at least) what the answer is.
In Prolog, we could express each of these specifications, then let Prolog's search strategy (database search engine, automated theorem prover) search for a solution. We don't have to worry about how to solve the problem -- we only have the specify what is to be solved.
Part of the problem is encoded in the data structure used to represent it: a list consisting of five sublists, each representing the five attributes of a house and its resident (Color, Nationality, Pet, Drink, Smoke), where the order of the sublists corresponds to the order of the houses from left to right:
S = [[C1,N1,P1,D1,S1], [C2,N2,P2,D2,S2], [C3,N3,P3,D3,S3], [C4,N4,P4,D4,S4], [C5,N5,P5,D5,S5]],
Each fact is then encoded by using a constant symbol to directly represent the attributes of some house/resident (or pair of houses/residents). Sometimes we use the predefined member predicate to assert that the pattern given must correspond to one of the sublists of the solution list; other times we use next_to (defined in the sample solution) to assert a relationship between two houses. The uniqueness conditions (two through six above) turn out not to be necessary, since the puzzle is so constrained, but one wouldn't know that in advance.
This solution uses five separate lists, one each for colors, nationalities, pets, drinks, and smokes. It uses predicates to define the relationships between the houses: in the same house, or in adjacent houses (either ordered from left to right or unordered).
A Poor (slow) Solution
This solution indicates how not to solve a puzzle like this in Prolog. It uses a generate-and-test strategy, by which all the possible permutations of lists of colors, nationalities, pets, drinks, and smokes are generated, and these are then tested against the constraints of the puzzle to see if they are consistent with them. This is a very slow way to solve the problem (unless you cheat by putting one or more of the lists into an order that makes the solution easy to find). Study the first and second solutions to see how they avoid the exponential cost of this slow solution.Copyright © 2002, 2004 Jonathan Mohr