-
Notifications
You must be signed in to change notification settings - Fork 1
/
sudokill.py
59 lines (48 loc) · 1.75 KB
/
sudokill.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from ortools.constraint_solver import pywrapcp
from collections import defaultdict
def solve(clues):
solver = pywrapcp.Solver('sudokill')
bloc = defaultdict(list)
cell = [0] * 9
cols = defaultdict(list)
for i in range(9):
cell[i] = [0] * 9
for j in range(9):
# Define lower and upper bounds per cell
clue = clues[i][j]
lb, ub = (clue, clue) if clue else (1, 9)
cell[i][j] = solver.IntVar(lb, ub, 'cell[%i][%i]' % (i, j))
# Collect vars for rules for blocks and columns
bloc[(i // 3, j // 3)].append(cell[i][j])
cols[j].append(cell[i][j])
# Add constraints
for n in range(9):
# All elements in a row must be different and sum 45
solver.Add(solver.AllDifferent(cell[n]))
solver.Add(solver.Sum(cell[n]) == 45)
# All elements in a col must be different and sum 45
solver.Add(solver.AllDifferent(cols[n]))
solver.Add(solver.Sum(cols[n]) == 45)
# All elements in a block should be different and sum 45
for q in bloc:
solver.Add(solver.AllDifferent(bloc[q]))
solver.Add(solver.Sum(bloc[q]) == 45)
flat_cells = sum(cell, [])
db = solver.Phase(flat_cells,
solver.CHOOSE_FIRST_UNBOUND,
solver.ASSIGN_MIN_VALUE)
solver.NewSearch(db)
return solver, cell
def get_solution(solver, cells):
solver.NextSolution()
grid = [0] * 9
for i in range(9):
grid[i] = [cells[i][j].Value() for j in range(9)]
return grid
def print_grid(grid):
for i in range(9):
grid[i].insert(3, '|')
grid[i].insert(7, '|')
print(' '.join(map(str, grid[i])))
if i % 3 == 2:
print('-' * 21)