{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
Peter Norvig
May 2015
\n", "\n", "# When Cheryl Met Eve: A Birthday Story\n", "\n", "The ***Cheryl's Birthday*** logic puzzle [made the rounds](https://www.google.com/webhp?#q=cheryl%27s+birthday),\n", "and I wrote [code](Cheryl.ipynb) that solves it. In that notebook I said that one reason for solving the puzzle with code rather than pencil and paper is that you can do more with code. \n", "\n", "**[Gabe Gaster](http://www.gabegaster.com/)** proved me right when he [tweeted](https://twitter.com/gabegaster/status/593976413314777089/photo/1) that he had used my code to generate a new list of dates that satisfies the constraints of the puzzle:\n", "\n", " January 15, January 4,\n", " July 13, July 24, July 30,\n", " March 13, March 24,\n", " May 11, May 17, May 30\n", "\n", "In this notebook, I verify Gabe's result, and explore some new variations on the puzzle.\n", "\n", "First, let's recap [the original Cheryl's Birthday puzzle](https://en.wikipedia.org/wiki/Cheryl%27s_Birthday):\n", "\n", "- Albert and Bernard became friends with Cheryl, and want to know when her birthday is.\n", "- Cheryl wrote down a list of 10 possible dates for all to see:\n", " - May 15, May 16, May 19,\n", " June 17, June 18,\n", " July 14, July 16,\n", " August 14, August 15, August 17\n", "- **Cheryl** then privately tells Albert the month and Bernard the day of her birthday.\n", "- **Albert**: \"I don't know when Cheryl's birthday is, and I know that Bernard does not know.\"\n", "- **Bernard**: \"At first I don't know when Cheryl's birthday is, but I know now.\"\n", "- **Albert**: \"Then I also know when Cheryl's birthday is.\"\n", "- So when is Cheryl's birthday?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Code for Original Cheryl's Birthday Puzzle" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a slight modification of my [previous code](Cheryl.ipynb). The puzzle concerns these key concepts:\n", "\n", "- **Possible dates** that might be Cheryl's birthday.\n", "- **Knowing** which dates are still possible; knowing for sure when only one is possible.\n", "- **Telling** Albert and Bernard specific facts about the birthday.\n", "- **Statements** made by Albert or Bernard about their knowledge of the birthday.\n", "\n", "I implement them as follows:\n", "- The global variable `dates` is a set of all possible dates (each date is a string).\n", "- `know(possible_dates)` is a function that returns `True` when there is only one possible date.\n", "- `told(part)` is a function that returns the set of possible dates that remain after Cheryl tells a part (month or day).\n", "- A statement is a function; *statement*`(date)` that returns true if the statement is true given that `date` is Cheryl's birthday.\n", "- `satisfy(possible_dates, statement,...)` returns a subset of possible_dates for which all the statements are true.\n", "\n", "In the [previous code](Cheryl.ipynb) I treated `dates` as a constant, but in this version the whole point is exploring different sets of possible dates. The easiest way to refactor the code was to make `dates` a global variable, and provide the function `update_dates` to set the value of the global variable. (It would be cleaner to package the dates into a non-global object, but it would be a big change to the code to inject this all the way down to the function `told`, where it is needed.)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Albert and Bernard just became friends with Cheryl, and they want to know when her birthday is. \n", "# Cheryl gave them a list of 10 possible dates:\n", "\n", "dates = {'May 15', 'May 16', 'May 19',\n", " 'June 17', 'June 18',\n", " 'July 14', 'July 16',\n", " 'August 14', 'August 15', 'August 17'}\n", "\n", "def month(date): return date.split()[0]\n", "def day(date): return date.split()[1]\n", "\n", "# Cheryl then tells Albert and Bernard separately \n", "# the month and the day of the birthday respectively.\n", "\n", "BeliefState = set\n", "\n", "def told(part: str) -> BeliefState:\n", " \"\"\"Cheryl told a part of her birthdate to someone; return a belief state of possible dates.\"\"\"\n", " return {date for date in dates if part in date}\n", "\n", "def know(beliefs: BeliefState) -> bool:\n", " \"\"\"A person `knows` the answer if their belief state has only one possibility.\"\"\"\n", " return len(beliefs) == 1\n", "\n", "def satisfy(some_dates, *statements) -> BeliefState:\n", " \"\"\"Return the subset of dates that satisfy all the statements.\"\"\"\n", " return {date for date in some_dates\n", " if all(statement(date) for statement in statements)}\n", "\n", "# Albert and Bernard make three statements:\n", "\n", "def albert1(date) -> bool:\n", " \"\"\"Albert: I don't know when Cheryl's birthday is, but I know that Bernard does not know too.\"\"\"\n", " albert_beliefs = told(month(date))\n", " return not know(albert_beliefs) and not satisfy(albert_beliefs, bernard_knows)\n", "\n", "def bernard_knows(date) -> bool: return know(told(day(date))) \n", "\n", "def bernard1(date) -> bool:\n", " \"\"\"Bernard: At first I don't know when Cheryl's birthday is, but I know now.\"\"\"\n", " at_first_beliefs = told(day(date))\n", " after_beliefs = satisfy(at_first_beliefs, albert1)\n", " return not know(at_first_beliefs) and know(after_beliefs)\n", "\n", "def albert2(date) -> bool:\n", " \"\"\"Albert: Then I also know when Cheryl's birthday is.\"\"\"\n", " then = satisfy(told(month(date)), bernard1)\n", " return know(then)\n", " \n", "# So when is Cheryl's birthday?\n", "\n", "def cheryls_birthday(possible_dates) -> BeliefState:\n", " \"\"\"Return a subset of the dates for which all three statements are true.\"\"\"\n", " return satisfy(update_dates(possible_dates), albert1, bernard1, albert2)\n", "\n", "def update_dates(possible_dates) -> BeliefState:\n", " \"\"\"Set the value of the global `dates` to `possible_dates`.\"\"\"\n", " global dates\n", " dates = possible_dates\n", " return dates" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Some tests\n", "\n", "assert month('May 19') == 'May'\n", "assert day('May 19') == '19'\n", "assert albert1('May 19') == False\n", "assert albert1('July 14') == True\n", "assert know(told('16')) == False\n", "assert know(told('19')) == True\n", "assert know(told('May')) == False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Below we trace through how this works.\n", "\n", "First Albert says that he doesn't know, and that Bernard doesn't either. So the possible remaining dates are:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'August 14', 'August 15', 'August 17', 'July 14', 'July 16'}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "satisfy(dates, albert1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bernard says he initially didn't know, but now he does. He knows, but we the puzzle-solvers don't. The remaining possible dates for us are:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'August 15', 'August 17', 'July 16'}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "satisfy(dates, albert1, bernard1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now Albert knows, and so do we:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'July 16'}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "satisfy(dates, albert1, bernard1, albert2)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'July 16'}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cheryls_birthday(dates)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Verifying Gabe's Version" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Gabe tweeted these ten dates:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "gabe_dates = [\n", " 'January 15', 'January 4',\n", " 'July 13', 'July 24', 'July 30',\n", " 'March 13', 'March 24',\n", " 'May 11', 'May 17', 'May 30']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can verify that they do indeed make the puzzle work:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'July 30'}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cheryls_birthday(gabe_dates)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating Our Own Versions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If Gabe can do it, we can do it! Our strategy will be to repeatedly pick a random sample of dates, and check if they solve the puzzle. We'll limit ourselves to a subset of dates (not all 366) to make it more likely that a random selection will have multiple dates with the same month and day (otherwise Albert and/or Bernard would know right away):" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "many_dates = {mo + ' ' + d1 + d2\n", " for mo in {'April', 'August', 'July', 'June', 'March', 'May'}\n", " for d1 in '12'\n", " for d2 in '3456789'}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we need to cycle through random samples of these possible dates until we hit one that works. I anticipate wanting to solve other puzzles besides the original `cheryls_birthday`, so I'll define the function `pick_dates` to take a parameter, `puzzle`:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "import random\n", "\n", "def pick_dates(puzzle, k=10) -> BeliefState:\n", " \"\"\"Pick a set of `k` dates for which the `puzzle` has a unique solution.\"\"\"\n", " while True:\n", " random_dates = random.sample(many_dates, k)\n", " solutions = puzzle(random_dates)\n", " if know(solutions):\n", " return set(random_dates)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'April 18',\n", " 'August 19',\n", " 'August 28',\n", " 'July 15',\n", " 'June 14',\n", " 'June 19',\n", " 'June 28',\n", " 'May 14',\n", " 'May 19',\n", " 'May 24'}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pick_dates(cheryls_birthday)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'July 16', 'July 18', 'July 29', 'March 16', 'March 18', 'May 29'}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pick_dates(cheryls_birthday, k=6)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'April 23',\n", " 'April 24',\n", " 'April 29',\n", " 'August 19',\n", " 'August 24',\n", " 'August 29',\n", " 'June 13',\n", " 'June 23',\n", " 'March 14',\n", " 'March 15',\n", " 'May 13',\n", " 'May 27'}" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pick_dates(cheryls_birthday, k=12)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Great! We can make a new puzzle, just like Gabe. But how often do we get a unique solution to the puzzle (that is, the puzzle returns a set of size 1)? How often do we get a solution where Albert and Bernard know, but we the puzzle solver don't (that is, a belief set of size greater than 1)? How often is there no solution (size 0)? Let's make a Counter of the number of times each length-of-solution occurs:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "from collections import Counter\n", "\n", "def solution_lengths(puzzle, N=10000, k=10, many_dates=many_dates):\n", " \"\"\"Try N random samples of k dates and count how often each possible \n", " length-of-puzzle-solution appears.\"\"\"\n", " return Counter(len(puzzle(random.sample(many_dates, k)))\n", " for _ in range(N))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Counter({0: 9414, 2: 380, 1: 206})" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "solution_lengths(cheryls_birthday)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This says that about 2% of the time we get a unique solution (a set of length 1). More often than that we get an ambiguous solution (with 2 or more possible birth dates), but about 95% of the time the sample of dates has no solution (a set of length 0).\n", "\n", "What happens if Cheryl changes the number of possible dates?" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Counter({0: 9987, 1: 3, 2: 10})" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "solution_lengths(cheryls_birthday, k=6)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Counter({0: 8821, 2: 632, 1: 537, 3: 10})" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "solution_lengths(cheryls_birthday, k=12)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is hard (but not impossible) to find a set of 6 dates that work for the puzzle, and much easier to find a solution with 12 dates." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# A New Puzzle: All About Eve" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's see if we can create a more complicated puzzle. We'll introduce a new character, Eve, and keep the same puzzle as before,e xcept that after Albert's second statement, Eve makes this statement:\n", "\n", "- **Eve**: \"Hi, Everybody. My name is Eve and I'm an evesdropper. It's what I do! I peeked and saw the first letter of the month and the first digit of the day. When I peeked, I didn't know Cheryl's birthday, but after listening to Albert and Bernard I do. And it's a good thing I peeked, because otherwise I couldn't have\n", "figured it out.\"\n", "\n", "We can easily code this up:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "def cheryls_birthday_with_eve(dates):\n", " \"Return a set of the dates for which Albert, Bernard, and Eve's statements are true.\"\n", " return satisfy(update_dates(dates), albert1, bernard1, albert2, eve1)\n", "\n", "def eve1(date):\n", " \"\"\"Eve: I peeked and saw the first letter of the month and the first digit of the day. \n", " When I peeked, I didn't know Cheryl's birthday, but after listening to Albert and Bernard \n", " I do. And it's a good thing I peeked, because otherwise I couldn't have figured it out.\"\"\"\n", " at_first = told(day(date)[0]) & told(month(date)[0])\n", " return (not know(at_first) and\n", " know(satisfy(at_first, albert1, bernard1, albert2)) and\n", " not know(satisfy(dates, albert1, bernard1, albert2)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Note*: I admit I \"cheated\" a bit here. Remember that the function `told` tests for `(part in date)`. For that to work for Eve, we have to make sure that the first letter is distinct from any other character in the date (it is—because only the first letter is uppercase) and that the first digit is distinct from any other character (it is—because in `many_dates` I carefully made sure that the first digit is always 1 or 2, and the second digit is never 1 or 2). \n", "\n", "I have no idea if it is possible to find a set of dates that works for this puzzle. But I can try:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'April 26',\n", " 'August 25',\n", " 'June 15',\n", " 'June 19',\n", " 'June 23',\n", " 'June 29',\n", " 'March 13',\n", " 'March 23',\n", " 'May 13',\n", " 'May 19'}" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pick_dates(puzzle=cheryls_birthday_with_eve)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That was easy. How often is a random sample of dates a solution to this puzzle?" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Counter({0: 9708, 1: 138, 2: 154})" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "solution_lengths(cheryls_birthday_with_eve)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A solution (a set of length 1) occurs a bit less often than with the original puzzle." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# An Even More Complex Puzzle" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's make the puzzle even more complicated by making Albert wait one more time before he finally knows:\n", "\n", "- Albert and Bernard just became friends with Cheryl, and they want to know when her birtxhday is. Cheryl wrote down a list of 10 possible dates for all to see.\n", "- **Cheryl** then writes down the month and shows it just to Albert, and also writes down the day and shows it just to Bernard.\n", "- **Albert**: I don't know when Cheryl's birthday is, but I know that Bernard does not know either. \n", "- **Bernard**: At first I didn't know when Cheryl's birthday is, but I know now.\n", "- **Albert**: I still don't know.\n", "- **Eve**: Hi, Everybody. My name is Eve and I'm an evesdropper. It's what I do! I peeked and saw the first letter of the month and the first digit of the day. When I peeked, I didn't know Cheryl's birthday, but after listening to Albert and Bernard I do. And it's a good thing I peeked, because otherwise I couldn't have\n", "figured it out.\n", "- **Albert**: OK, now I know.\n", "- So when is Cheryl's birthday?\n", "\n", "Albert's second statement is different; he has a new third statement; and Eve's statement uses the same words, but it now implicitly refers to a different statement by Albert. We'll use the names `albert2c`, `albert3c`, and `eve1c` (`c` for \"complex\") to represent the new statements:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "def cheryls_birthday_complex(dates):\n", " \"Return a set of the dates for which Albert, Bernard, and Eve's statements are true.\"\n", " return satisfy(update_dates(dates), albert1, bernard1, albert2c, eve1c, albert3c)\n", "\n", "def albert2c(date):\n", " \"Albert: I still don't know.\"\n", " return not know(satisfy(told(month(date)), bernard1))\n", "\n", "def eve1c(date):\n", " \"\"\"Eve: I peeked and saw the first letter of the month and the first digit of the day. \n", " When I peeked, I didn't know Cheryl's birthday, but after listening to Albert and Bernard \n", " I do. And it's a good thing I peeked, because otherwise I couldn't have figured it out.\"\"\"\n", " at_first = told(day(date)[0]) & told(month(date)[0])\n", " return (not know(at_first)\n", " and know(satisfy(at_first, albert1, bernard1, albert2c)) and\n", " not know(satisfy(dates, albert1, bernard1, albert2c)))\n", "\n", "def albert3c(date):\n", " \"Albert: OK, now I know.\"\n", " return know(satisfy(told(month(date)), bernard1, eve1c))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Again, I don't know if it is possible to find dates that works with this story, but I can try:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'April 16',\n", " 'April 28',\n", " 'August 13',\n", " 'August 18',\n", " 'August 23',\n", " 'August 26',\n", " 'August 27',\n", " 'July 16',\n", " 'July 27',\n", " 'June 29'}" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pick_dates(puzzle=cheryls_birthday_complex)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It worked! Were we just lucky, or are there many sets of dates that work?" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Counter({0: 9207, 1: 790, 2: 3})" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "solution_lengths(cheryls_birthday_complex)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Interesting. It was actually easier to find dates that work for this story than for any of the other stories." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Analyzing a Solution to the Complex Puzzle" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we will go through a solution step-by-step. We'll use these dates:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "complex_dates = {\n", " 'April 28',\n", " 'July 27',\n", " 'June 19',\n", " 'June 16',\n", " 'July 15',\n", " 'April 15',\n", " 'June 29',\n", " 'July 16',\n", " 'May 24',\n", " 'May 27'}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's find the solution:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'July 27'}" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cheryls_birthday_complex(complex_dates)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now the first step is that Albert was told \"July\":" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'July 15', 'July 16', 'July 27'}" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "told('July')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And no matter which of these three dates is the actual birthday, Albert knows that Bernard would not know the birthday, because each of the days (15, 16, 27) appears twice in the list of possible dates." ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not know(told('15')) and not know(told('16')) and not know(told('27'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Meanwhile, Bernard is told the day:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'July 27', 'May 27'}" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "told('27')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are two dates with a 27, so Bernard did not know then. But only one of these dates is still consistent after hearing Albert's statement:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'July 27'}" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "satisfy(told('27'), albert1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So after Albert's statement, Bernard knows. Poor Albert still doesn't know (after being told `'July'` and hearing Bernard's statement):" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'July 15', 'July 16', 'July 27'}" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "satisfy(told('July'), bernard1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then along comes Eve. She evesdrops the \"J\" and the \"2\":" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'July 27', 'June 29'}" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "told('J') & told('2')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Two dates, so Eve doesn't know after evesdropping. But only one of the dates works after hearing the three statements made by Albert and Bernard:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'July 27'}" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "satisfy(told('J') & told('2'), albert1, bernard1, albert2c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But Eve wouldn't have known if she hadn;t been told anything:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'July 15', 'July 16', 'July 27'}" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "satisfy(dates, albert1, bernard1, albert2c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What about Albert? After hearing Eve's statement he finally knows:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'July 27'}" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "satisfy(told('July'), eve1c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# What Next?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are many other directions you could take this:\n", "\n", "- Could you create a puzzle that goes one or two rounds more before everyone knows?\n", "- Could you add new characters: Faith, and then George, and maybe even a new Hope?\n", "- Would it be more interesting with a different number of possible dates (not 10)?\n", "- Should we include the year or the day of the week, as well as the month and day?\n", "- Perhaps a puzzle that starts with [Richard Smullyan](http://en.wikipedia.org/wiki/Raymond_Smullyan) announcing that one of the characters is a liar.\n", "- Or you could make a puzzle harder than [the hardest logic puzzle ever](https://en.wikipedia.org/wiki/The_Hardest_Logic_Puzzle_Ever).\n", "- Try the \"black and white hats\" [Riddler Express](https://fivethirtyeight.com/features/can-you-solve-these-colorful-puzzles/) stumper.\n", "- It's up to you ..." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.15" } }, "nbformat": 4, "nbformat_minor": 4 }