26 April 2015
by Jem Mawson

When is Cheryl's Birthday? (Scala)

This puzzle was popularised recently and caused many people headaches. You've probably heard about it. I want to demonstrate a thinking approach that makes the puzzle very easy to solve. But first, a refresher.

Cheryl is very cryptic. She wants her two friends to know her birthday, but not know it at the same time. She gives them a list of possible dates:

May 15, 16, 19
June 17, 18
July 14, 16
August 14, 15, 17

She then tells Albert the month, and Bernard the day of month and feels very happy with herself. At this point, Albert and Bernard should share their knowledge with each other and then everyone would know Cheryl's birthday, but instead they have this conversation:

Albert: I don't know when Cheryl's birthday is, but I know that Bernard doesn't know too.
Bernard: At first I didn't know when Cheryl's birthday is, but I know now.
Albert: Then I also know when Cheryl's birthday is.

The last person to know Cheryl's birthday, it would seem, is us. So how do we solve this puzzle? The secret is to think a lot about sets. No, I said "sets".

We start with a set of possible answers and need to reduce this set to a singleton set (that is, a set with only one value) by inferring clues from the conversation between Albert and Bernard.

{ May 15, May 16, May 19, June 17, June 18, July 14, July 16, August 14, August 15, August 17 } => { ? }

Clue #1

Albert says I don't know when Cheryl's birthday is, but I know that Bernard doesn't know too. There are two parts to this statement.

1: I don't know when Cheryl's birthday is

This is obvious. Albert knows the month only. There is no way we can take one of the four months and reduce to a singleton set. Albert's knowledge could only be one of the following:

May =>    { May 15, May 16, May 19 }
June =>   { June 17, June 18 }
July =>   { July 14, July 16 }
August => { August 14, August 15, August 17 }

The second part is more telling.

2: but I know that Bernard doesn't know too

It seems that Albert has done some analysis on what Bernard knows, just as we have done on his. And he's come to a definitive conclusion - that Bernard also doesn't know.

So what could Bernard's knowledge be before the first clue? He is given a day, which allows him to reduce the sets to one of:

14 => { July 14, August 14 }
15 => { May 15, August 15 }
16 => { May 16, July 16 }
17 => { June 17, August 17 }
18 => { June 18 }
19 => { May 19 }

We see that two day-of-month values { 18, 19 } resolve to singletons. Receiving either of these dates would have ensured that Bernard knew the birthday. When Albert states that he knows that Bernard doesn't know, we can infer that the month Albert received must not be paired with a unique day-of-month. That means the month cannot be in { May, June }. To be clear, if it was May, then there's a chance that the date could be 19 and therefore a chance that Bernard does know. Likewise with June and 18.

As observers, we can now reduce the set of possible answers by removing May and June from

{ May 15, May 16, May 19, June 17, June 18, July 14, July 16, August 14, August 15, August 17 }

to

{ July 14, July 16, August 14, August 15, August 17 }

Clue #2

Having quickly understood all of this, Bernard responds with At first I didn't know when Cheryl's birthday is, but I know now.

Something changed after hearing Albert speak. Bernard previously had one of the following in mind:

14 => { July 14, August 14 }
15 => { May 15, August 15 }
16 => { May 16, July 16 }
17 => { June 17, August 17 }

But on hearing Albert say that Bernard doesn't know, Bernard can infer exactly as we did in the step above - May and June are out. This leaves him with these options:

14 => { July 14, August 14 }
15 => { August 15 }
16 => { July 16 }
17 => { August 17 }

He states that he now definitively knows what the answer is, so he must have been given a date that resolves to a singleton set. We can remove the 14th on this basis:

15 => { August 15 }
16 => { July 16 }
17 => { August 17 }

Leaving us observers with a further reduced set of possible answers from:

{ July 14, July 16, August 14, August 15, August 17 }

to

{ July 16, August 15, August 17 }

Clue #3

Again, having displayed deductive acuity to rival Sherlock Holmes, Albert claims Then I also know when Cheryl's birthday is. Using the same reasoning as in step 2, we can come to the answer also. Recall that before Bernard spoke, Albert knew the possible answers were:

July => { July 14, July 16 }
August => { August 14, August 15, August 17 }

But after hearing that Bernard knew the answer, and realising it can't be the 14th of any month, Bernard's options are reduced to:

July => { July 16 }
August => { August 15, August 17 }

And, as Bernard proclaims that he knows the birthday, then his given month must map to a singleton set. The answer must be July 16.

The Code

Here is the above logic put far more succinctly into code.

Summary

If you like this kind of puzzle, why not try the fabled Blue Eyes, also known as The Hardest Logic Puzzle in the World.

puzzle

This post is written by Jeremy Mawson. Jem is an agile software engineer and technical lead. He is a specialist in software development with wide interests and currently focusing on Scala and the Typesafe stack. He lives in Australia with his wife Chen. Together they remotely assist innovative and socially positive companies to achieve their technology goals.
More about Loft in Space.