243 lines
7.2 KiB
Plaintext
243 lines
7.2 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2025 Google LLC."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "apache",
|
|
"metadata": {},
|
|
"source": [
|
|
"Licensed under the Apache License, Version 2.0 (the \"License\");\n",
|
|
"you may not use this file except in compliance with the License.\n",
|
|
"You may obtain a copy of the License at\n",
|
|
"\n",
|
|
" http://www.apache.org/licenses/LICENSE-2.0\n",
|
|
"\n",
|
|
"Unless required by applicable law or agreed to in writing, software\n",
|
|
"distributed under the License is distributed on an \"AS IS\" BASIS,\n",
|
|
"WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
|
|
"See the License for the specific language governing permissions and\n",
|
|
"limitations under the License.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "basename",
|
|
"metadata": {},
|
|
"source": [
|
|
"# alphametic"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "link",
|
|
"metadata": {},
|
|
"source": [
|
|
"<table align=\"left\">\n",
|
|
"<td>\n",
|
|
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/alphametic.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
|
|
"</td>\n",
|
|
"<td>\n",
|
|
"<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/alphametic.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
|
|
"</td>\n",
|
|
"</table>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "doc",
|
|
"metadata": {},
|
|
"source": [
|
|
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "install",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%pip install ortools"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "description",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"\n",
|
|
" Generic alphametic solver in Google CP Solver.\n",
|
|
"\n",
|
|
" This is a generic alphametic solver.\n",
|
|
"\n",
|
|
" Usage:\n",
|
|
" python alphametic.py\n",
|
|
" -> solves SEND+MORE=MONEY in base 10\n",
|
|
"\n",
|
|
" python alphametic.py 'SEND+MOST=MONEY' 11\n",
|
|
" -> solver SEND+MOST=MONEY in base 11\n",
|
|
"\n",
|
|
" python alphametic.py TEST <base>\n",
|
|
" -> solve some test problems in base <base>\n",
|
|
" (defined in test_problems())\n",
|
|
"\n",
|
|
" Assumptions:\n",
|
|
" - we only solves problems of the form\n",
|
|
" NUMBER<1>+NUMBER<2>...+NUMBER<N-1> = NUMBER<N>\n",
|
|
" i.e. the last number is the sum\n",
|
|
" - the only nonletter characters are: +, =, \\d (which are splitted upon)\n",
|
|
"\n",
|
|
"\n",
|
|
" Compare with the following model:\n",
|
|
" * Zinc: http://www.hakank.org/minizinc/alphametic.zinc\n",
|
|
"\n",
|
|
" This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
|
|
" Also see my other Google CP Solver models:\n",
|
|
" http://www.hakank.org/google_or_tools/\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import sys\n",
|
|
"import re\n",
|
|
"\n",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"def main(problem_str=\"SEND+MORE=MONEY\", base=10):\n",
|
|
"\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywrapcp.Solver(\"Send most money\")\n",
|
|
"\n",
|
|
" # data\n",
|
|
" print(\"\\nproblem:\", problem_str)\n",
|
|
"\n",
|
|
" # convert to array.\n",
|
|
" problem = re.split(\"[\\s+=]\", problem_str)\n",
|
|
"\n",
|
|
" p_len = len(problem)\n",
|
|
" print(\"base:\", base)\n",
|
|
"\n",
|
|
" # create the lookup table: list of (digit : ix)\n",
|
|
" a = sorted(set(\"\".join(problem)))\n",
|
|
" n = len(a)\n",
|
|
" lookup = dict(list(zip(a, list(range(n)))))\n",
|
|
"\n",
|
|
" # length of each number\n",
|
|
" lens = list(map(len, problem))\n",
|
|
"\n",
|
|
" #\n",
|
|
" # declare variables\n",
|
|
" #\n",
|
|
"\n",
|
|
" # the digits\n",
|
|
" x = [solver.IntVar(0, base - 1, \"x[%i]\" % i) for i in range(n)]\n",
|
|
" # the sums of each number (e.g. the three numbers SEND, MORE, MONEY)\n",
|
|
" sums = [solver.IntVar(1, 10**(lens[i]) - 1) for i in range(p_len)]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
" solver.Add(solver.AllDifferent(x))\n",
|
|
"\n",
|
|
" ix = 0\n",
|
|
" for prob in problem:\n",
|
|
" this_len = len(prob)\n",
|
|
"\n",
|
|
" # sum all the digits with proper exponents to a number\n",
|
|
" solver.Add(\n",
|
|
" sums[ix] == solver.Sum([(base**i) * x[lookup[prob[this_len - i - 1]]]\n",
|
|
" for i in range(this_len)[::-1]]))\n",
|
|
" # leading digits must be > 0\n",
|
|
" solver.Add(x[lookup[prob[0]]] > 0)\n",
|
|
" ix += 1\n",
|
|
"\n",
|
|
" # the last number is the sum of the previous numbers\n",
|
|
" solver.Add(solver.Sum([sums[i] for i in range(p_len - 1)]) == sums[-1])\n",
|
|
"\n",
|
|
" #\n",
|
|
" # solution and search\n",
|
|
" #\n",
|
|
" solution = solver.Assignment()\n",
|
|
" solution.Add(x)\n",
|
|
" solution.Add(sums)\n",
|
|
"\n",
|
|
" db = solver.Phase(x, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE)\n",
|
|
"\n",
|
|
" solver.NewSearch(db)\n",
|
|
"\n",
|
|
" num_solutions = 0\n",
|
|
" while solver.NextSolution():\n",
|
|
" num_solutions += 1\n",
|
|
" print(\"\\nsolution #%i\" % num_solutions)\n",
|
|
" for i in range(n):\n",
|
|
" print(a[i], \"=\", x[i].Value())\n",
|
|
" print()\n",
|
|
" for prob in problem:\n",
|
|
" for p in prob:\n",
|
|
" print(p, end=\" \")\n",
|
|
" print()\n",
|
|
" print()\n",
|
|
" for prob in problem:\n",
|
|
" for p in prob:\n",
|
|
" print(x[lookup[p]].Value(), end=\" \")\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print(\"sums:\", [sums[i].Value() for i in range(p_len)])\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print(\"\\nnum_solutions:\", num_solutions)\n",
|
|
" print(\"failures:\", solver.Failures())\n",
|
|
" print(\"branches:\", solver.Branches())\n",
|
|
" print(\"WallTime:\", solver.WallTime())\n",
|
|
"\n",
|
|
"\n",
|
|
"def test_problems(base=10):\n",
|
|
" problems = [\n",
|
|
" \"SEND+MORE=MONEY\", \"SEND+MOST=MONEY\", \"VINGT+CINQ+CINQ=TRENTE\",\n",
|
|
" \"EIN+EIN+EIN+EIN=VIER\", \"DONALD+GERALD=ROBERT\",\n",
|
|
" \"SATURN+URANUS+NEPTUNE+PLUTO+PLANETS\", \"WRONG+WRONG=RIGHT\"\n",
|
|
" ]\n",
|
|
"\n",
|
|
" for p in problems:\n",
|
|
" main(p, base)\n",
|
|
"\n",
|
|
"\n",
|
|
"problem = \"SEND+MORE=MONEY\"\n",
|
|
"base = 10\n",
|
|
"if len(sys.argv) > 1:\n",
|
|
" problem = sys.argv[1]\n",
|
|
"if len(sys.argv) > 2:\n",
|
|
" base = int(sys.argv[2])\n",
|
|
"\n",
|
|
"if problem == \"TEST\" or problem == \"test\":\n",
|
|
" test_problems(base)\n",
|
|
"else:\n",
|
|
" main(problem, base)\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|