Added questions to all problem pages, solution workings for days 1 and 2
[ou-summer-of-code-2017.git] / 10-word-search / wordsearch-solution.ipynb
index bf6f6c111d4a1fb3cbcd2e98855cae20994a4b14..61d009756777b3cb64ff1bd1b0547a07cdf247a5 100644 (file)
@@ -4,40 +4,85 @@
    "cell_type": "markdown",
    "metadata": {},
    "source": [
-    "# Wordsearch\n",
-    "Given a text file, consisting of three parts (a grid size, a grid, and a list of words), find:\n",
-    "* the words present in the grid, \n",
-    "* the longest word present in the grid, \n",
-    "* the number of words not present in the grid, \n",
-    "* the longest word not present that can be formed from the leftover letters\n",
+    "First nights in hotels are always a bit of an anticlimax, what with the recovery from travel and all. You decided to do one of your wordsearch puzzles.\n",
     "\n",
-    "The only words that need be considered are the ones given in the list in the puzzle input.\n",
+    "These puzzles are a bit different from normal because they have a puzzle grid and a list of words, but only some of the words are in the puzzle; some of the words given are decoys and aren't present.\n",
     "\n",
-    "The puzzle consists of:\n",
-    "1. A line consisting of _w_`x`_h_, where _w_ and _h_ are integers giving the width and height of the grid.\n",
-    "2. The grid itself, consisting of _h_ lines each of _w_ letters.\n",
-    "3. A list of words, one word per line, of arbitrary length. "
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "## Example\n",
+    "For instance, given the grid:\n",
     "\n",
-    "\n",
-    "`...p.mown.\n",
-    ".sdse..ee.\n",
-    ".e.elad.cr\n",
-    "pi.dtir.ah\n",
+    "```\n",
+    "fhjpamownq\n",
+    "wsdseuqeev\n",
+    "ieaeladhcr\n",
+    "piedtiriah\n",
     "rzsiwovspu\n",
-    "oawh.kieab\n",
-    "brow.c.rda\n",
-    "ecnotops.r\n",
-    "d.kc.d...b\n",
-    ".staple...`\n",
+    "oawhakieab\n",
+    "browpcfrda\n",
+    "ecnotopssr\n",
+    "dikchdnpnb\n",
+    "bstapleokr\n",
+    "```\n",
+    "and the list of words:\n",
+    "\n",
+    "* adapting, apace, bombing, cackles, carnal, chump, coccyxes, cowhides, crazies, crumbled, dock, fickler, foaming, guts, knows, lived, minuend, molested, mown, pears, probed, rhubarb, rioted, shinier, solaria, staple, tops, wide, winced\n",
+    "\n",
+    "you can find these words:\n",
+    "\n",
+    "* apace, cowhides, crazies, dock, knows, lived, mown, pears, probed, rhubarb, rioted, staple, tops, wide\n",
+    "\n",
+    "but these are the decoys:\n",
+    "\n",
+    "* adapting, bombing, cackles, carnal, chump, coccyxes, crumbled, fickler, foaming, guts, minuend, molested, shinier, solaria, winced\n",
+    "\n",
+    "For this puzzle, there are 14 words with a total length of 76 letters. (Some of the words may overlap in the grid, but don't worry about that when counting word lengths in your solution.)\n",
+    "\n",
+    "## About wordsearches\n",
+    "\n",
+    "Words can go in any of the eight directions (up, down, left, right, and diagonals) in a straight line. A letter in the grid can be in more than one word. Words don't wrap around the edges of the grid.\n",
+    "\n",
+    "In the example above, the words \"lived\", \"wide\" and \"staple\" are in these positions (two words are diagonal and share a letter).\n",
+    "\n",
+    "```\n",
+    "..........\n",
+    ".......e..\n",
+    "....l.d...\n",
+    ".....i....\n",
+    "....w.v...\n",
+    ".......e..\n",
+    "........d.\n",
+    "..........\n",
+    "..........\n",
+    ".staple...\n",
+    "```\n",
+    "\n",
+    "The longest word, \"cowhides\", runs vertically upwards:\n",
+    "\n",
+    "```\n",
+    "..........\n",
+    "...s......\n",
+    "...e......\n",
+    "...d......\n",
+    "...i......\n",
+    "...h......\n",
+    "...w......\n",
+    "...o......\n",
+    "...c......\n",
+    "..........\n",
+    "```\n",
+    "\n",
+    "If there are words present in the grid that aren't in the list of words given, ignore them. For instance, you can see the word \"brow\" running left to right on the seventh row of the grid, but that doesn't count as a word in this puzzle because \"brow\" isn't in the list of words you're given.\n",
+    "\n",
+    "You're safe to assume that each word in the puzzle is present either zero or one times, never more.\n",
+    "\n",
+    "## File format\n",
+    "The wordsearch puzzle is given as a text file. The first line of the file is WxH, where W and H are the width and height of the puzzle grid, in characters. The next H lines are the grid, each line being W characters long. Finally, there's an arbitrary number of words to look for, one on each line.\n",
+    "\n",
+    "Ignore any trailing or leading blank lines, and any whitespace on a line.\n",
+    "\n",
+    "The example puzzle above, a ten by ten grid, would be written to a file as:\n",
     "\n",
     "```\n",
+    "10x10\n",
     "fhjpamownq\n",
     "wsdseuqeev\n",
     "ieaeladhcr\n",
     "ecnotopssr\n",
     "dikchdnpnb\n",
     "bstapleokr\n",
+    "adapting\n",
+    "apace\n",
+    "bombing\n",
+    "cackles\n",
+    "carnal\n",
+    "chump\n",
+    "coccyxes\n",
+    "cowhides\n",
+    "crazies\n",
+    "crumbled\n",
+    "dock\n",
+    "fickler\n",
+    "foaming\n",
+    "guts\n",
+    "knows\n",
+    "lived\n",
+    "minuend\n",
+    "molested\n",
+    "mown\n",
+    "pears\n",
+    "probed\n",
+    "rhubarb\n",
+    "rioted\n",
+    "shinier\n",
+    "solaria\n",
+    "staple\n",
+    "tops\n",
+    "wide\n",
+    "winced\n",
     "```\n",
     "\n",
-    "14 words added;  6 directions\n",
+    "# Part 1\n",
     "\n",
-    "Present: apace cowhides crazies dock knows lived mown pears probed rhubarb rioted staple tops wide\n",
+    "Your wordsearch puzzle is given in [10-wordsearch.txt](10-wordsearch.txt). \n",
+    "\n",
+    "What is the total of the lengths of all the words present in the puzzle?"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "After you've solved the wordsearch and found all the words you can, you'll have some letters unused in any word. For the example wordsearch, once you've found the words, you're left with this:\n",
     "\n",
-    "Decoys: adapting bombing boor brick cackles carnal casino chaplets chump coaster coccyxes coddle collies creels crumbled cunt curds curled curlier deepen demeanor dicier dowses ensuing faddish fest fickler foaming gambol garoting gliding gristle grunts guts ibex impugns instants kielbasy lanyard loamier lugs market meanly minuend misprint mitts molested moonshot mucking oaks olives orgasmic pastrami perfect proceed puckered quashed refined regards retraces revel ridges ringlet scoff shinier siren solaria sprain sunder sunup tamped tapes thirds throw tiller times trains tranquil transfix typesets uric wariness welts whimsy winced winced\n",
+    "```\n",
+    "fhj.a....q\n",
+    "w....uq..v\n",
+    "i.a....h..\n",
+    "..e....i..\n",
+    "..........\n",
+    "....a.....\n",
+    "....p.f...\n",
+    "........s.\n",
+    ".i..h.npn.\n",
+    "b......okr\n",
+    "```\n",
+    "The letters remaining in the grid are `aaabeffhhhiiijknnoppqqrsuvw`. 9 of those letters are vowels. \n",
     "\n",
-    "Decoys: fickler, adapting, chump, foaming, molested, carnal, crumbled, guts, minuend, bombing, winced, coccyxes, solaria, shinier, cackles\n",
+    "# Part 2\n",
     "\n",
-    "All words: adapting, apace, bombing, cackles, carnal, chump, coccyxes, cowhides, crazies, crumbled, dock, fickler, foaming, guts, knows, lived, minuend, molested, mown, pears, probed, rhubarb, rioted, shinier, solaria, staple, tops, wide, winced\n",
+    "Your wordsearch puzzle is still given in [10-wordsearch.txt](10-wordsearch.txt).\n",
     "\n",
-    "Directions:  [('probed', '`(True, 3, 0, <Direction.down: 4>)`'), ('staple', '`(True, 9, 1, <Direction.right: 2>)`'), ('rioted', '`(True, 6, 7, <Direction.upleft: 5>)`'), ('cowhides', '`(True, 8, 3, <Direction.up: 3>)`'), ('tops', '`(True, 7, 4, <Direction.right: 2>)`'), ('knows', '`(True, 8, 2, <Direction.up: 3>)`'), ('lived', '`(True, 2, 4, <Direction.downright: 8>)`'), ('rhubarb', '`(True, 2, 9, <Direction.down: 4>)`'), ('crazies', '`(True, 7, 1, <Direction.up: 3>)`'), ('dock', '`(True, 8, 5, <Direction.up: 3>)`'), ('apace', '`(True, 5, 8, <Direction.up: 3>)`'), ('mown', '`(True, 0, 5, <Direction.right: 2>)`'), ('pears', '`(True, 0, 3, <Direction.downright: 8>)`'), ('wide', '`(True, 4, 4, <Direction.upright: 6>)`')]"
+    "How many vowels are left over after solving this puzzle?"
    ]
   },
   {