File: sudoku.py

package info (click to toggle)
logilab-constraint 1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 380 kB
  • sloc: python: 2,928; xml: 165; makefile: 25
file content (135 lines) | stat: -rw-r--r-- 3,396 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# copyright 2002-2021 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of logilab-constraint.
#
# logilab-constraint is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 2.1 of the License, or (at your
# option) any later version.
#
# logilab-constraint is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with logilab-constraint. If not, see <http://www.gnu.org/licenses/>.


from logilab.constraint import fd, Repository, Solver


# games found on http://www.websudoku.com/
# I'm not sure how they rate the difficulty of their problems.
easy = [
    "  5   27 ",
    " 4   79  ",
    "1 6 8  35",
    "4 32 16 9",
    "   5 8   ",
    "8 76 95 3",
    "73  2 1 6",
    "  41   2 ",
    " 12   8  ",
]

medium = [
    " 9 85    ",
    "  3 1 5  ",
    " 283   1 ",
    "   2  4 7",
    " 3     5 ",
    "4 7  5   ",
    " 4   362 ",
    "  2 7 1  ",
    "    26 3 ",
]

hard = [
    " 19 73  4",
    "   98 72 ",
    "        5",
    "      4 6",
    "93     72",
    "4 6      ",
    "8        ",
    " 92 36   ",
    "5  42 31 ",
]

evil = [
    "  1  9   ",
    " 5 4     ",
    "2   1 365",
    "     327 ",
    "9       8",
    " 821     ",
    "473 5   1",
    "     6 4 ",
    "   3  8  ",
]


def sudoku(problem, verbose=0):
    assert len(problem) == 9  # more sizes later
    variables = ["v%02d_%02d" % (i, j) for i in range(9) for j in range(9)]
    domains = {}
    constraints = []
    values = list("123456789")
    for v in variables:
        domains[v] = fd.FiniteDomain(values)

    # line and column constraints
    for i in range(9):
        constraints.append(fd.AllDistinct(["v%02d_%02d" % (i, j) for j in range(9)]))
        constraints.append(fd.AllDistinct(["v%02d_%02d" % (j, i) for j in range(9)]))

    # square constraints:
    for i in (0, 3, 6):
        for j in (0, 3, 6):
            constraints.append(
                fd.AllDistinct(
                    [
                        "v%02d_%02d" % (i + ii, j + jj)
                        for ii in (0, 1, 2)
                        for jj in (0, 1, 2)
                    ]
                )
            )

    # fixed values:
    for i, line in enumerate(problem):
        for j, value in enumerate(line):
            if value != " ":
                constraints.append(fd.Equals("v%02d_%02d" % (i, j), value))

    r = Repository(variables, domains, constraints)
    s = Solver().solve_one(r, verbose)
    return s


def display_solution(d):
    for i in range(9):
        for j in range(9):
            print(d["v%02d_%02d" % (i, j)], end=" ")
        print()


if __name__ == "__main__":
    import sys
    import getopt

    opts, args = getopt.getopt(sys.argv[1:], "dv")
    verbose = 0
    display = 0
    for o, v in opts:
        if o == "-v":
            verbose += 1
        if o == "-d":
            display = 1
    sol = sudoku(evil, verbose)
    if display:
        display_solution(sol)
    else:
        print(sol)