Home > Artificial Intelligence > Solving Logic Puzzles in Prolog: Puzzle 2 of 3

## Solving Logic Puzzles in Prolog: Puzzle 2 of 3

This post is about solving the 2nd of 3 small logic puzzles in Prolog. The previous post is available here.

## The puzzle

Eilen, Ada, Verena and Jenny participated in a painting competition. Find out who painted which subject and who took which place in the competition, using the hints provided. Hints:

1. Eilen painted a constable and was not last in the competition
2. Jenny took the third place
3. The person painting the Monet took the first place
4. Ada beat the person painting a talyor, and the person painting a Van Gogh beat Vera

## Solving the puzzle in Prolog

We just translate the hints into rules that ensure that a) places 1-4 and all mentioned painting subjects exist in our solution and b) the mentioned hints are fulfilled:

```% ####################################################################
% Logic puzzle solver.
%
% ['puzzle.pl'].
%
%
% Rainhard Findling
% 10/2015
% ####################################################################
all_members([H],L2) :- member(H,L2).
all_members([H|T],L2) :- member(H,L2), all_members(T, L2).

% 4 members
All = [Eilen, Ada, Vera, Jenny],
Eilen = [Eilen_place, Eilen_subject],
Vera = [Vera_place, Vera_subject],
Jenny = [Jenny_place, Jenny_subject],
% we know max place is 4th
% we know existing paintings
% #3 1st was monet
member([1, monet], All),
% #2 Jenny got third
member([3, Jenny_subject], All),
% #1 Eilen painted a constable and was not last
member([Eilen_place, constable], All),
4 > Eilen_place,
% #4 ada beat the taylor painter...
member([H2_taylor_place, taylor], All),
% #4 ...and the van gogh painter beat vera member([H4_fangogh_place, fangogh], All), member([Vera_place, Vera_subject], All), Vera_place > H4_fangogh_place.
```

If we ask for a solution, the single valid solution is presented:

```?- solve(Eilen, Ada, Vera, Jenny).
Eilen = [2, constable],