← Mon, Nov 30 digest

Rail Cube in Prolog

read article ↗ hn thread ↗

A developer built a Prolog solver for the Rail Cube 3D train puzzle, beating the high score to 47 and exhaustively counting 14.1 billion valid layouts.

read it The deep dive into Prolog state representation, custom pruning heuristics, and exhaustive enumeration makes this highly rewarding for systems and logic programmers.

summary

An engineer implements a puzzle solver for Rail Cube—a 3D toy train set with tracks that run on walls and upside down—using Prolog without external constraint libraries. By applying strict pruning rules and leveraging the layout's intersecting X block to split the search space, the program finds a 47-point layout forming a trefoil knot. Furthermore, an exhaustive enumeration counts 14,137,062,002 distinct valid layouts, generating a massive 523 GB solution database. The write-up details the state representation, geometric constraints, and canonical string normalization used to eliminate duplicate rotations and reflections.

key points

  1. The solver runs under Scryer and Trealla Prolog without constraint libraries, using ordinary recursion and arithmetic.
  2. Pruning strategies—including distance bounds, parity checks, and precomputed endings—reject invalid tracks early to make the exhaustive search viable.
  3. An exhaustive count revealed 14,137,062,002 distinct valid layouts, exported to a 487 GiB text file.
  4. A canonicalization algorithm standardizes starting positions and travel directions to prevent duplicate counts from rotations and reversals.

who should read it

Prolog programmers, puzzle enthusiasts, and software engineers interested in constraint solving and search space optimization.

Generated by an LLM from the article and thread. Check the sources before relying on it.