Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwintitus14 authored Feb 16, 2020
1 parent bc0d421 commit bc326d8
Showing 1 changed file with 260 additions and 0 deletions.
260 changes: 260 additions & 0 deletions sudoku.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "sudoku.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "K2JQb0LYi6qd",
"colab_type": "code",
"colab": {}
},
"source": [
"grid = [[8,0,0,0,0,0,0,0,0],\n",
" [0,0,3,6,0,0,0,0,0],\n",
" [0,7,0,0,9,0,2,0,0],\n",
" [0,5,0,0,0,7,0,0,0],\n",
" [0,0,0,0,4,5,7,0,0],\n",
" [0,0,0,1,0,0,0,3,0],\n",
" [0,0,1,0,0,0,0,6,8],\n",
" [0,0,8,5,0,0,0,1,0],\n",
" [0,9,0,0,0,0,4,0,0]]\n",
"# World's hardest sudoku puzzle (https://www.telegraph.co.uk/news/science/science-news/9359579/Worlds-hardest-sudoku-can-you-crack-it.html)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "nNRTyALKj_hz",
"colab_type": "code",
"outputId": "24559295-3dd3-4832-f13d-c62829a0d7f3",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 182
}
},
"source": [
"def show():\n",
" for i in grid:\n",
" print(i)\n",
"show()"
],
"execution_count": 70,
"outputs": [
{
"output_type": "stream",
"text": [
"[8, 0, 0, 0, 0, 0, 0, 0, 0]\n",
"[0, 0, 3, 6, 0, 0, 0, 0, 0]\n",
"[0, 7, 0, 0, 9, 0, 2, 0, 0]\n",
"[0, 5, 0, 0, 0, 7, 0, 0, 0]\n",
"[0, 0, 0, 0, 4, 5, 7, 0, 0]\n",
"[0, 0, 0, 1, 0, 0, 0, 3, 0]\n",
"[0, 0, 1, 0, 0, 0, 0, 6, 8]\n",
"[0, 0, 8, 5, 0, 0, 0, 1, 0]\n",
"[0, 9, 0, 0, 0, 0, 4, 0, 0]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "jJKttkAB9KfZ",
"colab_type": "code",
"outputId": "93253f02-2ec9-4b6f-a38d-84ee2d8adb40",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 182
}
},
"source": [
"import numpy as np\n",
"print(np.matrix(grid))"
],
"execution_count": 71,
"outputs": [
{
"output_type": "stream",
"text": [
"[[8 0 0 0 0 0 0 0 0]\n",
" [0 0 3 6 0 0 0 0 0]\n",
" [0 7 0 0 9 0 2 0 0]\n",
" [0 5 0 0 0 7 0 0 0]\n",
" [0 0 0 0 4 5 7 0 0]\n",
" [0 0 0 1 0 0 0 3 0]\n",
" [0 0 1 0 0 0 0 6 8]\n",
" [0 0 8 5 0 0 0 1 0]\n",
" [0 9 0 0 0 0 4 0 0]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "AGSAlO47_v3v",
"colab_type": "code",
"outputId": "b5f3c0b2-936c-4817-9234-74d2bb1a48a4",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"source": [
"grid[3][0]"
],
"execution_count": 72,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0"
]
},
"metadata": {
"tags": []
},
"execution_count": 72
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "nFbIPJlB9Qni",
"colab_type": "code",
"colab": {}
},
"source": [
"def possible(y, x, n):\n",
" \"\"\" Returns true if it's possible to insert n at x,y in the grid. \"\"\"\n",
" global grid\n",
" for i in range(9):\n",
" if grid[y][i] == n:\n",
" return False\n",
" if grid[i][x] == n:\n",
" return False\n",
" x0 = (x//3) * 3\n",
" y0 = (y//3) * 3\n",
" for i in range(x0,x0+3):\n",
" for j in range(y0, y0+3):\n",
" if grid[j][i] == n:\n",
" return False\n",
" return True"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "hkk-M7VDeACa",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "a2028729-1e62-4681-8e77-9805b98cfdc8"
},
"source": [
"possible(2,3,2)"
],
"execution_count": 74,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {
"tags": []
},
"execution_count": 74
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "v03hURIr9mFE",
"colab_type": "code",
"colab": {}
},
"source": [
"count = 0\n",
"def solve():\n",
" global grid\n",
" global count\n",
" for x in range(9):\n",
" for y in range(9):\n",
" if grid[y][x] == 0:\n",
" for i in range(1,10):\n",
" if possible(y,x,i):\n",
" grid[y][x] = i\n",
" count+=1\n",
" solve()\n",
" grid[y][x] = 0\n",
" return\n",
" print(np.matrix(grid))\n",
" print(count)\n",
" input(\"More?\")"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "evdYrs_QfwXF",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 219
},
"outputId": "02205d28-a437-476b-ded8-fa147e5fe599"
},
"source": [
"solve()"
],
"execution_count": 76,
"outputs": [
{
"output_type": "stream",
"text": [
"[[8 1 2 7 5 3 6 4 9]\n",
" [9 4 3 6 8 2 1 7 5]\n",
" [6 7 5 4 9 1 2 8 3]\n",
" [1 5 4 2 3 7 8 9 6]\n",
" [3 6 9 8 4 5 7 2 1]\n",
" [2 8 7 1 6 9 5 3 4]\n",
" [5 2 1 9 7 4 3 6 8]\n",
" [4 3 8 5 2 6 9 1 7]\n",
" [7 9 6 3 1 8 4 5 2]]\n",
"1897907\n",
"More?\n"
],
"name": "stdout"
}
]
}
]
}

0 comments on commit bc326d8

Please sign in to comment.