Check the "try it out" section for the live demo, on Replit. Direct Replit Link

🐍 The Python Code

# 0 is empty
board = [
    [5, 3, 0, 0, 7, 0, 0, 0, 0],
    [6, 0, 0, 1, 9, 5, 0, 0, 0],
    [0, 9, 8, 0, 0, 0, 0, 6, 0],
    [8, 0, 0, 0, 6, 0, 0, 0, 3],
    [4, 0, 0, 8, 0, 3, 0, 0, 1],
    [7, 0, 0, 0, 2, 0, 0, 0, 6],
    [0, 6, 0, 0, 0, 0, 2, 8, 0],
    [0, 0, 0, 4, 1, 9, 0, 0, 5],
    [0, 0, 0, 0, 8, 0, 0, 0, 0]
]

def findNextEmptySquare(board):
    for row in range(len(board)):
        for col in range(len(board[row])):
            # Iter through rows and columns, then check if square isn't filled
            if board[row][col] == 0:
                return (row, col)
    # All squares are filled, return None left
    return None

def notNumInRowColumnOrBox(board, possibleValue, positionOnBoard):
    # Check row
    for row in range(len(board[0])):
        if board[positionOnBoard[0]][row] == possibleValue and positionOnBoard[1] != row:
            return False
    # Check column
    for col in range(len(board)):
        if board[col][positionOnBoard[1]] == possibleValue and positionOnBoard[0] != col:
            return False
    # Check box
    # Get which box the point is in
    box_x = positionOnBoard[1] // 3
    box_y = positionOnBoard[0] // 3
    # Iter through all 9 values of box
    for i in range(box_y*3, box_y*3 + 3):
        for j in range(box_x * 3, box_x*3 + 3):
            # Check if number is not in box
            if board[i][j] == possibleValue and (i,j) != positionOnBoard:
                return False
    # All tests passed
    return True

def solveSudoku(board):
    emptySquare = findNextEmptySquare(board)
    if not emptySquare:
        # All squares are filled
        return True
    else:
        row, col = emptySquare
    for possibleValue in range(1,10):
        # Iter through all possible values
        if notNumInRowColumnOrBox(board, possibleValue, (row, col)):
            # Set value, then check is sudoku will solve with it
            board[row][col] = possibleValue
            if solveSudoku(board):
                return True
            else:
                # Sudoku rejects the number, reset it
                board[row][col] = 0
    # Sudoku is bad
    return False

def printBoard(board):
    for row in board:
        for number in row:
            # Print number and a space at the end
            print(number, end=" ")
        # New line for new row
        print()

printBoard(board)
if solveSudoku(board):
    printBoard(board)
else:
    print("Sudoku is bad.")

🚧 What It Does

This is a program which solves sudoku's. This even works on the world's hardest sudoku.

🛠️ How I Built It

This program uses the old-style guess and check technique. It uses plain python, so the program can be ported to any language. I intentionally used long variable/function names and kept a lot of comments. Instead of explaining it through text, python is very readable, so you can read the code and comments.

👨🏾‍💻 Challenges I Ran Into

When I first started this project, I had the assumption some data model or AI had to be created to solve it. Because of python's fast run time, I could use guess and check, and it works out fine.

📙 What I Learned

I have never made anything like a puzzle solver, so this was a new experience for me. In all of my projects, I try to find hacky ways to solve problems, instead of developing something advanced. That's what hacking is about, after all, finding ways to hack to success.

🔜 What's Next For Sudoku Solver

I will add a method to take and upload a picture. I am already planning it out, it will use opencv and a learning model to get the numbers, then just solve it.

Built With

Share this project:

Updates

Submission history