Files
2025-02-04 18:04:03 +01:00

259 lines
8.1 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": [
"# kakuro"
]
},
{
"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/kakuro.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/kakuro.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",
" Kakuru puzzle in Google CP Solver.\n",
"\n",
" http://en.wikipedia.org/wiki/Kakuro\n",
" '''\n",
" The object of the puzzle is to insert a digit from 1 to 9 inclusive\n",
" into each white cell such that the sum of the numbers in each entry\n",
" matches the clue associated with it and that no digit is duplicated in\n",
" any entry. It is that lack of duplication that makes creating Kakuro\n",
" puzzles with unique solutions possible, and which means solving a Kakuro\n",
" puzzle involves investigating combinations more, compared to Sudoku in\n",
" which the focus is on permutations. There is an unwritten rule for\n",
" making Kakuro puzzles that each clue must have at least two numbers\n",
" that add up to it. This is because including one number is mathematically\n",
" trivial when solving Kakuro puzzles; one can simply disregard the\n",
" number entirely and subtract it from the clue it indicates.\n",
" '''\n",
"\n",
" This model solves the problem at the Wikipedia page.\n",
" For a larger picture, see\n",
" http://en.wikipedia.org/wiki/File:Kakuro_black_box.svg\n",
"\n",
" The solution:\n",
" 9 7 0 0 8 7 9\n",
" 8 9 0 8 9 5 7\n",
" 6 8 5 9 7 0 0\n",
" 0 6 1 0 2 6 0\n",
" 0 0 4 6 1 3 2\n",
" 8 9 3 1 0 1 4\n",
" 3 1 2 0 0 2 1\n",
"\n",
" Compare with the following models:\n",
" * Comet : http://www.hakank.org/comet/kakuro.co\n",
" * MiniZinc: http://www.hakank.org/minizinc/kakuro.mzn\n",
" * SICStus : http://www.hakank.org/sicstus/kakuro.pl\n",
" * ECLiPSe: http://www.hakank.org/eclipse/kakuro.ecl\n",
" * Gecode: http://www.hakank.org/gecode/kenken2.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": [
"import sys\n",
"\n",
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"#\n",
"# Ensure that the sum of the segments\n",
"# in cc == res\n",
"#\n",
"\n",
"\n",
"def calc(cc, x, res):\n",
"\n",
" solver = list(x.values())[0].solver()\n",
"\n",
" # ensure that the values are positive\n",
" for i in cc:\n",
" solver.Add(x[i[0] - 1, i[1] - 1] >= 1)\n",
"\n",
" # sum the numbers\n",
" solver.Add(solver.Sum([x[i[0] - 1, i[1] - 1] for i in cc]) == res)\n",
"\n",
"\n",
"def main():\n",
"\n",
" # Create the solver.\n",
" solver = pywrapcp.Solver(\"Kakuro\")\n",
"\n",
" #\n",
" # data\n",
" #\n",
"\n",
" # size of matrix\n",
" n = 7\n",
"\n",
" # segments\n",
" # [sum, [segments]]\n",
" # Note: 1-based\n",
" problem = [[16, [1, 1], [1, 2]], [24, [1, 5], [1, 6], [1, 7]],\n",
" [17, [2, 1], [2, 2]], [29, [2, 4], [2, 5], [2, 6], [2, 7]],\n",
" [35, [3, 1], [3, 2], [3, 3], [3, 4], [3, 5]], [7, [4, 2], [4, 3]],\n",
" [8, [4, 5], [4, 6]], [16, [5, 3], [5, 4], [5, 5], [5, 6], [5, 7]],\n",
" [21, [6, 1], [6, 2], [6, 3], [6, 4]], [5, [6, 6], [6, 7]],\n",
" [6, [7, 1], [7, 2], [7, 3]], [3, [7, 6], [7, 7]],\n",
" [23, [1, 1], [2, 1], [3, 1]], [30, [1, 2], [2, 2], [3, 2], [4, 2]],\n",
" [27, [1, 5], [2, 5], [3, 5], [4, 5], [5, 5]], [12, [1, 6], [2, 6]],\n",
" [16, [1, 7], [2, 7]], [17, [2, 4], [3, 4]],\n",
" [15, [3, 3], [4, 3], [5, 3], [6, 3], [7, 3]],\n",
" [12, [4, 6], [5, 6], [6, 6], [7, 6]], [7, [5, 4], [6, 4]],\n",
" [7, [5, 7], [6, 7], [7, 7]], [11, [6, 1], [7, 1]],\n",
" [10, [6, 2], [7, 2]]]\n",
"\n",
" num_p = len(problem)\n",
"\n",
" # The blanks\n",
" # Note: 1-based\n",
" blanks = [[1, 3], [1, 4], [2, 3], [3, 6], [3, 7], [4, 1], [4, 4], [4, 7],\n",
" [5, 1], [5, 2], [6, 5], [7, 4], [7, 5]]\n",
" num_blanks = len(blanks)\n",
"\n",
" #\n",
" # variables\n",
" #\n",
"\n",
" # the set\n",
" x = {}\n",
" for i in range(n):\n",
" for j in range(n):\n",
" x[i, j] = solver.IntVar(0, 9, \"x[%i,%i]\" % (i, j))\n",
"\n",
" x_flat = [x[i, j] for i in range(n) for j in range(n)]\n",
"\n",
" #\n",
" # constraints\n",
" #\n",
"\n",
" # fill the blanks with 0\n",
" for i in range(num_blanks):\n",
" solver.Add(x[blanks[i][0] - 1, blanks[i][1] - 1] == 0)\n",
"\n",
" for i in range(num_p):\n",
" segment = problem[i][1::]\n",
" res = problem[i][0]\n",
"\n",
" # sum this segment\n",
" calc(segment, x, res)\n",
"\n",
" # all numbers in this segment must be distinct\n",
" segment = [x[p[0] - 1, p[1] - 1] for p in segment]\n",
" solver.Add(solver.AllDifferent(segment))\n",
"\n",
" #\n",
" # search and solution\n",
" #\n",
" db = solver.Phase(x_flat, solver.INT_VAR_DEFAULT, solver.INT_VALUE_DEFAULT)\n",
"\n",
" solver.NewSearch(db)\n",
"\n",
" num_solutions = 0\n",
" while solver.NextSolution():\n",
" for i in range(n):\n",
" for j in range(n):\n",
" val = x[i, j].Value()\n",
" if val > 0:\n",
" print(val, end=\" \")\n",
" else:\n",
" print(\" \", end=\" \")\n",
" print()\n",
"\n",
" print()\n",
" num_solutions += 1\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
}