2017-10-17 13:08:10 +02:00
|
|
|
# Copyright 2010-2017 Google
|
2010-09-15 12:42:33 +00:00
|
|
|
# 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
|
|
|
|
|
#
|
2010-10-06 19:46:05 +00:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2010-09-15 12:42:33 +00:00
|
|
|
#
|
|
|
|
|
# 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.
|
|
|
|
|
"""Send + more = money.
|
|
|
|
|
|
|
|
|
|
In this model, we try to solve the following cryptarythm
|
|
|
|
|
SEND + MORE = MONEY
|
|
|
|
|
Each letter corresponds to one figure and all letters have different values.
|
|
|
|
|
"""
|
|
|
|
|
|
2016-01-15 00:18:32 +01:00
|
|
|
from __future__ import print_function
|
2013-12-24 11:35:01 +00:00
|
|
|
from ortools.constraint_solver import pywrapcp
|
2010-09-15 12:42:33 +00:00
|
|
|
|
|
|
|
|
|
2015-12-09 14:49:52 +01:00
|
|
|
def main():
|
2010-09-15 12:42:33 +00:00
|
|
|
# Create the solver.
|
|
|
|
|
solver = pywrapcp.Solver('SEND + MORE = MONEY')
|
|
|
|
|
|
2016-01-15 00:18:32 +01:00
|
|
|
digits = list(range(0, 10))
|
2010-09-15 12:42:33 +00:00
|
|
|
s = solver.IntVar(digits, 's')
|
|
|
|
|
e = solver.IntVar(digits, 'e')
|
|
|
|
|
n = solver.IntVar(digits, 'n')
|
|
|
|
|
d = solver.IntVar(digits, 'd')
|
|
|
|
|
m = solver.IntVar(digits, 'm')
|
|
|
|
|
o = solver.IntVar(digits, 'o')
|
|
|
|
|
r = solver.IntVar(digits, 'r')
|
|
|
|
|
y = solver.IntVar(digits, 'y')
|
|
|
|
|
|
|
|
|
|
letters = [s, e, n, d, m, o, r, y]
|
|
|
|
|
|
2018-06-11 11:51:18 +02:00
|
|
|
solver.Add(1000 * s + 100 * e + 10 * n + d + 1000 * m + 100 * o + 10 * r +
|
|
|
|
|
e == 10000 * m + 1000 * o + 100 * n + 10 * e + y)
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2013-10-17 08:58:26 +00:00
|
|
|
# pylint: disable=g-explicit-bool-comparison
|
2010-09-15 12:42:33 +00:00
|
|
|
solver.Add(s != 0)
|
|
|
|
|
solver.Add(m != 0)
|
|
|
|
|
|
2012-01-16 10:40:52 +00:00
|
|
|
solver.Add(solver.AllDifferent(letters))
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2018-06-11 11:51:18 +02:00
|
|
|
solver.NewSearch(
|
|
|
|
|
solver.Phase(letters, solver.INT_VAR_DEFAULT, solver.INT_VALUE_DEFAULT))
|
2010-10-06 16:04:31 +00:00
|
|
|
solver.NextSolution()
|
2016-01-15 00:18:32 +01:00
|
|
|
print(letters)
|
2010-10-06 16:04:31 +00:00
|
|
|
solver.EndSearch()
|
2010-09-15 12:42:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-12-09 14:49:52 +01:00
|
|
|
main()
|