예제 #1
0
 def solve_pair(self, puzzle, solution):
     puzzle_str = gen3.array_to_str(puzzle)
     solver = SudokuSolver(puzzle_str, grid_class=StringGrid)
     solver.run()
     computed_solution = str(solver.grid.layer)
     solution_str = gen3.array_to_str(solution)
     self.assertEqual(computed_solution, solution_str)
예제 #2
0
 def test_solve(self):
     grid = '1638.5.7...8.4..65..5..7..845..82.393.1..' \
            '..4.7........839.5....6.42..59.....93.81'
     solver = SudokuSolver(grid, grid_class=StringGrid)
     self.assertEquals(solver.grid.completed, False)
     solver.run()
     self.assertEquals(solver.grid.completed, True)
예제 #3
0
 def test_use_backtracking(self):
     grid = '123456.8957813962449687215395238146764129' \
            '7835387564291719623548864915372235748916'
     solver = SudokuSolver(grid, grid_class=StringGrid,
                           solvers=[])
     self.assertEquals(solver.grid.completed, False)
     solver.run()
     self.assertEquals(solver.grid.completed, True)
예제 #4
0
 def test_unsolvable_without_backtracking(self):
     grid = '.5.9.7.4.1..8.2..5.7..1..3...4...8..72...' \
            '..16..1...2...9..8..2.2..6.9..1.1.7.3.8.'
     solver = SudokuSolver(grid, grid_class=StringGrid,
                           backtracking_solver_class=None)
     self.assertEquals(solver.grid.completed, False)
     solver.run()
     self.assertEquals(solver.grid.completed, False)
    def get_solved_sudoku(self):
        ''' SudokuSolver takes the sudoku as string
         Example : [[1, 0, 3], [0, 2, 1]] = "102021 or 1.2.21" '''

        solver = SudokuSolver(''.join([
            str(cell_value) for row in self.row_wise_sudoku
            for cell_value in row
        ]),
                              grid_class=StringGrid)
        solver.run()
        self.solved_string = str(solver)

        ansi_escape = re.compile(
            r'''
                        \x1B    # ESC
                        [@-_]   # 7-bit C1 Fe
                        [0-?]*  # Parameter bytes
                        [ -/]*  # Intermediate bytes
                        [@-~]   # Final byte
                    ''', re.VERBOSE)

        result = ansi_escape.sub('', self.solved_string)
        print(result)

        self.row_wise_solved_sudoku = list()
        self.row_elements = []

        for value in result:
            try:
                cell_value = int(value)
                self.row_elements.append(cell_value)
                if len(self.row_elements) == 9:
                    self.row_wise_solved_sudoku.append(self.row_elements)
                    self.row_elements = []
            except:
                continue
        print(self.row_wise_solved_sudoku)
        return self.row_wise_solved_sudoku
예제 #6
0
				for d in (digits & get_digits_in_row(pp,i)&get_digits_in_column(pp,j)&get_digits_in_box(pp,i,j)):
					pp = set_pos(pp,i,j,d)
					ppp = solve(pp)
					if ppp is not None:
						return ppp
					pp = set_pos(pp,i,j,0)
	return None

from sudokulib.grid import StringGrid
from sudokulib.solver import SudokuSolver


x = 0

for grid in puzzles:
	s = SudokuSolver(grid, grid_class=StringGrid)
	s.run()
	r = list(grid[:3])
	for i in range(3):
		if r[i]=='0':
			r[i]=s.grid.data_solution[i]
	x+=int("".join(r))
	print([x,r])



#file parsers
def get_sudoku_from_file(fn):
	with open(fn, "rtU") as f:
		puzzle=""
		for line in f: