{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# When Cheryl Met Eve: A Birthday Story"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div align=right>Peter Norvig, May 2015</div>\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](http://nbviewer.ipython.org/url/norvig.com/ipython/Cheryl.ipynb) that solves it. There I said that one reason for solving the problem with code rather than pencil and paper is that you can do more with code.  [Gabe Gaster](http://www.gabegaster.com/) proved me right when he [tweeted](https://twitter.com/gabegaster/status/593976413314777089/photo/1)  that he had extended my code to generate a new list of dates that satisfies the constraints of the puzzle:\n",
    "<pre>\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",
    "</pre>\n",
    "In this notebook, I verify Gabe's result, and find some other variations on the puzzle."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Code for Original Cheryl's Birthday Puzzle"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's recap the puzzle and the code to solve it. Like before, we model each statement by a person as a function that takes a date as input, and returns true if the statement is true of that date. Like before, the function `tell` takes a string as input, and returns a set of all the dates that contain that string; and the function `know` takes a set of dates and returns true if that set consists of one unique date.\n",
    "\n",
    "I've refactored my previous code, in a few ways:\n",
    "- `DATES` is now a global variable, not a constant. \n",
    "- I switched to *sets* of possible dates, not *lists*. \n",
    "- I define a new function, `satisfy`, which is similar to the built-in function `filter`, except that `satisfy` returns a set, not a list, and it allows you to specify more than one function that the candidate elements must satisfy.\n",
    "- I changed the name `statement3` to `albert1`, and so on."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Albert and Bernard just became friends with Cheryl, and they want to know when her birthday is. \n",
    "# Cheryl gave them a set of 10 possible dates:\n",
    "\n",
    "from __future__ import division, print_function\n",
    "\n",
    "CHERYL_DATES = {\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",
    "\n",
    "# Cheryl then tells Albert and Bernard separately the month and the day of the birthday respectively.\n",
    "\n",
    "def tell(part):\n",
    "    \"Cheryl tells a part of her birthdate to someone; return a new set of possible dates that match the part.\"\n",
    "    return {date for date in DATES if part in date}\n",
    "\n",
    "def Month(date): return date.split()[0]\n",
    "\n",
    "def Day(date):   return date.split()[1]\n",
    "\n",
    "def know(possible_dates):\n",
    "    \"A person knows the birthdate if they know there is exactly one possible date.\"\n",
    "    return len(possible_dates) == 1\n",
    "\n",
    "# Albert and Bernard make three statements:\n",
    "\n",
    "def albert1(date):\n",
    "    \"Albert: I don't know when Cheryl's birthday is, but I know that Bernard does not know too.\"\n",
    "    told = tell(Month(date))\n",
    "    return (not know(told) \n",
    "            and all(not know(tell(Day(d)))\n",
    "                    for d in told))\n",
    "\n",
    "def bernard1(date):\n",
    "    \"Bernard: At first I don't know when Cheryl's birthday is, but I know now.\"\n",
    "    at_first = tell(Day(date))\n",
    "    return (not know(at_first)\n",
    "            and know(satisfy(at_first, albert1)))\n",
    "\n",
    "def albert2(date):\n",
    "    \"Albert: Then I also know when Cheryl's birthday is.\"\n",
    "    return know(satisfy(tell(Month(date)), bernard1))\n",
    "    \n",
    "# So when is Cheryl's birthday?\n",
    "\n",
    "def cheryls_birthday(dates):\n",
    "    \"Return a list of the possible dates for which Albert and Bernard's statements are true.\"\n",
    "    global DATES\n",
    "    DATES = dates # Assign to global DATES here; used by `tell`\n",
    "    return satisfy(dates, albert1, bernard1, albert2)\n",
    "\n",
    "def satisfy(items, *predicates):\n",
    "    \"Return the subset of items that satisfy all the predicates.\"\n",
    "    return {item for item in items\n",
    "            if all(p(item) for p in predicates)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'July 16'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cheryls_birthday(CHERYL_DATES)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "know(cheryls_birthday(CHERYL_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": 4,
   "metadata": {
    "collapsed": false
   },
   "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, giving a single known birthdate:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'July 30'}"
      ]
     },
     "execution_count": 5,
     "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 a random selection will have multiple dates with the same month and day (otherwise Albert and Bernard would know right away):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "some_dates = {mo + ' ' + d1 + d2\n",
    "              for mo in ('March', 'April', 'May', 'June', 'July')\n",
    "              for d1 in '12'\n",
    "              for d2 in '56789'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's how to pick a random sample of the dates:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "def sample(dates=some_dates, n=10):\n",
    "    \"Return a random sample of dates.\"\n",
    "    return set(random.sample(dates, n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'April 26',\n",
       " 'April 27',\n",
       " 'July 15',\n",
       " 'July 29',\n",
       " 'June 25',\n",
       " 'March 15',\n",
       " 'March 16',\n",
       " 'March 17',\n",
       " 'March 29',\n",
       " 'May 27'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we need to cycle through samples until we hit one that works.  I anticipate wanting to solve other puzzles besides the original `cheryls_birthday`, so I'll make the `puzzle` be a parameter of the function `pick_dates`. Note that `pick_dates` returns two things: the ten dates that form the puzzle, and the one date that is the solution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def pick_dates(puzzle=cheryls_birthday):\n",
    "    \"Pick a set of dates for which the puzzle has a unique solution.\"\n",
    "    while True:\n",
    "        dates = sample()\n",
    "        solutions = puzzle(dates)\n",
    "        if know(solutions):\n",
    "            return dates, solutions.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "({'April 16',\n",
       "  'July 15',\n",
       "  'July 18',\n",
       "  'July 28',\n",
       "  'June 15',\n",
       "  'June 16',\n",
       "  'June 27',\n",
       "  'March 18',\n",
       "  'March 27',\n",
       "  'May 15'},\n",
       " 'March 18')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pick_dates()"
   ]
  },
  {
   "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 doesn't (that is, a 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": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from collections import Counter\n",
    "\n",
    "def counter(puzzle=cheryls_birthday, N=10000):\n",
    "    \"Try N random samples and count how often each possible length-of-solution appears.\"\n",
    "    return dict(Counter(len(puzzle(sample())) \n",
    "                for _ in range(N)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: 8707, 1: 642, 2: 648, 3: 3}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "counter(cheryls_birthday)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This says that about 6% of the time we get a unique solution (a set of `len` 1). A little more often than that we get an ambiguous solution (with 2 or more possible birth dates).  And most of the time, the sample of dates leads to no solution."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# A New Puzzle: Enter 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, give her a statement, and alter the rest of the puzzle slightly:\n",
    "\n",
    "*Albert and Bernard just became friends with Cheryl, and they want to know when her birthday is. Cheryl wrote down a list of 10 possible dates for all to see.*\n",
    "    \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",
    "\n",
    "**Albert**: *I don't know when Cheryl's birthday is, but I know that Bernard does not know either.*\n",
    "\n",
    "**Bernard**: *At first I didn't know when Cheryl's birthday is, but I know now.*\n",
    "    \n",
    "**Albert**: *Then I also know when Cheryl's birthday is.*\n",
    "\n",
    "**Eve**: *Hi, 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 I couldn't have\n",
    "figured it out without peeking.*\n",
    "    \n",
    "*So when is Cheryl's birthday?*\n",
    "\n",
    "We can easily code this up:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def cheryls_birthday_with_eve(dates):\n",
    "    \"Return a list of the possible dates for which Albert, Bernard, and Eve's statements are true.\"\n",
    "    global DATES\n",
    "    DATES = dates\n",
    "    return satisfy(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 I do. \n",
    "    And it's a good thing I peeked, because I couldn't have figured it out without peeking.\"\"\"\n",
    "    at_first = tell(first(Day(date))) & tell(first(Month(date)))\n",
    "    return (not know(at_first)\n",
    "            and know(satisfy(at_first, albert1, bernard1, albert2)) and\n",
    "            not know(satisfy(tell(''), albert1, bernard1, albert2)))\n",
    "\n",
    "def first(seq): return seq[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Note*: I admit I \"cheated\" a bit here.  Remember that the function `tell`  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&mdash;because only the first letter is uppercase) and that the first digit is distinct from any other character (it is&mdash;because in `some_dates` I carefully made sure that the first digit is always 1 or 2, and the second digit is never 1 or 2). Also note that `tell('')` denotes the hypothetical situation where Cheryl told Eve nothing, and thus is equal to `DATES`.\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": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "({'April 26',\n",
       "  'July 17',\n",
       "  'June 15',\n",
       "  'June 16',\n",
       "  'June 18',\n",
       "  'June 27',\n",
       "  'March 27',\n",
       "  'March 28',\n",
       "  'May 16',\n",
       "  'May 28'},\n",
       " 'March 27')"
      ]
     },
     "execution_count": 14,
     "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": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: 9424, 1: 281, 2: 294, 3: 1}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "counter(cheryls_birthday_with_eve)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "About half as often as for 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",
    "    \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",
    "    \n",
    "**Albert**: *I don't know when Cheryl's birthday is, but I know that Bernard does not know either.*\n",
    "    \n",
    "**Bernard**: *At first I didn't know when Cheryl's birthday is, but I know now.*\n",
    "    \n",
    "**Albert**: *I still don't know.*\n",
    "\n",
    "**Eve**: *Hi, 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 I couldn't have figured it out without peeking.*\n",
    "\n",
    "**Albert**: *OK, now I know.*\n",
    "    \n",
    "*So when is Cheryl's birthday?*\n",
    "\n",
    "Let's be careful in coding this up; 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`,  `eve1c`, and `albert3c` (`c` for \"complex\") to represent the new statements:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def cheryls_birthday_complex(dates):\n",
    "    \"Return a list of the possible dates for which Albert, Bernard, and Eve's statements are true.\"\n",
    "    global DATES\n",
    "    DATES = dates\n",
    "    return satisfy(dates, albert1, bernard1, albert2c, eve1c, albert3c)\n",
    "\n",
    "def albert2c(date):\n",
    "    \"Albert: I still don't know.\"\n",
    "    return not know(satisfy(tell(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 I do. \n",
    "    And it's a good thing I peeked, because I couldn't have figured it out without peeking.\"\"\"\n",
    "    at_first = tell(first(Day(date))) & tell(first(Month(date)))\n",
    "    return (not know(at_first)\n",
    "            and know(satisfy(at_first, albert1, bernard1, albert2c)) and\n",
    "            not know(satisfy(tell(''), albert1, bernard1, albert2c)))\n",
    "\n",
    "def albert3c(date):\n",
    "    \"Albert: OK, now I know.\"\n",
    "    return know(satisfy(tell(Month(date)), 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": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "({'April 18',\n",
       "  'April 26',\n",
       "  'April 28',\n",
       "  'July 15',\n",
       "  'July 18',\n",
       "  'July 29',\n",
       "  'June 29',\n",
       "  'March 16',\n",
       "  'May 18',\n",
       "  'May 26'},\n",
       " 'May 18')"
      ]
     },
     "execution_count": 17,
     "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": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: 9153, 1: 844, 2: 3}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "counter(cheryls_birthday_complex)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Interesting. It was actually easier to find dates that work for this story than for either 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 a set of dates selected in a previous run:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "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": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'July 27'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cheryls_birthday_complex(DATES)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now the first step is that Albert was told \"July\":"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'July 15', 'July 16', 'July 27'}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tell('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": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all(not know(tell(Day(d)))\n",
    "    for d in tell('July'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, Bernard is told the day:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'July 27', 'May 27'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tell('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 satisfies Albert's statement:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'July 27'}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "satisfy(tell('27'), albert1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So now Bernard knows. Poor Albert still doesn't know:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'July 15', 'July 16', 'July 27'}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "satisfy(tell('July'), bernard1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then along comes Eve. She evesdrops the \"J\" and the \"2\":"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'July 27', 'June 29'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tell('J') & tell('2')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Two dates, so Eve doesn't know yet. But only one of the dates satisfies the statements:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'July 27'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "satisfy(tell('J') & tell('2'), albert1, bernard1, albert2c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But she wouldn't have known if she had been told nothing:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'July 15', 'July 16', 'July 27'}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "satisfy(tell(''), albert1, bernard1, albert2c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What about Albert?  After hearing Eve's statement he finally knows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'July 27'}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "satisfy(tell('July'), eve1c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Three Children\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def child_age_puzzle(states):\n",
    "    global STATES\n",
    "    STATES = states \n",
    "    return satisfy(states, parent1, friend1, parent2, friend2, parent3, friend3)\n",
    "\n",
    "def state(a, b, c, n): return \n",
    "\n",
    "STATES = {tuple(sorted([a, b, c])) + (n,) \n",
    "          for a in range(20) for b in range(20) for c in range(20) for n in range(60)\n",
    "          if a * b * c == 36}\n",
    "\n",
    "def parent1(state): \n",
    "    \"Parent: the product of my three childrens' ages is 36.\"\n",
    "    a, b, c, n = state\n",
    "    return a * b * c == 36\n",
    "\n",
    "def parent2(state):\n",
    "    \"Parent: the sum of their ages is the same as the number of people in this room.\"\n",
    "    a, b, c, n = state\n",
    "    return a + b + c == n\n",
    "\n",
    "def parent3(state):\n",
    "    \"Parent: oldest one likes bananas.\"\n",
    "    a, b, c, n = state\n",
    "    return c > b\n",
    "\n",
    "def friend1(state): return not know(satisfy(STATES, parent1))\n",
    "def friend2(state): return not know(satisfy(STATES, parent1, friend1, parent2))\n",
    "def friend3(state): return     know(satisfy(STATES, parent1, parent2, friend2, parent3))\n",
    "\n",
    "child_age_puzzle(STATES)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(1, 2, 18, 21),\n",
       " (1, 3, 12, 16),\n",
       " (1, 4, 9, 14),\n",
       " (1, 6, 6, 13),\n",
       " (2, 2, 9, 13),\n",
       " (2, 3, 6, 11),\n",
       " (3, 3, 4, 10)}"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "satisfy(STATES, parent1, friend1, parent2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(1, 2, 18, 21),\n",
       " (1, 3, 12, 16),\n",
       " (1, 4, 9, 14),\n",
       " (2, 2, 9, 13),\n",
       " (2, 3, 6, 11),\n",
       " (3, 3, 4, 10)}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "satisfy(STATES, parent1, friend1, parent2, friend2, parent3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "satisfy(STATES, parent1, friend1, parent2, friend2, parent3, friend3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# What Next?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you like, 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",
    "- It's up to you ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
