Created
June 28, 2020 13:36
-
-
Save Klepvink/0cc67bad3709752f00468880becbdf05 to your computer and use it in GitHub Desktop.
[HvA] Automatic sudoku solver
This file contains hidden or 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
import time | |
from selenium import webdriver | |
from selenium.webdriver.common.keys import Keys | |
from selenium.common.exceptions import NoSuchElementException | |
idRow = [["00", "01", "02", "03", "04", "05", "06", "07", "08"], | |
["10", "11", "12", "13", "14", "15", "16", "17", "18"], | |
["20", "21", "22", "23", "24", "25", "26", "27", "28"], | |
["30", "31", "32", "33", "34", "35", "36", "37", "38"], | |
["40", "41", "42", "43", "44", "45", "46", "47", "48"], | |
["50", "51", "52", "53", "54", "55", "56", "57", "58"], | |
["60", "61", "62", "63", "64", "65", "66", "67", "68"], | |
["70", "71", "72", "73", "74", "75", "76", "77", "78"], | |
["80", "81", "82", "83", "84", "85", "86", "87", "88"]] | |
puzzle = [] | |
def findNextCellToFill(grid, i, j): | |
for x in range(i, 9): | |
for y in range(j, 9): | |
if grid[x][y] == 0: | |
return x, y | |
for x in range(0, 9): | |
for y in range(0, 9): | |
if grid[x][y] == 0: | |
return x, y | |
return -1, -1 | |
def isValid(grid, i, j, e): | |
rowOk = all([e != grid[i][x] for x in range(9)]) | |
if rowOk: | |
columnOk = all([e != grid[x][j] for x in range(9)]) | |
if columnOk: | |
secTopX, secTopY = 3 * (i//3), 3 * (j//3) | |
for x in range(secTopX, secTopX+3): | |
for y in range(secTopY, secTopY+3): | |
if grid[x][y] == e: | |
return False | |
return True | |
return False | |
def solveSudoku(grid, i=0, j=0): | |
i, j = findNextCellToFill(grid, i, j) | |
if i == -1: | |
return True | |
for e in range(1, 10): | |
if isValid(grid, i, j, e): | |
grid[i][j] = e | |
if solveSudoku(grid, i, j): | |
return True | |
grid[i][j] = 0 | |
return False | |
def checkIfSpanExists(xPath): | |
try: | |
value = driver.find_element_by_xpath(xPath).text | |
except NoSuchElementException: | |
value = 0 | |
return int(value) | |
def checkIfInputExists(xPath, number): | |
try: | |
driver.find_element_by_xpath(xPath).send_keys(number) | |
except NoSuchElementException: | |
pass | |
driver = webdriver.Chrome() | |
driver.get( | |
"https://testdrive-archive.azurewebsites.net/Performance/Sudoku/Default.html") | |
for list in idRow: | |
row = [] | |
for id in list: | |
cssPath = '//*[@id="' + id + '"]/span' | |
row.append(checkIfSpanExists(cssPath)) | |
puzzle.append(row) | |
solveSudoku(puzzle) | |
for list in puzzle: | |
for i in list: | |
listIndex = puzzle.index(list) | |
index = list.index(i) | |
cssPath = '//*[@id="' + idRow[listIndex][index] + '"]/input' | |
checkIfInputExists(cssPath, i) | |
input("Press enter to close this window.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment