{ "metadata": { "name": "", "signature": "sha256:2513cb86760ebf975b3dce5f7884ee5e23c13f94e20cc9af8fc245f251455fda" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Project Euler problem 11\n", "Find the largest product of four adjacent numbers in the grid." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Constants" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ROWS = COLUMNS = 20\n", "SECTION_LEN = 4" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 97 }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Data structure\n", "Convert the text of the numbers into a 2d array of integers.\n", "\n", "(Alterntive data structures include a 1d list of integers, or a dict with keys of (r, c) pairs.)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "GRID_STRING = \"\"\"08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08\n", "49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00\n", "81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65\n", "52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91\n", "22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80\n", "24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50\n", "32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70\n", "67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21\n", "24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72\n", "21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95\n", "78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92\n", "16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57\n", "86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58\n", "19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40\n", "04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66\n", "88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69\n", "04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36\n", "20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16\n", "20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54\n", "01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48\"\"\"\n", "\n", "GRID_LIST = [int(n) for n in GRID_STRING.split()]\n", "GRID = [GRID_LIST[i:i+COLUMNS] for i in range(0, ROWS * COLUMNS, COLUMNS)]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 98 }, { "cell_type": "code", "collapsed": false, "input": [ "for row in GRID:\n", " print(row)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8]\n", "[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0]\n", "[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65]\n", "[52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91]\n", "[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80]\n", "[24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50]\n", "[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70]\n", "[67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21]\n", "[24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72]\n", "[21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95]\n", "[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92]\n", "[16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57]\n", "[86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58]\n", "[19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40]\n", "[4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66]\n", "[88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69]\n", "[4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36]\n", "[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16]\n", "[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54]\n", "[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]\n" ] } ], "prompt_number": 99 }, { "cell_type": "markdown", "metadata": {}, "source": [ "#Directions\n", "What lines do we examine? Each number paricipates in up to 8 \u00d7 4 = 32 lines (fewer near the edges), but we can use the fact that multipication is commutative to only examine four lines that start at a number.\n", "\n", "`directions` stores those directions, and how to move in the direction." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Directions, as the pair (difference-in-row, difference-in-column)\n", "DIRECTIONS = {'N': (-1, 0), 'NW': (-1, -1), 'W': (0, -1), 'SW': (1, -1)}" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 20 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Finding the right numbers\n", "Given a starting position and a direction, find the right numbers.\n", "\n", "### Question:\n", "Should we worry if the request goes out of the bounds of the grid?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def numbers(row, column, direction):\n", " nums = []\n", " dr, dc = DIRECTIONS[direction]\n", " for _ in range(SECTION_LEN):\n", " nums.append(GRID[row][column])\n", " row += dr\n", " column += dc\n", " return nums" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 71 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Test it." ] }, { "cell_type": "code", "collapsed": false, "input": [ "numbers(0, 3, 'W')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 72, "text": [ "[97, 22, 2, 8]" ] } ], "prompt_number": 72 }, { "cell_type": "code", "collapsed": false, "input": [ "numbers(3, 0, 'N')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 73, "text": [ "[52, 81, 49, 8]" ] } ], "prompt_number": 73 }, { "cell_type": "code", "collapsed": false, "input": [ "numbers(3, 3, 'NW')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 74, "text": [ "[23, 31, 49, 8]" ] } ], "prompt_number": 74 }, { "cell_type": "code", "collapsed": false, "input": [ "numbers(3, 3, 'SW')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 75, "text": [ "[23, 16, 47, 32]" ] } ], "prompt_number": 75 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Product of a list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def product(ns):\n", " p = 1\n", " for n in ns:\n", " p *= n\n", " return p" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "product(numbers(0, 3, 'W'))" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 26, "text": [ "34144" ] } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "97 * 22 * 2 * 8" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 27, "text": [ "34144" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What directions don't take us out outside of the boundaries?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def valid_direction_explicit(row, column, direction):\n", " if direction == 'N' and row >= SECTION_LEN -1:\n", " return True\n", " elif direction == 'W' and column >= SECTION_LEN -1:\n", " return True\n", " elif direction == 'NW' and row >= SECTION_LEN -1 and column >= SECTION_LEN -1:\n", " return True\n", " elif direction == 'SW' and row + SECTION_LEN <= ROWS and column >= SECTION_LEN -1:\n", " return True\n", " else:\n", " return False" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 63 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction_explicit(0, 0, 'N')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": [ "False" ] } ], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction_explicit(5, 5, 'N')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 35, "text": [ "True" ] } ], "prompt_number": 35 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction_explicit(5, 5, 'NW')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 36, "text": [ "True" ] } ], "prompt_number": 36 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction_explicit(5, 5, 'W')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 37, "text": [ "True" ] } ], "prompt_number": 37 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction_explicit(5, 5, 'SW')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 38, "text": [ "True" ] } ], "prompt_number": 38 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction_explicit(17, 5, 'SW')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 61, "text": [ "False" ] } ], "prompt_number": 61 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction_explicit(16, 5, 'SW')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 64, "text": [ "True" ] } ], "prompt_number": 64 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction_explicit(2, 2, 'NW')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 60, "text": [ "False" ] } ], "prompt_number": 60 }, { "cell_type": "code", "collapsed": false, "input": [ "def valid_direction(row, column, direction):\n", " dr, dc = DIRECTIONS[direction]\n", " end_row = row + dr * (SECTION_LEN -1)\n", " end_col = column + dc * (SECTION_LEN -1)\n", " if end_row >= 0 and end_row < ROWS and end_col >= 0 and end_col < COLUMNS:\n", " return True\n", " else:\n", " return False" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 79 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction(0, 0, 'N')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 80, "text": [ "False" ] } ], "prompt_number": 80 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction(3, 3, 'N')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 81, "text": [ "True" ] } ], "prompt_number": 81 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction(3, 3, 'NW')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 82, "text": [ "True" ] } ], "prompt_number": 82 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction(3, 3, 'W')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 83, "text": [ "True" ] } ], "prompt_number": 83 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction(3, 3, 'SW')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 84, "text": [ "True" ] } ], "prompt_number": 84 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction(17, 17, 'N')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 85, "text": [ "True" ] } ], "prompt_number": 85 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction(17, 17, 'NW')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 86, "text": [ "True" ] } ], "prompt_number": 86 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction(17, 17, 'W')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 87, "text": [ "True" ] } ], "prompt_number": 87 }, { "cell_type": "code", "collapsed": false, "input": [ "valid_direction(17, 17, 'SW')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 89, "text": [ "False" ] } ], "prompt_number": 89 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Now to solve the problem" ] }, { "cell_type": "code", "collapsed": false, "input": [ "best_product = 0\n", "for row in range(ROWS):\n", " for column in range(COLUMNS):\n", " for direction in DIRECTIONS:\n", " if valid_direction(row, column, direction):\n", " this_product = product(numbers(row, column, direction))\n", " if this_product > best_product:\n", " best_product = this_product\n", "best_product" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 94, "text": [ "70600674" ] } ], "prompt_number": 94 }, { "cell_type": "code", "collapsed": false, "input": [ "max(product(numbers(r, c, d)) \n", " for r in range(ROWS) \n", " for c in range(COLUMNS) \n", " for d in DIRECTIONS \n", " if valid_direction(r, c, d))" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 101, "text": [ "70600674" ] } ], "prompt_number": 101 }, { "cell_type": "markdown", "metadata": {}, "source": [ "# All the code in one place" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ROWS = COLUMNS = 20\n", "SECTION_LEN = 4\n", "\n", "GRID_STRING = \"\"\"08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08\n", "49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00\n", "81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65\n", "52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91\n", "22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80\n", "24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50\n", "32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70\n", "67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21\n", "24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72\n", "21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95\n", "78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92\n", "16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57\n", "86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58\n", "19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40\n", "04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66\n", "88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69\n", "04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36\n", "20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16\n", "20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54\n", "01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48\"\"\"\n", "\n", "GRID_LIST = [int(n) for n in GRID_STRING.split()]\n", "GRID = [GRID_LIST[i:i+COLUMNS] for i in range(0, ROWS * COLUMNS, COLUMNS)]\n", "\n", "# Directions, as the pair (difference-in-row, difference-in-column)\n", "DIRECTIONS = {'N': (-1, 0), 'NW': (-1, -1), 'W': (0, -1), 'SW': (1, -1)}\n", "\n", "def numbers(row, column, direction):\n", " nums = []\n", " dr, dc = DIRECTIONS[direction]\n", " for _ in range(SECTION_LEN):\n", " nums.append(GRID[row][column])\n", " row += dr\n", " column += dc\n", " return nums\n", "\n", "def product(ns):\n", " p = 1\n", " for n in ns:\n", " p *= n\n", " return p\n", "\n", "def valid_direction(row, column, direction):\n", " dr, dc = DIRECTIONS[direction]\n", " end_row = row + dr * (SECTION_LEN -1)\n", " end_col = column + dc * (SECTION_LEN -1)\n", " if end_row >= 0 and end_row < ROWS and end_col >= 0 and end_col < COLUMNS:\n", " return True\n", " else:\n", " return False\n", "\n", "max(product(numbers(r, c, d)) \n", " for r in range(ROWS) \n", " for c in range(COLUMNS) \n", " for d in DIRECTIONS \n", " if valid_direction(r, c, d)) " ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 102, "text": [ "70600674" ] } ], "prompt_number": 102 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }