https://github.com/grachale/sudoku_solver
Implementation of sudoku solver in Prolog.
https://github.com/grachale/sudoku_solver
prolog sudoku-solver
Last synced: 2 months ago
JSON representation
Implementation of sudoku solver in Prolog.
- Host: GitHub
- URL: https://github.com/grachale/sudoku_solver
- Owner: grachale
- Created: 2023-08-13T12:25:01.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-08-13T12:27:49.000Z (over 1 year ago)
- Last Synced: 2025-01-13T01:20:42.142Z (4 months ago)
- Topics: prolog, sudoku-solver
- Language: Prolog
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Sudoku solver in Prolog
### The task assignment description.
Write a sudoku solver. At the entrance you will receive a game board with any number of pre-filled cells. Your task is to complete the Sudoku if there is a solution.
### Features were implemented.
Predicate solve_sudoku(+Matrix) finds (with usage of Brute Force algorithm) one possible solutution to any given correct 9x9 Sudoku and prints the solution.
In case input matrix does not follow at least one rule of original sudoku (each row, column and 3x3 square needs to be filled out with the numbers 1-9, without repeating any numbers within the row, column or square) fails.
No predicate uses constraint solvers or similar libraries.
## Example usage
Predicate solve_sudoku(+Matrix) takes a matrix 9x9 (represented as list of lists) and prints solved Sudoku if it exists, otherwise fails.
Tests were taken from https://www.sudokuonline.io and the most difficult one was created by author of code:
EASY LEVEL:
solve_sudoku(
[[0,0,0,2,6,0,7,0,1],
[6,8,0,0,7,0,0,9,0],
[1,9,0,0,0,4,5,0,0],
[8,2,0,1,0,0,0,4,0],
[0,0,4,6,0,2,9,0,0],
[0,5,0,0,0,3,0,2,8],
[0,0,9,3,0,0,0,7,4],
[0,4,0,0,5,0,0,3,6],
[7,0,3,0,1,8,0,0,0]
]).Output:
[4,3,5,2,6,9,7,8,1]
[6,8,2,5,7,1,4,9,3]
[1,9,7,8,3,4,5,6,2]
[8,2,6,1,9,5,3,4,7]
[3,7,4,6,8,2,9,1,5]
[9,5,1,7,4,3,6,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],
[7,3,0,0,0,0,0,5,1]
]).Output:
false.
Duplicate in a row:
solve_sudoku(
[[3,0,1,6,7,0,0,0,4],
[0,0,0,0,3,1,0,0,0],
[0,0,0,5,4,0,0,6,0],
[0,0,0,0,0,0,0,0,0],
[0,0,8,0,6,6,0,7,0],
[0,0,6,1,0,0,3,8,2],
[6,0,0,4,1,0,0,0,0],
[8,0,0,0,6,0,0,0,0],
[7,3,0,0,0,0,0,5,1]
]).Output:
false.