{"spec_id":"maze-printable","library":"altair","language":"python","code":"\"\"\" anyplot.ai\nmaze-printable: Printable Maze Puzzle\nLibrary: altair 6.1.0 | Python 3.13.13\nQuality: 82/100 | Updated: 2026-05-16\n\"\"\"\n\nimport os\nimport sys\n\n\nif \"\" in sys.path:\n    sys.path.remove(\"\")\nif \".\" in sys.path:\n    sys.path.remove(\".\")\ncurrent_dir = os.path.dirname(os.path.abspath(__file__))\nif current_dir in sys.path:\n    sys.path.remove(current_dir)\n\nimport altair as alt\nimport numpy as np\nimport pandas as pd\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nBRAND = \"#009E73\"  # Okabe-Ito position 1\n\n# Maze parameters\nnp.random.seed(42)\nWIDTH = 25\nHEIGHT = 25\n\n# Initialize maze grid (0 = path, 1 = wall)\nmaze = np.ones((HEIGHT * 2 + 1, WIDTH * 2 + 1), dtype=int)\n\n# Generate maze using iterative DFS with explicit stack (no recursion)\nstack = [(0, 0)]\nmaze[1, 1] = 0  # Mark starting cell as path\n\nwhile stack:\n    x, y = stack[-1]\n    directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]\n    np.random.shuffle(directions)\n\n    found = False\n    for dx, dy in directions:\n        nx, ny = x + dx, y + dy\n        if 0 <= nx < WIDTH and 0 <= ny < HEIGHT and maze[ny * 2 + 1, nx * 2 + 1] == 1:\n            # Carve wall between current and next cell\n            maze[y * 2 + 1 + dy, x * 2 + 1 + dx] = 0\n            maze[ny * 2 + 1, nx * 2 + 1] = 0\n            stack.append((nx, ny))\n            found = True\n            break\n\n    if not found:\n        stack.pop()\n\n# Create openings for start (top-left) and goal (bottom-right)\nmaze[1, 0] = 0  # Start entrance\nmaze[HEIGHT * 2 - 1, WIDTH * 2] = 0  # Goal exit\n\n# Convert maze to rectangle data for Altair\nrectangles = []\nfor row in range(maze.shape[0]):\n    for col in range(maze.shape[1]):\n        if maze[row, col] == 1:\n            rectangles.append({\"x\": col, \"x2\": col + 1, \"y\": row, \"y2\": row + 1})\n\ndf_walls = pd.DataFrame(rectangles)\n\n# Create markers for Start and Goal (positioned closer to entrances)\nmarkers = pd.DataFrame(\n    [\n        {\"x\": -0.5, \"y\": 1.5, \"label\": \"S\"},  # Start marker at entrance\n        {\"x\": WIDTH * 2 + 0.5, \"y\": HEIGHT * 2 - 0.5, \"label\": \"G\"},  # Goal marker at exit\n    ]\n)\n\n# Create the maze walls using mark_rect\nmaze_chart = (\n    alt.Chart(df_walls)\n    .mark_rect(color=INK)\n    .encode(x=alt.X(\"x:Q\", axis=None), x2=\"x2:Q\", y=alt.Y(\"y:Q\", axis=None, scale=alt.Scale(reverse=True)), y2=\"y2:Q\")\n)\n\n# Create start/goal markers\nmarker_chart = (\n    alt.Chart(markers)\n    .mark_text(fontSize=56, fontWeight=\"bold\", color=BRAND)\n    .encode(x=alt.X(\"x:Q\", axis=None), y=alt.Y(\"y:Q\", axis=None, scale=alt.Scale(reverse=True)), text=\"label:N\")\n)\n\n# Create title text\ntitle_df = pd.DataFrame([{\"x\": (WIDTH * 2 + 1) / 2, \"y\": -3, \"text\": \"maze-printable · altair · anyplot.ai\"}])\n\ntitle_chart = (\n    alt.Chart(title_df)\n    .mark_text(fontSize=32, fontWeight=\"bold\", color=INK)\n    .encode(x=alt.X(\"x:Q\", axis=None), y=alt.Y(\"y:Q\", axis=None, scale=alt.Scale(reverse=True)), text=\"text:N\")\n)\n\n# Combine layers\nchart = (\n    alt.layer(maze_chart, marker_chart, title_chart)\n    .properties(width=1600, height=1600, background=PAGE_BG)\n    .configure_view(strokeWidth=0)\n)\n\n# Save with theme-suffixed filenames\noutput_dir = os.path.dirname(os.path.abspath(__file__))\nchart.save(os.path.join(output_dir, f\"plot-{THEME}.png\"), scale_factor=3.0)\nchart.save(os.path.join(output_dir, f\"plot-{THEME}.html\"))\n"}