195 lines
5.9 KiB
Plaintext
195 lines
5.9 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": [
|
|
"# crypta"
|
|
]
|
|
},
|
|
{
|
|
"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/crypta.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/crypta.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",
|
|
" Cryptarithmetic puzzle in Google CP Solver.\n",
|
|
"\n",
|
|
" Prolog benchmark problem GNU Prolog (crypta.pl)\n",
|
|
" '''\n",
|
|
" Name : crypta.pl\n",
|
|
" Title : crypt-arithmetic\n",
|
|
" Original Source: P. Van Hentenryck's book\n",
|
|
" Adapted by : Daniel Diaz - INRIA France\n",
|
|
" Date : September 1992\n",
|
|
"\n",
|
|
" Solve the operation:\n",
|
|
"\n",
|
|
" B A I J J A J I I A H F C F E B B J E A\n",
|
|
" + D H F G A B C D I D B I F F A G F E J E\n",
|
|
" -----------------------------------------\n",
|
|
" = G J E G A C D D H F A F J B F I H E E F\n",
|
|
" '''\n",
|
|
"\n",
|
|
"\n",
|
|
" Compare with the following models:\n",
|
|
" * Comet: http://hakank.org/comet/crypta.co\n",
|
|
" * MiniZinc: http://hakank.org/minizinc/crypta.mzn\n",
|
|
" * ECLiPSe: http://hakank.org/eclipse/crypta.ecl\n",
|
|
" * Gecode: http://hakank.org/gecode/crypta.cpp\n",
|
|
" * SICStus: http://hakank.org/sicstus/crypta.pl\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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
"\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywrapcp.Solver(\"Crypta\")\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
"\n",
|
|
" #\n",
|
|
" # variables\n",
|
|
" #\n",
|
|
" LD = [solver.IntVar(0, 9, \"LD[%i]\" % i) for i in range(0, 10)]\n",
|
|
" A, B, C, D, E, F, G, H, I, J = LD\n",
|
|
"\n",
|
|
" Sr1 = solver.IntVar(0, 1, \"Sr1\")\n",
|
|
" Sr2 = solver.IntVar(0, 1, \"Sr2\")\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
" solver.Add(solver.AllDifferent(LD))\n",
|
|
" solver.Add(B >= 1)\n",
|
|
" solver.Add(D >= 1)\n",
|
|
" solver.Add(G >= 1)\n",
|
|
"\n",
|
|
" solver.Add(A + 10 * E + 100 * J + 1000 * B + 10000 * B + 100000 * E +\n",
|
|
" 1000000 * F + E + 10 * J + 100 * E + 1000 * F + 10000 * G +\n",
|
|
" 100000 * A + 1000000 * F == F + 10 * E + 100 * E + 1000 * H +\n",
|
|
" 10000 * I + 100000 * F + 1000000 * B + 10000000 * Sr1)\n",
|
|
"\n",
|
|
" solver.Add(C + 10 * F + 100 * H + 1000 * A + 10000 * I + 100000 * I +\n",
|
|
" 1000000 * J + F + 10 * I + 100 * B + 1000 * D + 10000 * I +\n",
|
|
" 100000 * D + 1000000 * C + Sr1 == J + 10 * F + 100 * A + 1000 * F +\n",
|
|
" 10000 * H + 100000 * D + 1000000 * D + 10000000 * Sr2)\n",
|
|
"\n",
|
|
" solver.Add(A + 10 * J + 100 * J + 1000 * I + 10000 * A + 100000 * B + B +\n",
|
|
" 10 * A + 100 * G + 1000 * F + 10000 * H + 100000 * D + Sr2 == C +\n",
|
|
" 10 * A + 100 * G + 1000 * E + 10000 * J + 100000 * G)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # search and result\n",
|
|
" #\n",
|
|
" db = solver.Phase(LD, solver.INT_VAR_SIMPLE, solver.INT_VALUE_SIMPLE)\n",
|
|
"\n",
|
|
" solver.NewSearch(db)\n",
|
|
"\n",
|
|
" num_solutions = 0\n",
|
|
" str = \"ABCDEFGHIJ\"\n",
|
|
" while solver.NextSolution():\n",
|
|
" num_solutions += 1\n",
|
|
" for (letter, val) in [(str[i], LD[i].Value()) for i in range(len(LD))]:\n",
|
|
" print(\"%s: %i\" % (letter, val))\n",
|
|
" print()\n",
|
|
"\n",
|
|
" solver.EndSearch()\n",
|
|
"\n",
|
|
" print()\n",
|
|
" print(\"num_solutions:\", num_solutions)\n",
|
|
" print(\"failures:\", solver.Failures())\n",
|
|
" print(\"branches:\", solver.Branches())\n",
|
|
" print(\"WallTime:\", solver.WallTime())\n",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|