|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Sudoku DLX — Demo Notebook\n", |
| 8 | + "\n", |
| 9 | + "This notebook walks through:\n", |
| 10 | + "1. Generate & analyze a puzzle\n", |
| 11 | + "2. Solve & capture a trace\n", |
| 12 | + "3. Review human-style explanation steps\n", |
| 13 | + "4. (Optional) SAT cross-check\n", |
| 14 | + "\n", |
| 15 | + "> Set up locally with `pip install -e \".[dev]\"` and optionally `pip install -e \".[sat]\"`." |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": null, |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [], |
| 23 | + "source": [ |
| 24 | + "from textwrap import dedent\n", |
| 25 | + "\n", |
| 26 | + "from sudoku_dlx import analyze, generate, solve, to_string, explain, build_reveal_trace\n", |
| 27 | + "from sudoku_dlx.crosscheck import sat_solve\n", |
| 28 | + "\n", |
| 29 | + "puzzle = generate(seed=123, target_givens=30, minimal=False, symmetry=\"mix\")\n", |
| 30 | + "print(\"Puzzle:\")\n", |
| 31 | + "for row in puzzle:\n", |
| 32 | + " print(\" \\".join(str(x) if x else \".\" for x in row))\n", |
| 33 | + "print(\"\nAnalysis:\")\n", |
| 34 | + "print(analyze(puzzle))" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": null, |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [], |
| 42 | + "source": [ |
| 43 | + "result = solve([row[:] for row in puzzle])\n", |
| 44 | + "assert result is not None\n", |
| 45 | + "print(\"Solved in\", f\"{result.stats.ms:.2f} ms\", \"nodes:\", result.stats.nodes)\n", |
| 46 | + "print(\"Solution (81):\", to_string(result.grid))\n", |
| 47 | + "trace = build_reveal_trace(puzzle, result.grid, result.stats)\n", |
| 48 | + "print(\"Trace keys:\", list(trace.keys()))" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "markdown", |
| 53 | + "metadata": {}, |
| 54 | + "source": [ |
| 55 | + "### Human-style steps" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "code", |
| 60 | + "execution_count": null, |
| 61 | + "metadata": {}, |
| 62 | + "outputs": [], |
| 63 | + "source": [ |
| 64 | + "explanation = explain(puzzle, max_steps=200)\n", |
| 65 | + "print(\"Steps:\", len(explanation['steps']))\n", |
| 66 | + "print(\"Progress:\", explanation['progress'])\n", |
| 67 | + "print(\"Example step:\", explanation['steps'][0] if explanation['steps'] else None)" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "markdown", |
| 72 | + "metadata": {}, |
| 73 | + "source": [ |
| 74 | + "### Optional: SAT cross-check\n", |
| 75 | + "If `python-sat` is installed, the SAT solution should match DLX." |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": null, |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [], |
| 83 | + "source": [ |
| 84 | + "sat_grid = None\n", |
| 85 | + "try:\n", |
| 86 | + " sat_grid = sat_solve(puzzle)\n", |
| 87 | + "except Exception as exc:\n", |
| 88 | + " print(\"SAT unavailable:\", exc)\n", |
| 89 | + "if sat_grid is not None:\n", |
| 90 | + " print(\"SAT solved:\", to_string(sat_grid))\n", |
| 91 | + " print(\"Match:\", to_string(sat_grid) == to_string(result.grid))\n", |
| 92 | + "else:\n", |
| 93 | + " print(\"SAT solver not installed. Run pip install -e '.[sat]'\")" |
| 94 | + ] |
| 95 | + } |
| 96 | + ], |
| 97 | + "metadata": { |
| 98 | + "kernelspec": { |
| 99 | + "display_name": "Python 3", |
| 100 | + "language": "python", |
| 101 | + "name": "python3" |
| 102 | + }, |
| 103 | + "language_info": { |
| 104 | + "name": "python", |
| 105 | + "pygments_lexer": "ipython3" |
| 106 | + } |
| 107 | + }, |
| 108 | + "nbformat": 4, |
| 109 | + "nbformat_minor": 5 |
| 110 | +} |
0 commit comments