I'm working on the sudoku solver problem and heres what I got so far using a binary approach (dont bother trying to understand it its very crude)
class Solution:
def solveSudoku(self, board: List[List[str]]) -> None: #This is the main default function
board = self.solve(board)
def solve(self, board):
bitboards = [0, 0, 0, 0, 0, 0, 0, 0, 0]
bitboardmasks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
cubicledict = {(0, 0): 0, (0, 1): 0, (0, 2): 0, (0, 3): 1, (0, 4): 1, (0, 5): 1, (0, 6): 2, (0, 7): 2, (0, 8): 2,
(1, 0): 0, (1, 1): 0, (1, 2): 0, (1, 3): 1, (1, 4): 1, (1, 5): 1, (1, 6): 2, (1, 7): 2, (1, 8): 2,
(2, 0): 0, (2, 1): 0, (2, 2): 0, (2, 3): 1, (2, 4): 1, (2, 5): 1, (2, 6): 2, (2, 7): 2, (2, 8): 2,
(3, 0): 3, (3, 1): 3, (3, 2): 3, (3, 3): 4, (3, 4): 4, (3, 5): 4, (3, 6): 5, (3, 7): 5, (3, 8): 5,
(4, 0): 3, (4, 1): 3, (4, 2): 3, (4, 3): 4, (4, 4): 4, (4, 5): 4, (4, 6): 5, (4, 7): 5, (4, 8): 5,
(5, 0): 3, (5, 1): 3, (5, 2): 3, (5, 3): 4, (5, 4): 4, (5, 5): 4, (5, 6): 5, (5, 7): 5, (5, 8): 5,
(6, 0): 6, (6, 1): 6, (6, 2): 6, (6, 3): 7, (6, 4): 7, (6, 5): 7, (6, 6): 8, (6, 7): 8, (6, 8): 8,
(7, 0): 6, (7, 1): 6, (7, 2): 6, (7, 3): 7, (7, 4): 7, (7, 5): 7, (7, 6): 8, (7, 7): 8, (7, 8): 8,
(8, 0): 6, (8, 1): 6, (8, 2): 6, (8, 3): 7, (8, 4): 7, (8, 5): 7, (8, 6): 8, (8, 7): 8, (8, 8): 8}
# magic number f**kery
magicrows = [0x1FF000000000000000000, 0xFF8000000000000000, 0x7FC0000000000000, 0x3FE00000000000, 0x1FF000000000, 0xFF8000000, 0x7FC0000, 0x3FE00, 0x1FF]
magiccolumns = [0x100804020100804020100, 0x80402010080402010080, 0x40201008040201008040, 0x20100804020100804020, 0x10080402010080402010, 0x8040201008040201008,
0x4020100804020100804, 0x2010080402010080402, 0x1008040201008040201]
magiccubicles = [0x1C0E07000000000000000, 0x381C0E00000000000000, 0x70381C0000000000000, 0x381C0E00000000, 0x70381C0000000, 0xE07038000000, 0x70381C0, 0xE07038, 0x1C0E07]
boardh = []
LSBh = []
numh = []
def logicSolve(number, mask, columnorcubicleorrow, position):
for k in range(9):
possiblelocations = ~mask
possiblelocations &= columnorcubicleorrow[k]
if int.bit_count(possiblelocations) == 1:
position[(8 - int(((possiblelocations & -possiblelocations).bit_length() - 1) / 9))][(8 - int(((possiblelocations & -possiblelocations).bit_length() - 1) % 9))] = str(number)
return position
while bitboardmasks[9] != 2417851639229258349412351:
temp9 = bitboardmasks[9]
for indexI, arrayI in enumerate(board):
for indexJ, arrayJ in enumerate(board[indexI]):
if arrayJ != '.':
bitboardmasks[9] |= (1 << (8 - indexI) * 9 + (8 - indexJ))
for i in range(9):
if str(i + 1) == arrayJ:
bitboardmasks[i] |= (magicrows[indexI] | magiccolumns[indexJ] | magiccubicles[cubicledict[(indexI, indexJ)]] | bitboardmasks[9])
bitboards[i] |= (1 << (8 - indexI) * 9 + (8 - indexJ))
break
for i in range(9):
board = logicSolve(i + 1, bitboardmasks[i], magicrows, board)
board = logicSolve(i + 1, bitboardmasks[i], magiccolumns, board)
board = logicSolve(i + 1, bitboardmasks[i], magiccubicles, board)
andbb = bitboardmasks[8] & bitboardmasks[7] & bitboardmasks[6] & bitboardmasks[5] & bitboardmasks[4] & bitboardmasks[3] & bitboardmasks[2] & bitboardmasks[1] & bitboardmasks[0]
if bitboardmasks[9] != 2417851639229258349412351 and andbb == 2417851639229258349412351:
board = boardh[0]
bitboards = [0, 0, 0, 0, 0, 0, 0, 0, 0]
bitboardmasks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
bitboardmasks[numh[0]] |= (1 << LSBh[0])
if temp9 == bitboardmasks[9]:
if len(boardh) == 0:
tempboard = [x[:] for x in board]
boardh.append(tempboard)
numberfilled = [(int.bit_count(bitboardmasks[_] ^ 2417851639229258349412351) if int.bit_count(bitboardmasks[_] ^ 2417851639229258349412351) > 0 else 81) for _ in range(9)]
numfill = numberfilled.index(min(numberfilled))
possiblelocations = bitboardmasks[numfill] ^ 2417851639229258349412351
LSBindex = (possiblelocations & -possiblelocations).bit_length() - 1
LSBh.append(LSBindex), numh.append(numfill)
rowofindex, columnofindex, cubicleofindex = 8 - int(LSBindex / 9), 8 - int(LSBindex % 9), cubicledict[(8 - int(LSBindex / 9), 8 - int(LSBindex % 9))]
# fills the last empty cell and deduce from there
board[rowofindex][columnofindex] = str(numfill + 1)
return boardfor the input
[[".",".","9","7","4","8",".",".","."],["7",".",".",".",".",".",".",".","."],[".","2",".","1",".","9",".",".","."],[".",".","7",".",".",".","2","4","."],[".","6","4",".","1",".","5","9","."],[".","9","8",".",".",".","3",".","."],[".",".",".","8",".","3",".","2","."],[".",".",".",".",".",".",".",".","6"],[".",".",".","2","7","5","9",".","."]]the correct answer should be
[["5","1","9","7","4","8","6","3","2"],["7","8","3","6","5","2","4","1","9"],["4","2","6","1","3","9","8","7","5"],["3","5","7","9","8","6","2","4","1"],["2","6","4","3","1","7","5","9","8"],["1","9","8","5","2","4","3","6","7"],["9","7","5","8","6","3","1","2","4"],["8","3","2","4","9","1","7","5","6"],["6","4","1","2","7","5","9","8","3"]]and of course i used a more powerful external IDE to edit my code (plus I'm not a premium member so theres wait time for every test). In the IDE I entered:
from typing import List
class Solution:
def solveSudoku(self, board: List[List[str]]) -> None: #This is the main default function
self.board = self.solve(board)
def solve(self, board):
bitboards = [0, 0, 0, 0, 0, 0, 0, 0, 0]
bitboardmasks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
cubicledict = {(0, 0): 0, (0, 1): 0, (0, 2): 0, (0, 3): 1, (0, 4): 1, (0, 5): 1, (0, 6): 2, (0, 7): 2, (0, 8): 2,
(1, 0): 0, (1, 1): 0, (1, 2): 0, (1, 3): 1, (1, 4): 1, (1, 5): 1, (1, 6): 2, (1, 7): 2, (1, 8): 2,
(2, 0): 0, (2, 1): 0, (2, 2): 0, (2, 3): 1, (2, 4): 1, (2, 5): 1, (2, 6): 2, (2, 7): 2, (2, 8): 2,
(3, 0): 3, (3, 1): 3, (3, 2): 3, (3, 3): 4, (3, 4): 4, (3, 5): 4, (3, 6): 5, (3, 7): 5, (3, 8): 5,
(4, 0): 3, (4, 1): 3, (4, 2): 3, (4, 3): 4, (4, 4): 4, (4, 5): 4, (4, 6): 5, (4, 7): 5, (4, 8): 5,
(5, 0): 3, (5, 1): 3, (5, 2): 3, (5, 3): 4, (5, 4): 4, (5, 5): 4, (5, 6): 5, (5, 7): 5, (5, 8): 5,
(6, 0): 6, (6, 1): 6, (6, 2): 6, (6, 3): 7, (6, 4): 7, (6, 5): 7, (6, 6): 8, (6, 7): 8, (6, 8): 8,
(7, 0): 6, (7, 1): 6, (7, 2): 6, (7, 3): 7, (7, 4): 7, (7, 5): 7, (7, 6): 8, (7, 7): 8, (7, 8): 8,
(8, 0): 6, (8, 1): 6, (8, 2): 6, (8, 3): 7, (8, 4): 7, (8, 5): 7, (8, 6): 8, (8, 7): 8, (8, 8): 8}
# magic number fuckery
magicrows = [0x1FF000000000000000000, 0xFF8000000000000000, 0x7FC0000000000000, 0x3FE00000000000, 0x1FF000000000, 0xFF8000000, 0x7FC0000, 0x3FE00, 0x1FF]
magiccolumns = [0x100804020100804020100, 0x80402010080402010080, 0x40201008040201008040, 0x20100804020100804020, 0x10080402010080402010, 0x8040201008040201008,
0x4020100804020100804, 0x2010080402010080402, 0x1008040201008040201]
magiccubicles = [0x1C0E07000000000000000, 0x381C0E00000000000000, 0x70381C0000000000000, 0x381C0E00000000, 0x70381C0000000, 0xE07038000000, 0x70381C0, 0xE07038, 0x1C0E07]
boardh = []
LSBh = []
numh = []
def logicSolve(number, mask, columnorcubicleorrow, position):
for k in range(9):
possiblelocations = ~mask
possiblelocations &= columnorcubicleorrow[k]
if int.bit_count(possiblelocations) == 1:
position[(8 - int(((possiblelocations & -possiblelocations).bit_length() - 1) / 9))][(8 - int(((possiblelocations & -possiblelocations).bit_length() - 1) % 9))] = str(number)
return position
while bitboardmasks[9] != 2417851639229258349412351:
temp9 = bitboardmasks[9]
for indexI, arrayI in enumerate(board):
for indexJ, arrayJ in enumerate(board[indexI]):
if arrayJ != '.':
bitboardmasks[9] |= (1 << (8 - indexI) * 9 + (8 - indexJ))
for i in range(9):
if str(i + 1) == arrayJ:
bitboardmasks[i] |= (magicrows[indexI] | magiccolumns[indexJ] | magiccubicles[cubicledict[(indexI, indexJ)]] | bitboardmasks[9])
bitboards[i] |= (1 << (8 - indexI) * 9 + (8 - indexJ))
break
for i in range(9):
board = logicSolve(i + 1, bitboardmasks[i], magicrows, board)
board = logicSolve(i + 1, bitboardmasks[i], magiccolumns, board)
board = logicSolve(i + 1, bitboardmasks[i], magiccubicles, board)
andbb = bitboardmasks[8] & bitboardmasks[7] & bitboardmasks[6] & bitboardmasks[5] & bitboardmasks[4] & bitboardmasks[3] & bitboardmasks[2] & bitboardmasks[1] & bitboardmasks[0]
if bitboardmasks[9] != 2417851639229258349412351 and andbb == 2417851639229258349412351:
board = boardh[0]
bitboards = [0, 0, 0, 0, 0, 0, 0, 0, 0]
bitboardmasks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
bitboardmasks[numh[0]] |= (1 << LSBh[0])
if temp9 == bitboardmasks[9]:
if len(boardh) == 0:
tempboard = [x[:] for x in board]
boardh.append(tempboard)
numberfilled = [(int.bit_count(bitboardmasks[_] ^ 2417851639229258349412351) if int.bit_count(bitboardmasks[_] ^ 2417851639229258349412351) > 0 else 81) for _ in range(9)]
numfill = numberfilled.index(min(numberfilled))
possiblelocations = bitboardmasks[numfill] ^ 2417851639229258349412351
LSBindex = (possiblelocations & -possiblelocations).bit_length() - 1
LSBh.append(LSBindex), numh.append(numfill)
rowofindex, columnofindex, cubicleofindex = 8 - int(LSBindex / 9), 8 - int(LSBindex % 9), cubicledict[(8 - int(LSBindex / 9), 8 - int(LSBindex % 9))]
# fills the last empty cell and deduce from there
board[rowofindex][columnofindex] = str(numfill + 1)
return board
if __name__ =="__main__":
Solution().solveSudoku([[".",".","9","7","4","8",".",".","."],["7",".",".",".",".",".",".",".","."],[".","2",".","1",".","9",".",".","."],[".",".","7",".",".",".","2","4","."],[".","6","4",".","1",".","5","9","."],[".","9","8",".",".",".","3",".","."],[".",".",".","8",".","3",".","2","."],[".",".",".",".",".",".",".",".","6"],[".",".",".","2","7","5","9",".","."]])everything is in order in the IDE, yet somehow if I test my code on leetcode it spits out the wrong output, the return value of the solve() function seems to be correct, somehow the default function outputs a different value. the problem asks us not to return anything, but how can we check our answer if thats the case?