We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.

Square

From Esolang
Jump to navigation Jump to search

Square is an esoteric programming language created in 2026 by Andrew Bayly in which the obfuscation mechanism is a block permutation puzzle. The programmer writes ordinary JavaScript, and an encoder scrambles it into a rectangular grid of 3×3 character blocks. An interpreter must search through all possible arrangements to recover and execute the original program.

The encoding is trivially fast. Decoding is combinatorially explosive. This asymmetry is the point.

Concept

A Square program is a shuffled grid of 3×3 character blocks. Each block contains nine characters of escaped source code. The blocks are arranged into the squarest possible rectangle and separated by blank lines. To execute the program, an interpreter must find the permutation of blocks that reassembles into valid JavaScript — then run it.

The search space is N! where N is the number of blocks. This grows superexponentially with program length: a short program may be decoded in seconds, while a program of modest length may require longer than the age of the universe to decode. The language has a natural complexity hierarchy determined entirely by how many characters the programmer writes.

Encoding

Escape sequences

Before blocking, the source is escaped so that whitespace and backslashes can be represented as printable two-character sequences:

Character Escaped form
Space \s
Newline \n
Tab \t
Backslash \\

All other characters are passed through unchanged.

Encoder pipeline

  1. Escape the source using the table above
  2. Pad the escaped source with # characters until its length is a multiple of 9
  3. Cut the padded string into blocks of 9 characters each
  4. Shuffle the blocks randomly
  5. Determine the grid dimensions: find the factor pair (W, H) of N (the block count) with W ≤ H that minimises |W − H|. This gives the squarest possible rectangle, with W columns and H rows of blocks
  6. Output the grid: H row-groups, each containing W blocks displayed side by side (3 characters wide each, separated by a single space), 3 lines tall. Row-groups are separated by a single blank line

Example

The source console.log('hello') (20 characters) escapes unchanged, is padded to 27 characters, and cut into 3 blocks:

console.l
og('hello
')#######

N = 3, layout = 1×3 (one column, three rows). After shuffling, a valid Square program might look like:

og(
'he
llo

')#
###
###

con
sol
e.l

Interpretation

Interpreter pipeline

  1. Parse the grid: split on blank lines to recover H row-groups; split each group's 3 lines on spaces to recover the N blocks
  2. Enumerate all N! permutations of the blocks using Heap's algorithm
  3. For each permutation, concatenate the blocks, unescape the result, and attempt to compile it as JavaScript
  4. The first permutation that produces syntactically valid JavaScript is executed

Execution semantics

The interpreter executes the first permutation whose unescaped content is syntactically valid JavaScript. This is not guaranteed to be the original program. Any permutation that happens to form valid JavaScript — however semantically unrelated to the original — is a legitimate execution of the Square program.

For short programs (small N), the search space is small and many permutations may produce syntactically valid JavaScript by chance, making false positives common. For long programs, a random concatenation of blocks almost never satisfies JavaScript's structural requirements, making the correct arrangement effectively unique — but the search takes cosmologically long to reach it.

The language is inherently probabilistic: encoding a program does not guarantee its recovery.

Complexity

The search space is N! permutations, where N = ⌈escaped_length / 9⌉.

Program N Grid N! Approximate decode time
console.log('hello') 3 1×3 6 Instant
Primes, ungolfable (88 chars) 10 2×5 3,628,800 ~15 seconds (unreliable)
Primes, golfed (114 chars) 13 1×13 6,227,020,800 ~13 hours (measured)
Primes, minified (169 chars) 20 4×5 2,432,902,008,176,640,000 Age of the universe
Primes, formatted (240 chars) 36 6×6 ~3.7 × 1041 Vastly longer

Decode times are measured on a contemporary browser running the reference implementation. The "unreliable" annotation indicates that false positives are likely at this block count: the interpreter will find and execute a syntactically valid JavaScript program, but it may not be the intended one.

The ungolfable primes program was written by the language's creator as an empirical test of the false positive boundary, spending approximately one hour golfing it to its present form.

Note that the formatted primes program produces a perfect 6×6 square grid — an accidental consequence of its length.

The golfing incentive

Because decode time grows as N!, Square programmers are strongly incentivised to minimise source length. Whitespace is particularly expensive: each space or newline becomes a two-character escape sequence, increasing the escaped length and potentially pushing N up by one, multiplying the decode time by N. Well-formatted code and terse code occupy entirely different complexity classes in Square.

The false positive trap

There is a tension between golfing and reliability. Short programs decode quickly but are vulnerable to false positives — scrambled arrangements that happen to parse as valid JavaScript. Longer programs are unambiguous but take impractically long to decode. The zone where programs are both fast to decode and reliably recovered is very narrow, and even within it decode times are measured in hours.

Notes on the grid layout

The grid dimensions are chosen to minimise |W − H| subject to W ≤ H and W × H = N. Consequences:

  • If N is a perfect square, the grid is square (e.g. N=36 gives a 6×6 grid)
  • If N has a factor pair close to its square root, the grid is nearly square
  • If N is prime, no rectangular layout exists and the grid degenerates to a single column (1×N), one block per row-group

The layout is self-describing: the interpreter infers W from the number of space-separated segments on the first line of any row-group, and H from the number of row-groups.

Padding character

The character # is used for padding because it does not require escaping and is visually distinct. It is silently discarded during unescaping. Note that # also appears in valid JavaScript (private class fields), so its presence in source code is handled correctly by the escape/unescape pipeline — only trailing padding # characters at the end of the last block are semantically inert.

Implementation

The reference implementation is a self-contained HTML/JavaScript file providing an encoder and interpreter with a browser-based interface. It is available at:

The encoder runs instantly for any program length. The interpreter uses Heap's algorithm with asynchronous batched execution to remain responsive in the browser, with a live permutation counter and estimated time remaining.

See also

  • Malbolge — another esolang where program execution is intentionally difficult
  • JSFuck — valid JavaScript written using only six characters