├── README.md └── cheryl.pl /README.md: -------------------------------------------------------------------------------- 1 | # cheryls-birthday-prolog 2 | 3 | This is an attempt to solve the [Cheryl's Birthday](http://en.wikipedia.org/wiki/Cheryl%27s_Birthday) problem with [Prolog](http://en.wikipedia.org/wiki/Prolog), the venerable logic programming language. 4 | 5 | See blog post here for more details: http://jxf.me/entries/cheryls-birthday/ 6 | -------------------------------------------------------------------------------- /cheryl.pl: -------------------------------------------------------------------------------- 1 | /* 2 | See blog post here for more details: http://jxf.me/entries/cheryls-birthday/ 3 | */ 4 | 5 | 6 | /* 7 | Cheryl's Birthday 8 | 9 | A and B have just met Cheryl. "When is your birthday?" A asked Cheryl. 10 | 11 | Cheryl thought for a moment and said, "I won't tell you, but I'll give you some clues." 12 | 13 | She wrote down a list of 10 dates: 14 | 15 | May 15, May 16, May 19 16 | June 17, June 18 17 | July 14, July 16 18 | August 14, August 15, August 17 19 | 20 | "One of these is my birthday," she said. 21 | 22 | Cheryl whispered in A's ear the month, and only the month, of her birthday. 23 | To B, she whispered the day, and only the day. 24 | 25 | "Can you figure it out now?" she asked A. 26 | 27 | A: I don't know when your birthday is, but I know B doesn't know, either. 28 | B: I didn't know originally, but now I do. 29 | A: Well, now I know, too! 30 | 31 | When is Cheryl’s birthday? 32 | */ 33 | 34 | candidate_birthday(Month, Day) :- 35 | member(Month/Day, 36 | [ 37 | 'May'/15, 'May'/16, 'May'/19, 38 | 'June'/17, 'June'/18, 39 | 'July'/14, 'July'/16, 40 | 'August'/14, 'August'/15, 'August'/17 41 | ] 42 | ). 43 | 44 | /* true if month contains a day that uniquely decides month */ 45 | month_has_deciding_day(Month):- 46 | candidate_birthday(Month, Day), 47 | findall(X, candidate_birthday(X, Day), [_]). 48 | 49 | /* A: I don't know when your birthday is, but I know B doesn't know, either. */ 50 | s1(Month, Day):- 51 | /* must be a day that Cheryl specified */ 52 | candidate_birthday(Month, Day), 53 | /* A doesn't know; so at least 2 birthdays in that month */ 54 | findall(X, candidate_birthday(Month, X), [_, _ | _]), 55 | /* A knows B doesn't know; so month doesn't decide day */ 56 | not(month_has_deciding_day(Month)). 57 | 58 | /* B: I didn't know originally, but now I do. */ 59 | s2(Month, Day):- 60 | /* must be a day that Cheryl specified */ 61 | candidate_birthday(Month, Day), 62 | /* B now knows, so his day must uniquely decide the month */ 63 | findall(X, s1(X, Day), [Month]). 64 | 65 | /* A: Well, now I know, too! */ 66 | s3(Month, Day):- 67 | /* must be a day that Cheryl specified */ 68 | candidate_birthday(Month, Day), 69 | /* A now knows, so his month must uniquely decide this day */ 70 | findall(X, s2(Month, X), [Day]). 71 | 72 | /* When is Cheryl’s birthday? */ 73 | birthday(Month, Day):- s3(Month, Day). 74 | --------------------------------------------------------------------------------