|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2010-2022 Google LLC |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""This model implements a sudoku solver.""" |
| 16 | + |
| 17 | +from ortools.sat.python import cp_model |
| 18 | + |
| 19 | + |
| 20 | +def solve_sudoku(): |
| 21 | + """Solves the sudoku problem with the CP-SAT solver.""" |
| 22 | + # Create the model. |
| 23 | + model = cp_model.CpModel() |
| 24 | + |
| 25 | + cell_size = 3 |
| 26 | + line_size = cell_size**2 |
| 27 | + line = list(range(0, line_size)) |
| 28 | + cell = list(range(0, cell_size)) |
| 29 | + |
| 30 | + initial_grid = [ |
| 31 | + [0, 0, 0, 0, 0, 0, 0, 0, 1], |
| 32 | + [0, 0, 0, 0, 0, 0, 0, 2, 3], |
| 33 | + [0, 0, 4, 0, 0, 5, 0, 0, 0], |
| 34 | + [0, 0, 0, 1, 0, 0, 0, 0, 0], |
| 35 | + [0, 0, 0, 0, 3, 0, 6, 0, 0], |
| 36 | + [0, 0, 7, 0, 0, 0, 5, 8, 0], |
| 37 | + [0, 0, 0, 0, 6, 7, 0, 0, 6], |
| 38 | + [0, 1, 0, 0, 0, 4, 0, 0, 0], |
| 39 | + [5, 2, 0, 0, 0, 0, 0, 0, 0], |
| 40 | + ] |
| 41 | + |
| 42 | + hard1 = [ |
| 43 | +[0, 0, 0, 0, 0, 6, 0, 0, 0], |
| 44 | +[0, 5, 9, 0, 0, 0, 0, 0, 8], |
| 45 | +[2, 0, 0, 0, 0, 8, 0, 0, 0], |
| 46 | +[0, 4, 5, 0, 0, 0, 0, 0, 0], |
| 47 | +[0, 0, 3, 0, 0, 0, 0, 0, 0], |
| 48 | +[0, 0, 6, 0, 0, 3, 0, 5, 4], |
| 49 | +[0, 0, 0, 3, 2, 5, 0, 0, 6], |
| 50 | +[0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 51 | +[0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 52 | +] |
| 53 | + hard0 = [ |
| 54 | + [0, 0, 0, 0, 0, 0, 0, 0, 1], |
| 55 | + [0, 0, 0, 0, 0, 0, 0, 2, 3], |
| 56 | + [0, 0, 4, 0, 0, 5, 0, 0, 0], |
| 57 | + [0, 0, 0, 1, 0, 0, 0, 0, 0], |
| 58 | + [0, 0, 0, 0, 3, 0, 6, 0, 0], |
| 59 | + [0, 0, 7, 0, 0, 0, 5, 8, 0], |
| 60 | + [0, 0, 0, 0, 6, 7, 0, 0, 0], |
| 61 | + [0, 1, 0, 0, 0, 4, 0, 0, 0], |
| 62 | + [5, 2, 0, 0, 0, 0, 0, 0, 0], |
| 63 | +] |
| 64 | + |
| 65 | + initial_grid = hard0 |
| 66 | + |
| 67 | + grid = {} |
| 68 | + for i in line: |
| 69 | + for j in line: |
| 70 | + grid[(i, j)] = model.NewIntVar(1, line_size, "grid %i %i" % (i, j)) |
| 71 | + |
| 72 | + # AllDifferent on rows. |
| 73 | + for i in line: |
| 74 | + model.AddAllDifferent(grid[(i, j)] for j in line) |
| 75 | + |
| 76 | + # AllDifferent on columns. |
| 77 | + for j in line: |
| 78 | + model.AddAllDifferent(grid[(i, j)] for i in line) |
| 79 | + |
| 80 | + # AllDifferent on cells. |
| 81 | + for i in cell: |
| 82 | + for j in cell: |
| 83 | + one_cell = [] |
| 84 | + for di in cell: |
| 85 | + for dj in cell: |
| 86 | + one_cell.append(grid[(i * cell_size + di, j * cell_size + dj)]) |
| 87 | + |
| 88 | + model.AddAllDifferent(one_cell) |
| 89 | + |
| 90 | + # Initial values. |
| 91 | + for i in line: |
| 92 | + for j in line: |
| 93 | + if initial_grid[i][j]: |
| 94 | + model.Add(grid[(i, j)] == initial_grid[i][j]) |
| 95 | + |
| 96 | + # Solve and print out the solution. |
| 97 | + solver = cp_model.CpSolver() |
| 98 | + status = solver.Solve(model) |
| 99 | + if status == cp_model.OPTIMAL: |
| 100 | + for i in line: |
| 101 | + print([int(solver.Value(grid[(i, j)])) for j in line]) |
| 102 | + |
| 103 | + |
| 104 | +solve_sudoku() |
0 commit comments