forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alphametic.py
162 lines (125 loc) · 4.2 KB
/
alphametic.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Copyright 2010 Hakan Kjellerstrand [email protected]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Generic alphametic solver in Google CP Solver.
This is a generic alphametic solver.
Usage:
python alphametic.py
-> solves SEND+MORE=MONEY in base 10
python alphametic.py 'SEND+MOST=MONEY' 11
-> solver SEND+MOST=MONEY in base 11
python alphametic.py TEST <base>
-> solve some test problems in base <base>
(defined in test_problems())
Assumptions:
- we only solves problems of the form
NUMBER<1>+NUMBER<2>...+NUMBER<N-1> = NUMBER<N>
i.e. the last number is the sum
- the only nonletter characters are: +, =, \d (which are splitted upon)
Compare with the following model:
* Zinc: http://www.hakank.org/minizinc/alphametic.zinc
This model was created by Hakan Kjellerstrand ([email protected])
Also see my other Google CP Solver models:
http://www.hakank.org/google_or_tools/
"""
import sys
import re
from ortools.constraint_solver import pywrapcp
def main(problem_str="SEND+MORE=MONEY", base=10):
# Create the solver.
solver = pywrapcp.Solver("Send most money")
# data
print("\nproblem:", problem_str)
# convert to array.
problem = re.split("[\s+=]", problem_str)
p_len = len(problem)
print("base:", base)
# create the lookup table: list of (digit : ix)
a = sorted(set("".join(problem)))
n = len(a)
lookup = dict(list(zip(a, list(range(n)))))
# length of each number
lens = list(map(len, problem))
#
# declare variables
#
# the digits
x = [solver.IntVar(0, base - 1, "x[%i]" % i) for i in range(n)]
# the sums of each number (e.g. the three numbers SEND, MORE, MONEY)
sums = [solver.IntVar(1, 10**(lens[i]) - 1) for i in range(p_len)]
#
# constraints
#
solver.Add(solver.AllDifferent(x))
ix = 0
for prob in problem:
this_len = len(prob)
# sum all the digits with proper exponents to a number
solver.Add(
sums[ix] == solver.Sum([(base**i) * x[lookup[prob[this_len - i - 1]]]
for i in range(this_len)[::-1]]))
# leading digits must be > 0
solver.Add(x[lookup[prob[0]]] > 0)
ix += 1
# the last number is the sum of the previous numbers
solver.Add(solver.Sum([sums[i] for i in range(p_len - 1)]) == sums[-1])
#
# solution and search
#
solution = solver.Assignment()
solution.Add(x)
solution.Add(sums)
db = solver.Phase(x, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE)
solver.NewSearch(db)
num_solutions = 0
while solver.NextSolution():
num_solutions += 1
print("\nsolution #%i" % num_solutions)
for i in range(n):
print(a[i], "=", x[i].Value())
print()
for prob in problem:
for p in prob:
print(p, end=" ")
print()
print()
for prob in problem:
for p in prob:
print(x[lookup[p]].Value(), end=" ")
print()
print("sums:", [sums[i].Value() for i in range(p_len)])
print()
print("\nnum_solutions:", num_solutions)
print("failures:", solver.Failures())
print("branches:", solver.Branches())
print("WallTime:", solver.WallTime())
def test_problems(base=10):
problems = [
"SEND+MORE=MONEY", "SEND+MOST=MONEY", "VINGT+CINQ+CINQ=TRENTE",
"EIN+EIN+EIN+EIN=VIER", "DONALD+GERALD=ROBERT",
"SATURN+URANUS+NEPTUNE+PLUTO+PLANETS", "WRONG+WRONG=RIGHT"
]
for p in problems:
main(p, base)
problem = "SEND+MORE=MONEY"
base = 10
if __name__ == "__main__":
if len(sys.argv) > 1:
problem = sys.argv[1]
if len(sys.argv) > 2:
base = int(sys.argv[2])
if problem == "TEST" or problem == "test":
test_problems(base)
else:
main(problem, base)