Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Cheryl's birthday

32 views
Skip to first unread message

neng...@gmail.com

unread,
Apr 23, 2015, 6:28:13 AM4/23/15
to
Here is a program in Picat, which is much more compact than that in Prolog

Cheers,
Neng-Fa

/*
Albert and Bernard just met Cheryl. "When's your birthday?" Albert asked Cheryl.

Cheryl thought a second and said, "I'm not going to tell you, but I'll give you some clues." She wrote down a list of 10 dates:

May 15, May 16, May 19

June 17, June 18

July 14, July 16

August 14, August 15, August 17

"My birthday is one of these," she said.

Then Cheryl whispered in Albert's ear the month -- and only the month -- of her birthday. To Bernard, she whispered the day, and only the day.

"Can you figure it out now?" she asked Albert.

Albert: I don't know when your birthday is, but I know Bernard doesn't know, either.

Bernard: I didn't know originally, but now I do.

Albert: Well, now I know, too!
*/
% in Picat, by Neng-Fa Zhou, April, 2015
main =>
Table = [(5,15),(5,16),(5,19),(6,17),(6,18),(7,14),(7,16),(8,14),(8,15),(8,17)],
Months = 5..8,
Days = 14..19,

% Albert: I don't know when your birthday is, but I know Bernard doesn't know, either.
Months1 = [M : M in Months, findall(D,member((M,D),Table))=[_,_|_]], % Albert's possible months
member(Month,Months1), % Abert is told a month
Days1 = [D : D in Days, findall(_M,member((_M,D),Table))=[_,_|_]], % Bernard's possible days
Table1 = [(Month,D) : D in Days1, member((Month,D),Table)],

% Bernard: I didn't know originally, but now I do.
member(Day,Days1), % Bernad is told a day
findall(M,member((M,Day),Table)) = [_,_|_], % I didn't know originally
findall(M,member((M,Day),Table1)) = [Month], % but now I do

% Albert: Well, now I know, too!
findall(D,member((Month,D),Table1)) = [Day], % unique
writeln((Month,Day)).

neng...@gmail.com

unread,
Apr 24, 2015, 11:51:52 AM4/24/15
to
On Thursday, April 23, 2015 at 6:28:13 AM UTC-4, neng...@gmail.com wrote:

Joachim Schimpf correctly pointed out a mistake in my original program. Here is a corrected version.

% in Picat, by Neng-Fa Zhou, April 24, 2015
main =>
Table = [(5,15),(5,16),(5,19),(6,17),(6,18),(7,14),(7,16),(8,14),(8,15),(8,17)],
Months = [5,6,7,8],
Days = [14,15,16,17,18,19],

findall(M,clue1(Table,Months,Days,M)) = AlbertMonths, % possible months that were told
member(Month,AlbertMonths),

% Clue-2 Bernard: I didn't know originally, but now I do.
Table1 = [(M,D) : M in AlbertMonths, D in Days, member((M,D),Table)],
member(Day,Days), % Bernard was told a day
findall(M,member((M,Day),Table)) = [_,_|_], % I didn't know originally
findall(M,member((M,Day),Table1)) = [Month], % but now I do (Albert: Well, now I know, too!)

writeln((Month,Day)).

% Clue-1 Albert: I don't know when your birthday is, but I know Bernard doesn't know, either.
clue1(Table,Months,Days,Month) =>
Months1 = [M : M in Months, findall(D,member((M,D),Table))=[_,_|_]], % Albert doesn't know
member(Month,Months1), % Albert was told a month
foreach (Day in Days) % Bernard doesn't know, either.
if member((Month,Day),Table) then
findall(M,member((M,Day),Table)) = [_,_|_]
end
end.
0 new messages