I'm reading the "7 Languages in 7 Days"-book, and have reached the Prolog chapter. As a learning exercises I'm trying to solve some textual logic puzzles. The puzzle goes as follow:
Five sisters all have their birthday in a different month and each on a different day of the week. Using the clues below, determine the month and day of the week each sister's birthday falls.
- Paula was born in March but not on Saturday. Abigail's birthday was not on Friday or Wednesday.
- The girl whose birthday is on Monday was born earlier in the year than Brenda and Mary.
- Tara wasn't born in February and her birthday was on the weekend.
- Mary was not born in December nor was her birthday on a weekday. The girl whose birthday was in June was born on Sunday.
- Tara was born before Brenda, whose birthday wasn't on Friday. Mary wasn't born in July.
My current implementation probably looks like a joke to experienced Prolog programmers. The code is pasted below.
I would love some input on how to solve the question, and how to make the code both clear and dense.
Ie:
- How can I avoid typing out the limitations saying that the Days must be unique.
- How can I avoid typing out the limitations saying that the Months must be unique.
- Add the limitation about the ordering of the birthdays.
is_day(Day) :-
member(Day, [sunday, monday, wednesday, friday, saturday]).
is_month(Month) :-
member(Month, [february, march, june, july, december]).
solve(S) :-
S = [[Name1, Month1, Day1],
[Name2, Month2, Day2],
[Name3, Month3, Day3],
[Name4, Month4, Day4],
[Name5, Month5, Day5]],
% Five girls; Abigail, Brenda, Mary, Paula, Tara
Name1 = abigail,
Name2 = brenda,
Name3 = mary,
Name4 = paula,
Name5 = tara,
is_day(Day1), is_day(Day2), is_day(Day3), is_day(Day4), is_day(Day5),
Day1 \== Day2, Day1 \== Day3, Day1 \== Day4, Day1 \== Day5,
Day2 \== Day1, Day2 \== Day3, Day2 \== Day4, Day2 \== Day5,
Day3 \== Day1, Day3 \== Day2, Day3 \== Day4, Day3 \== Day5,
Day4 \== Day1, Day4 \== Day2, Day4 \== Day3, Day4 \== Day5,
is_month(Month1), is_month(Month2), is_month(Month3), is_month(Month4), is_month(Month5),
Month1 \== Month2, Month1 \== Month3, Month1 \== Month4, Month1 \== Month5,
Month2 \== Month1, Month2 \== Month3, Month2 \== Month4, Month2 \== Month5,
Month3 \== Month1, Month3 \== Month2, Month3 \== Month4, Month3 \== Month5,
Month4 \== Month1, Month4 \== Month2, Month4 \== Month3, Month4 \== Month5,
% Paula was born in March but not on Saturday.
member([paula, march, _], S),
Day4 \== sunday,
% Abigail's birthday was not on Friday or Wednesday.
Day1 \== friday,
Day1 \== wednesday,
% The girl whose birthday is on Monday was born
% earlier in the year than Brenda and Mary.
% Tara wasn't born in February, and
% her birthday was on the weekend.
Month5 \== february,
Day5 \== monday, Day5 \== wednesday, Day5 \== friday,
% Mary was not born in December nor was her
% birthday on a weekday.
Month3 \== december,
Day3 \== monday, Day3 \== wednesday, Day3 \== friday,
% The girl whose birthday was in June was
% born on Sunday.
member([_, june, sunday], S),
% Tara was born before Brenda, whose birthday
% wasn't on Friday.
Day2 \== friday,
% Mary wasn't born in July.
Month3 \== july.
Update Based on the answer from chac I was able to solve the puzzle. Following the same recipe we (the programming language competency group at work) was able to solve a second puzzle as well. I have posted the complemete implementation, and example output as a gist on GitHub.