Skip to content

Instantly share code, notes, and snippets.

@follesoe
Created September 4, 2012 16:08
Show Gist options
  • Save follesoe/3622877 to your computer and use it in GitHub Desktop.
Save follesoe/3622877 to your computer and use it in GitHub Desktop.
Puzzles solved in Prolog

Five sisters

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.

  1. Paula was born in March but not on Saturday. Abigail's birthday was not on Friday or Wednesday.
  2. The girl whose birthday is on Monday was born earlier in the year than Brenda and Mary.
  3. Tara wasn't born in February and her birthday was on the weekend.
  4. Mary was not born in December nor was her birthday on a weekday. The girl whose birthday was in June was born on Sunday.
  5. Tara was born before Brenda, whose birthday wasn't on Friday. Mary wasn't born in July.

Puzzle taken from puzzelersparadise.com.

before_in_year(X, Y, Months) :-
nth1(Xi, Months, X),
nth1(Yi, Months, Y),
Xi < Yi.
print_solution([Head|Tail]) :-
write(Head),
nl,
print_solution(Tail).
solution(S) :-
S = [[Name1, Month1, Day1],
[Name2, Month2, Day2],
[Name3, Month3, Day3],
[Name4, Month4, Day4],
[Name5, Month5, Day5]],
% Five sisters; Abigail, Brenda, Mary, Paula, Tara
Girls = [abigail, brenda, mary, paula, tara],
Girls = [Name1, Name2, Name3, Name4, Name5],
% All have their birthday in a different month
Months =[february, march, june, july, december],
permutation(Months, [Month1, Month2, Month3, Month4, Month5]),
% and each of a different day of the week.
Days = [sunday, monday, wednesday, friday, saturday],
permutation(Days, [Day1, Day2, Day3, Day4, Day5]),
% Paula was born in March but not on Saturday.
member([paula, march, Day4], S), Day4 \= saturday,
% 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.
member([_, C1, monday], S),
member([brenda, Month2, Day2], S), before_in_year(C1, Month2, Months),
member([mary, Month3, _], S), before_in_year(C1, Month3, Months),
% Tara wasn't born in February, and
% her birthday was on the weekend.
member([tara, Month5, Day5], S), Month5 \= february, (Day5 = saturday ; Day5 = sunday),
% Mary was not born in December nor was her
% birthday on a weekday.
member([mary, Month3, Day3], S), Month3 \= december, (Day3 = saturday ; Day3 = sunday),
% 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.
member([brenda, Month2, Day2], S), Day2 \= friday,
member([tara, Month5, _], S), before_in_year(Month5, Month2, Months),
Day2 \== friday,
% Mary wasn't born in July.
Month3 \== july,
print_solution(S).

Executing the Prolog program solves the puzzle with following answer:

  • Abigail is born on a Monday in February,
  • Brenda is born on a Wednesday in December,
  • Mary is born on a Sunday in June,
  • Paula is born on a Friday in March,
  • Tara is born on a Saturday in July
$ gprolog  --consult-file birthday.pl --query-goal "solution(S)"
GNU Prolog 1.4.1
By Daniel Diaz
Copyright (C) 1999-2012 Daniel Diaz
compiling birthday.pl for byte code...
birthday.pl compiled, 64 lines read - 7810 bytes written, 19 ms
| ?- solution(S).

[abigail,february,monday]
[brenda,december,wednesday]
[mary,june,sunday]
[paula,march,friday]
[tara,july,saturday]

(15 ms) no
| ?-

Football Playoffs - by Shelly Hazard

The football season was coming to an end and the regional playoffs were this weekend. Millersville’s high school team, the Bulls, had made it to the playoffs and hopes were high as the whole town counted down to the weekend and game time. Millersville High School hadn’t had a winning football team in years, and this year’s team had been undefeated during the regular season. The star player on each of the four contending teams played a different position. Determine the name of the four contending teams and the full name and position played of each star player.

  1. The star player for the Bulls was a quarterback but his first name wasn’t Elliot.
  2. The Chargers’ star player was Alex, who wasn’t a kicker. The receiver didn’t play on the Lions team.
  3. Elliot’s last name wasn’t Fenn. Charlie’s last name was Koning.
  4. Brandon wasn’t a receiver.
  5. The running back had a last name of Zorn but he didn’t play for the Rams.
  6. The player whose last name was Mansfield played for the Lions. The quarterback wasn’t Charlie.

Puzzle taken from puzzelersparadise.com.

print_solution([H|Tail]) :-
write(H),
nl,
print_solution(Tail).
solution(S) :-
S = [[Team1, FName1, LName1, Position1],
[Team2, FName2, LName2, Position2],
[Team3, FName3, LName3, Position3],
[Team4, FName4, LName4, Position4]],
% Four teams, four star players
Teams = [bulls, chargers, lions, rams],
Teams = [Team1, Team2, Team3, Team4],
FNames = [alex, brandon, charlie, elliot],
permutation(FNames, [FName1, FName2, FName3, FName4]),
LNames = [fenn, koning, mansfield, zorn],
permutation(LNames, [LName1, LName2, LName3, LName4]),
Positions = [kicker, quarterback, receiver, runningb],
permutation(Positions, [Position1, Position2, Position3, Position4]),
% The star player for the Bulls was a quarterback
% but his first name wasn't Elliot.
member([bulls, FName1, _, quarterback], S), FName1 \= elliot,
% The Chargers star player was Alex, who wasn't a kicker.
member([chargers, alex, _, Position2], S), Position2 \= kicker,
% The receiver didn't play on the Lions team.
member([lions, _, _, Position3], S), Position3 \= receiver,
% Elliot's last name wasn't Fenn.
member([_, elliot, UnknownLName1, _], S), UnknownLName1 \= fenn,
% Charlie's last name was Koning.
member([_, charlie, koning, _], S),
% Brandon wasn't a receiver.
member([_, brandon, _, UnknownPosition1], S), UnknownPosition1 \= receiver,
% The running back had a last name of Zorn but he didn't play for the Rams.
member([UnknownTeam1, _, zorn, runningb], S), UnknownTeam1 \= rams,
% The player whose last name was Mansfield played for the Lions.
member([lions, _, mansfield, _], S),
% The quarterback wasn't Charlie.
member([_, UnknownFName1, _, quarterback], S), UnknownFName1 \= charlie,
print_solution(S).

Executing the Prolog program solves the puzzle with following answer:

  • Brandon Fenn is Quarter Back for Bulls
  • Alex Zorn is Running Back for Chargers
  • Elliot Mansfield is Kicker for Lions
  • Charlie Rams is Receiver for Rams
$ gprolog  --consult-file football.pl --query-goal "solution(S)"
GNU Prolog 1.4.1
By Daniel Diaz
Copyright (C) 1999-2012 Daniel Diaz
compiling football.pl for byte code...
football.pl compiled, 52 lines read - 6746 bytes written, 15 ms
| ?- solution(S).

[bulls,brandon,fenn,quarterback]
[chargers,alex,zorn,runningb]
[lions,elliot,mansfield,kicker]
[rams,charlie,koning,receiver]

(13 ms) no
| ?-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment