# File: main.py
# Author: Shawn Wilkinson

# Imports
import time

# Welcome Message
print("Welcome to Sudoku-Solver.")

# -- Main --

# Create two 9 x 9 Boards of Zeroes
board = [[0 for col in range(9)] for row in range(9)]
hints = [['' for col in range(9)] for row in range(9)]

# Sample Data - Set 1
board[0][5] = 1
board[0][6] = 5
board[0][7] = 6
board[0][8] = 3

board[1][2] = 7
board[1][3] = 5
board[1][5] = 3

board[2][3] = 4
board[2][4] = 2
board[2][5] = 8
board[2][7] = 7

# Sample Data - Set 2
board[3][1] = 3
board[3][3] = 9
board[3][4] = 7
board[3][5] = 4
board[3][6] = 6
board[3][7] = 2
board[3][8] = 1

board[4][1] = 9
board[4][2] = 1
board[4][3] = 2
board[4][8] = 4

board[5][3] = 8
board[5][6] = 3
board[5][7] = 9

# Sample Data - Set 3
board[6][1] = 2
board[6][3] = 6
board[6][4] = 5
board[6][8] = 8

board[7][4] = 8
board[7][6] = 4
board[7][7] = 1
board[7][8] = 6

board[8][3] = 1
board[8][5] = 9
board[8][6] = 2
board[8][7] = 5
board[8][8] = 7

# Fill Hints on Empty Spaces
for x in range(9):
    for y in range(9):
        if board[x][y] == 0:
            hints[x][y] = '123456789'

# Infinite Solving Loop
while 1:
    # Eliminate Horizontal Hint Duplicates
    for x in range(9):
        # Create an Empty List
        list = []
        # Get Values for That Row and Add to List
        for y in range(9):
            if board[x][y] != 0:
                list.append(board[x][y])
        # Loop through and remove that row from hints
        for y in range(9):
            for chr in list:
                hints[x][y] = hints[x][y].replace(str(chr),'')

    # Eliminate Vertical Hint Duplicates
    for x in range(9):
        # Create an Empty List
        list = []
        # Get Values for That Row and Add to List
        for y in range(9):
            if board[y][x] != 0:
                list.append(board[y][x])
        # Loop through and remove that row from hints
        for y in range(9):
            for chr in list:
                hints[y][x] = hints[y][x].replace(str(chr),'')
    
    # Sub-squares Function
    def subSquare(square):
        list = []
        if square == 0:
            for x in range(3):
                for y in range(3):
                    if board[y][x] != 0:
                        list.append(board[y][x])
                for y in range(3):
                    for chr in list:
                        hints[y][x] = hints[y][x].replace(str(chr),'')
        elif square == 1:
            for x in range(3,6):
                for y in range(3):
                    if board[y][x] != 0:
                        list.append(board[y][x])
                for y in range(3):
                    for chr in list:
                        hints[y][x] = hints[y][x].replace(str(chr),'')
        elif square == 2:
             for x in range(6,9):
                for y in range(3):
                    if board[y][x] != 0:
                        list.append(board[y][x])
                for y in range(3):
                    for chr in list:
                        hints[y][x] = hints[y][x].replace(str(chr),'')
        
        elif square == 3:
            for x in range(3):
                for y in range(3,6):
                    if board[y][x] != 0:
                        list.append(board[y][x])
                for y in range(3,6):
                    for chr in list:
                        hints[y][x] = hints[y][x].replace(str(chr),'')
        elif square == 4:
            for x in range(3,6):
                for y in range(3,6):
                    if board[y][x] != 0:
                        list.append(board[y][x])
                for y in range(3,6):
                    for chr in list:
                        hints[y][x] = hints[y][x].replace(str(chr),'')
        elif square == 5:
             for x in range(6,9):
                for y in range(3,6):
                    if board[y][x] != 0:
                        list.append(board[y][x])
                for y in range(3,6):
                    for chr in list:
                        hints[y][x] = hints[y][x].replace(str(chr),'')
                        
        elif square == 6:
            for x in range(3):
                for y in range(6,9):
                    if board[y][x] != 0:
                        list.append(board[y][x])
                for y in range(6,9):
                    for chr in list:
                        hints[y][x] = hints[y][x].replace(str(chr),'')
        elif square == 7:
            for x in range(3,6):
                for y in range(6,9):
                    if board[y][x] != 0:
                        list.append(board[y][x])
                for y in range(6,9):
                    for chr in list:
                        hints[y][x] = hints[y][x].replace(str(chr),'')
        elif square == 8:
             for x in range(6,9):
                for y in range(6,9):
                    if board[y][x] != 0:
                        list.append(board[y][x])
                for y in range(6,9):
                    for chr in list:
                        hints[y][x] = hints[y][x].replace(str(chr),'')
            

    # Eliminate Sub-squares Hint Duplicates
    for i in range(9):
        subSquare(i)

    # Solve Single Hints
    for x in range(9):
        for y in range(9):
            if len(hints[x][y]) == 1: 
                # Say Solved and Location
                print("Solved:", (x+1), ",", (y+1))
                # Set Hint to Board
                board[x][y] = int(hints[x][y])
                # Clear Hint
                hints[x][y] = ''

    # Check For a Solved Board
    exitLoop = True
    for x in range(9):
            for y in range(9):
                    # If a non-blank value is found then don't exit the loop
                    if board[x][y] != 0:
                            exitLoop = False
                    # else: exit loop
    if exitLoop:
            break # Break infinite while loop


    # Pause
    time.sleep(1)
    break

# Print Board and Hints
for row in board:
    print(row)
for row in hints:
    print(row)