1 // Copyright 2010-2021 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 // Use CP-SAT to solve a simple cryptarithmetic problem: SEND+MORE=MONEY.
15 
16 #include "ortools/sat/cp_model.h"
17 
18 namespace operations_research {
19 namespace sat {
20 
SendMoreMoney()21 void SendMoreMoney() {
22   CpModelBuilder cp_model;
23 
24   // Possible domains for variables.
25   Domain all_digits(0, 9);
26   Domain non_zero_digits(1, 9);
27   // Create variables.
28   // Since s is a leading digit, it can't be 0.
29   const IntVar s = cp_model.NewIntVar(non_zero_digits);
30   const IntVar e = cp_model.NewIntVar(all_digits);
31   const IntVar n = cp_model.NewIntVar(all_digits);
32   const IntVar d = cp_model.NewIntVar(all_digits);
33   // Since m is a leading digit, it can't be 0.
34   const IntVar m = cp_model.NewIntVar(non_zero_digits);
35   const IntVar o = cp_model.NewIntVar(all_digits);
36   const IntVar r = cp_model.NewIntVar(all_digits);
37   const IntVar y = cp_model.NewIntVar(all_digits);
38 
39   // Create carry variables. c0 is true if the first column of addends carries
40   // a 1, c2 is true if the second column carries a 1, and so on.
41   const BoolVar c0 = cp_model.NewBoolVar();
42   const BoolVar c1 = cp_model.NewBoolVar();
43   const BoolVar c2 = cp_model.NewBoolVar();
44   const BoolVar c3 = cp_model.NewBoolVar();
45 
46   // Force all letters to take on different values.
47   cp_model.AddAllDifferent({s, e, n, d, m, o, r, y});
48 
49   // Column 0:
50   cp_model.AddEquality(c0, m);
51 
52   // Column 1:
53   cp_model.AddEquality(c1 + s + m + o, 10 * c0);
54 
55   // Column 2:
56   cp_model.AddEquality(c2 + e + o, n + 10 * c1);
57 
58   // Column 3:
59   cp_model.AddEquality(c3 + n + r, e + 10 * c2);
60 
61   // Column 4:
62   cp_model.AddEquality(d + e, y + 10 * c3);
63 
64   // Declare the model, solve it, and display the results.
65   const CpSolverResponse response = Solve(cp_model.Build());
66   LOG(INFO) << CpSolverResponseStats(response);
67   LOG(INFO) << "s: " << SolutionIntegerValue(response, s);
68   LOG(INFO) << "e: " << SolutionIntegerValue(response, e);
69   LOG(INFO) << "n: " << SolutionIntegerValue(response, n);
70   LOG(INFO) << "d: " << SolutionIntegerValue(response, d);
71   LOG(INFO) << "m: " << SolutionIntegerValue(response, m);
72   LOG(INFO) << "o: " << SolutionIntegerValue(response, o);
73   LOG(INFO) << "r: " << SolutionIntegerValue(response, r);
74   LOG(INFO) << "y: " << SolutionIntegerValue(response, y);
75 }
76 
77 }  // namespace sat
78 }  // namespace operations_research
79 
main()80 int main() {
81   operations_research::sat::SendMoreMoney();
82   return EXIT_SUCCESS;
83 }
84