(Got this from Moritz Lenz (moritz.lenz@gmail.com) 10 years ago?)
I refactored in attempt to make more "pythonic", "DRY" and to fit BigSdk
No Good without perm.py - 1298 line PERMS list - willing to post - if NOT too big for forum?
- Code: Select all
"""
canon.py see end of file for comments
"""
# <editor-fold desc="python imports"
import sys
# </editor-fold>
# <editor-fold desc="local imports"
import settings as g
import sigs
from perm import PERMS
# </editor-fold>
# <editor-fold desc="globals"
BP = g.BREAK_POINT
# </editor-fold>
next_map = 1
_map = [0, -1, -1, -1, -1, -1, -1, -1, -1, -1]
def create(sudoku_string):
try:
global next_map
global _map
puzzle_81 = sudoku_string.replace('.', '0')
data = [[0 for __ in range(9)] for _ in range(9)]
for i in range(81):
data[i % 9][i // 9] = int(puzzle_81[i])
minlex = [9 for _ in range(81)]
transposed = [[0 for __ in range(9)] for _ in range(9)]
for y in range(9):
for x in range(9):
transposed[y][x] = data[x][y]
for i in range(1296):
for j in range(1296):
compare_and_update(minlex, data, i, j)
compare_and_update(minlex, transposed, i, j)
canon = ''.join(str(digit) for digit in minlex)
return canon
except Exception as e:
sigs.logger_except.exception(e)
sys.exit()
def compare_and_update(least, tmp, i, j):
try:
global next_map
global _map
count = 0
for y in range(9):
for x in range(9):
a = tmp[PERMS[i][x]][PERMS[j][y]]
if _map[a] == -1:
_map[a] = next_map
next_map += 1
if _map[a] < least[count]:
perm_copy(tmp, least, i, j)
return
elif _map[a] > least[count]:
return
count += 1
return
except Exception as e:
sigs.logger_except.exception(e)
sys.exit()
def perm_copy(source, dest, i, j):
try:
global next_map
global _map
for k in range(81):
a = source[PERMS[i][k % 9]][PERMS[j][k // 9]]
if _map[a] == -1:
_map[a] = next_map
next_map += 1
dest[k] = _map[a]
return
except Exception as e:
sigs.logger_except.exception(e)
sys.exit()
if __name__ == '__main__':
arto_inkala_givens = '800000000003600000070090200050007000000045700000100030001000068008500010090000400'
arto_inkala_solved = '812753649943682175675491283154237896369845721287169534521974368438526917796318452'
print(f'min_lex_canon = {create(arto_inkala_givens)}')
else:
file = __file__
print(f'importing {file} ')