Programming Puzzles

Tal Schuster, Ashwin Kalyan, Alex Polozov, Adam Tauman Kalai

TL;DR

The authors introduce Python Programming Puzzles ($P^3$), an open-source benchmark that formulates coding challenges entirely as short, executable Python verifier functions ($f(y) == \text{True}$). By framing synthesis as an objective NP-search problem, $P^3$ removes the ambiguity of natural language prompts and the need for hidden unit test suites. Leveraging this self-verifying property, both language models (Codex, GPT-3) and enumerative search algorithms can bootstrap their own training without reference solutions, revealing a striking correlation between human and machine puzzle difficulty.

What Problem Does This Paper Address?

Traditional program synthesis benchmarks typically evaluate systems using either Programming by Example (PbE, which heavily underspecifies general algorithms) or natural language problem descriptions (which are inherently ambiguous and conflate language comprehension with algorithmic reasoning). Furthermore, standard evaluations rely on hidden test sets, answer keys, or brittle output comparisons.

The authors address this by reframing programming challenges into self-contained programming puzzles: a puzzle is defined by the source code of a verifier function $f$. A solution is any input $y$ (or generating program $g$ where $g(x) = y$) that causes $f$ to return True within a set timeout. Because the specification is executable code, candidates are verified unambiguously without external test harnesses or reference solutions.

Methods

  • The Puzzle Formalism: Formally placed in the complexity class NP, any problem verifiable in polynomial time can be cast as a puzzle. The goal is to synthesize $y$ or a short program $g(x)$ such that $f(g(x), x) == \text{True}$.
  • Grammar-Guided Enumerative Solvers:
  • Builds Abstract Syntax Trees (ASTs) top-down over a core Python grammar.
  • Uses specialized COPY rules allowing the solver to reuse variables and constants directly from the verifier AST.
  • Models rule probabilities using a baseline Uniform distribution, a Random Forest (bag-of-rules context), and a Transformer (a RoBERTa model pretrained on GitHub code).
  • Self-training / Bootstrapping: Without reference solutions, models fit their parameters iteratively on parent-child AST rules extracted from previously solved puzzles across multiple training cycles.
  • Autoregressive LLM Solvers: Evaluated GPT-3 and Codex across different prompt formats: Short (zero-shot verifier code), Medium (5-shot tutorial puzzles), Long (5-shot + natural language docstrings), and Bootstrapping (dynamically appending newly solved puzzles into the context).

Datasets

  • Python Programming Puzzles ($P^3$): Introduced in this work.
  • v0.1: 200 problems (138 supported by the enumerative grammar).
  • v0.2: 397 problems spanning 18 domains—including string manipulation, classic recursion (Tower of Hanoi), chess, dynamic programming (Codeforces, ICPC, IMO), and open problems in mathematics and cryptography (e.g., finding Collatz cycles, integer factoring, and learning parity with noise).
  • CodeSearchNet / GitHub Python Repositories: Used for pre-training the enumerative solver’s RoBERTa encoder.

Key Insights and Ideas

  • Answer-Key-Free Bootstrapping: Because verifiers are self-contained and deterministic, models can definitively check their own candidate solutions. Solvers can self-train on their own discovered programs without human intervention or labeled data—mirroring self-play in game AI (e.g., AlphaZero).
  • Human and Machine Difficulty Correlate: In a study with 21 human programmers (beginners vs. experienced), puzzles that took humans longer to solve also required significantly more attempts from AI solvers (Spearman's rank correlation $\rho = 0.512$ for GPT-3 and $0.563$ for Codex).
  • Diminishing Returns of Raw Compute: While providing more attempts increases solve rates, the scaling is logarithmic. Codex required around 1,000 attempts per puzzle to match the accuracy of beginner human programmers on a 30-puzzle subset.

Key Results

  • Enumerative Bootstrapping: On the 138-puzzle v0.1 subset, the Transformer-guided enumerative solver with bootstrapping solved 76 problems at $k = 10^6$ tries—a 43% improvement over the naive uniform search baseline (53 solved).
  • Codex Performance: Codex decisively outperformed GPT-3 and enumerative search. On $P^3$ v0.2 (397 problems) using the natural-language-augmented Long prompt:
  • Codex-davinci: Achieved 18.3% pass@1, 48.7% pass@10, and 79.8% pass@1,000.
  • Codex-cushman: Achieved 14.9% pass@1 and 76.5% pass@1,000.
  • Human Study: Beginners solved an average of 22.9 / 30 puzzles (194 seconds/puzzle), while experienced coders solved 26.2 / 30 (149 seconds/puzzle).

Limitations and Future Work

  • Exclusions by Design: The benchmark cannot accommodate "soft" commonsense reasoning, date/fact recall, image synthesis, or interactive problems that do not fall neatly into deterministic polynomial-time verification (NP).
  • Low Single-Try Success: Even the strongest model (Codex Davinci) achieved under 20% on pass@1, showing that out-of-the-box zero-shot algorithmic synthesis remains a steep challenge.
  • Unsolvable Problems: Because some problems encode open conjectures (such as disproving Collatz), the theoretical maximum score on the dataset is strictly $< 100\%$.
  • Future Directions: Expanding $P^3$ via competitive programming scrapers, designing hybrid approaches that combine LM proposal generation with symbolic search, and developing more sample-efficient bootstrapping strategies.

Summary updated

BibTeX
@inproceedings{schuster2021programming,
  author = {Tal Schuster and Ashwin Kalyan and Alex Polozov and Adam Tauman Kalai},
  title = {{Programming Puzzles}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2021},
  url = {https://openreview.net/forum?id=fe_hCc4RBrg},
}
Figure