194 lines
5.7 KiB
Plaintext
194 lines
5.7 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": [
|
|
"# just_forgotten"
|
|
]
|
|
},
|
|
{
|
|
"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/just_forgotten.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/just_forgotten.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",
|
|
" Just forgotten puzzle (Enigma 1517) in Google CP Solver.\n",
|
|
"\n",
|
|
" From http://www.f1compiler.com/samples/Enigma 201517.f1.html\n",
|
|
" '''\n",
|
|
" Enigma 1517 Bob Walker, New Scientist magazine, October 25, 2008.\n",
|
|
"\n",
|
|
" Joe was furious when he forgot one of his bank account numbers.\n",
|
|
" He remembered that it had all the digits 0 to 9 in some order,\n",
|
|
" so he tried the following four sets without success:\n",
|
|
"\n",
|
|
" 9 4 6 2 1 5 7 8 3 0\n",
|
|
" 8 6 0 4 3 9 1 2 5 7\n",
|
|
" 1 6 4 0 2 9 7 8 5 3\n",
|
|
" 6 8 2 4 3 1 9 0 7 5\n",
|
|
"\n",
|
|
" When Joe finally remembered his account number, he realised that\n",
|
|
" in each set just four of the digits were in their correct position\n",
|
|
" and that, if one knew that, it was possible to work out his\n",
|
|
" account number. What was it?\n",
|
|
" '''\n",
|
|
"\n",
|
|
" Compare with the following models:\n",
|
|
" * MiniZinc: http://www.hakank.org/minizinc/just_forgotten.mzn\n",
|
|
" * SICStus Prolog: http://www.hakank.org/sicstis/just_forgotten.pl\n",
|
|
" * ECLiPSe: http://hakank.org/eclipse/just_forgotten.ecl\n",
|
|
" * Gecpde: http://hakank.org/gecode/just_forgotten.cpp\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(\"Just forgotten\")\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
" rows = 4\n",
|
|
" cols = 10\n",
|
|
" # The four tries\n",
|
|
" a = [[9, 4, 6, 2, 1, 5, 7, 8, 3, 0], [8, 6, 0, 4, 3, 9, 1, 2, 5, 7],\n",
|
|
" [1, 6, 4, 0, 2, 9, 7, 8, 5, 3], [6, 8, 2, 4, 3, 1, 9, 0, 7, 5]]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # variables\n",
|
|
" #\n",
|
|
" x = [solver.IntVar(0, 9, \"x[%i]\" % j) for j in range(cols)]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
" solver.Add(solver.AllDifferent(x))\n",
|
|
"\n",
|
|
" for r in range(rows):\n",
|
|
" b = [solver.IsEqualCstVar(x[c], a[r][c]) for c in range(cols)]\n",
|
|
" solver.Add(solver.Sum(b) == 4)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # search and result\n",
|
|
" #\n",
|
|
" db = solver.Phase(x, solver.INT_VAR_SIMPLE, solver.INT_VALUE_DEFAULT)\n",
|
|
"\n",
|
|
" solver.NewSearch(db)\n",
|
|
"\n",
|
|
" num_solutions = 0\n",
|
|
" while solver.NextSolution():\n",
|
|
" num_solutions += 1\n",
|
|
" xval = [x[j].Value() for j in range(cols)]\n",
|
|
" print(\"Account number:\")\n",
|
|
" for j in range(cols):\n",
|
|
" print(\"%i \" % xval[j], end=\" \")\n",
|
|
" print()\n",
|
|
" print(\"\\nThe four tries, where '!' represents a correct digit:\")\n",
|
|
" for i in range(rows):\n",
|
|
" for j in range(cols):\n",
|
|
" check = \" \"\n",
|
|
" if a[i][j] == xval[j]:\n",
|
|
" check = \"!\"\n",
|
|
" print(\"%i%s\" % (a[i][j], check), end=\" \")\n",
|
|
" print()\n",
|
|
" print()\n",
|
|
" print()\n",
|
|
"\n",
|
|
" solver.EndSearch()\n",
|
|
"\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
|
|
}
|