generated from github/codespaces-jupyter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* first ideas * first heuristics * more tweaks * first heuristic solver * adding jupyter book
- Loading branch information
1 parent
c85e1b0
commit e3b8d12
Showing
13 changed files
with
447 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"This notebook provides a simple sudoku solver. Let's start with a simple 4 (\\*\\*\\*\\*) star sudoku" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Parool Dinsdag 19 sept ****\n", | ||
"sudoku_grid = [\n", | ||
" [0, 6, 0, 0, 0, 0, 1, 9, 0],\n", | ||
" [0, 0, 2, 6, 1, 0, 0, 0, 4],\n", | ||
" [7, 0, 1, 0, 0, 0, 0, 0, 0],\n", | ||
" [0, 0, 0, 0, 7, 0, 0, 1, 0],\n", | ||
" [0, 0, 6, 0, 8, 3, 0, 0, 0],\n", | ||
" [5, 4, 0, 0, 6, 0, 0, 0, 3],\n", | ||
" [0, 8, 0, 0, 2, 7, 0, 3, 9],\n", | ||
" [0, 0, 0, 4, 0, 0, 0, 7, 8],\n", | ||
" [0, 0, 0, 0, 0, 0, 4, 0, 0]\n", | ||
"]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"And let's show the grid by importing a simple pretty printer. " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from sudoku.solvers import heuristic\n", | ||
"\n", | ||
"grid = heuristic.pencil_in_numbers(sudoku_grid)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"292" | ||
] | ||
}, | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"heuristic.simple_elimination(grid)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"0" | ||
] | ||
}, | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"heuristic.simple_elimination(grid)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"15" | ||
] | ||
}, | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"heuristic.hidden_single(grid)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#heuristic.hidden_single(grid)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[[[3, 4, 8],\n", | ||
" [6],\n", | ||
" [3, 4, 5, 8],\n", | ||
" [7],\n", | ||
" [3, 4, 5],\n", | ||
" [2, 4, 5, 8],\n", | ||
" [1],\n", | ||
" [9],\n", | ||
" [2, 5, 7]],\n", | ||
" [[3, 8, 9], [3, 5, 9], [2], [6], [1], [5, 8, 9], [7], [5, 8], [4]],\n", | ||
" [[7],\n", | ||
" [3, 5, 9],\n", | ||
" [1],\n", | ||
" [2, 3, 5, 8, 9],\n", | ||
" [3, 4, 5, 9],\n", | ||
" [2, 4, 5, 8, 9],\n", | ||
" [2, 3, 5, 6, 8],\n", | ||
" [2, 5, 6, 8],\n", | ||
" [2, 5, 6]],\n", | ||
" [[2, 3, 8, 9],\n", | ||
" [2, 3, 9],\n", | ||
" [3, 8, 9],\n", | ||
" [2, 5, 9],\n", | ||
" [7],\n", | ||
" [4],\n", | ||
" [2, 5, 6, 8, 9],\n", | ||
" [1],\n", | ||
" [2, 5, 6]],\n", | ||
" [[1, 2, 9],\n", | ||
" [1, 2, 7, 9],\n", | ||
" [6],\n", | ||
" [1, 2, 5, 9],\n", | ||
" [8],\n", | ||
" [3],\n", | ||
" [2, 5, 7, 9],\n", | ||
" [4],\n", | ||
" [2, 5, 7]],\n", | ||
" [[5], [4], [7, 8, 9], [1, 2, 9], [6], [1, 2, 9], [2, 7, 8, 9], [2, 8], [3]],\n", | ||
" [[1, 4, 6], [8], [4, 5], [1, 5], [2], [7], [5, 6], [3], [9]],\n", | ||
" [[1, 2, 3, 6, 9],\n", | ||
" [1, 2, 3, 5, 9],\n", | ||
" [3, 5, 9],\n", | ||
" [4],\n", | ||
" [3, 5, 9],\n", | ||
" [1, 5, 6, 9],\n", | ||
" [2, 5, 6],\n", | ||
" [7],\n", | ||
" [8]],\n", | ||
" [[1, 2, 3, 6, 9],\n", | ||
" [1, 2, 3, 5, 7, 9],\n", | ||
" [3, 5, 7, 9],\n", | ||
" [1, 3, 5, 8, 9],\n", | ||
" [3, 5, 9],\n", | ||
" [1, 5, 6, 8, 9],\n", | ||
" [4],\n", | ||
" [2, 5, 6],\n", | ||
" [1]]]" | ||
] | ||
}, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"grid" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"False" | ||
] | ||
}, | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from sudoku.solvers import heuristic\n", | ||
"\n", | ||
"heuristic.solve(grid)" | ||
] | ||
} | ||
], | ||
"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.11.6" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,31 @@ | ||
def count_nonzero(grid): | ||
# just get all coordinates | ||
all_grid = [(i, j) for i in range(9) for j in range(9)] | ||
|
||
def n_nonzero(grid): | ||
n = 0 | ||
for row in range(9): | ||
for col in range(9): | ||
if grid[row][col] != 0: | ||
n += 1 | ||
for (i, j) in all_grid: | ||
if grid[i][j] != 0: | ||
n += 1 | ||
return n | ||
|
||
|
||
def is_complete(grid): | ||
for row in range(9): | ||
for col in range(9): | ||
if grid[row][col] == 0: | ||
return False | ||
def is_solved(grid): | ||
for (i, j) in all_grid: | ||
if isinstance(grid[i][j], list) and len(grid[i][j]) != 1: | ||
return False | ||
elif grid[i][j] == 0 : | ||
return False | ||
return True | ||
|
||
|
||
def find_empty_cell(grid): | ||
for row in range(9): | ||
for col in range(9): | ||
if grid[row][col] == 0: | ||
return row, col | ||
for (i, j) in all_grid: | ||
if grid[i][j] == 0: | ||
return i, j | ||
return None, None | ||
|
||
|
||
def flatten(grid): | ||
for (i, j) in all_grid: | ||
if isinstance(grid[i][j], list) and len(grid[i][j]) == 1: | ||
grid[i][j] = grid[i][j][0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.