Hexagony
The source is folded into a hexagon. Six instruction pointers wait in its corners, and memory is a hexagonal grid too.
Martin Ender, 2015 · Turing complete: yes · ●●● hard · esolangs.org
Tested with 1 example and 1 known program · Hello World on Try It Online: identical · Test method
Run it
Hello World
Expected output
Hello, World!
Output
The idea
Martin Ender made Hexagony in 2015. Most grid languages use a square grid because text is square. Hexagony refuses: it takes your program, strips the whitespace, and lays the characters out row by row in a regular hexagon. The instruction pointer then moves in one of six directions.
Read the story
Everything is hexagonal. There are six instruction pointers, one in each corner, and only one is active at a time. Memory is not a tape but the edges of an infinite hexagonal grid, and the memory pointer sits on an edge facing one of its ends.
The result is famously hard to write by hand. People use visualisers, and the best programs are puzzles that fold a long path into a tiny hexagon.
The machine
Layout. A hexagon of side n holds 1, 7, 19, 37, … cells (3n² − 3n + 1). The source is padded with . (no-op) up to the next such number. Seven characters make side 2:
a b
c d e
f g
Movement. The active IP starts in the top-left corner moving east. When it leaves through an edge it re-enters on the opposite edge. When it leaves through a corner, pointing at that corner, the current memory value picks the way: positive continues on one row, zero or negative on another.
Memory. Each edge holds an arbitrary-size integer, starting at 0. Binary operations combine the two edges in front of the memory pointer, its "left" and "right" neighbours.
| Char | Does |
|---|---|
| letters | Set the current edge to the letter's character code |
0–9 | Multiply the edge by 10 and add the digit |
) ( ~ | Increment, decrement, negate |
+ - * : % | Combine the left and right neighbours into the current edge |
, ? | Read a byte, read a number |
; ! | Print the edge as a byte (mod 256), as a decimal number |
_ \| / \ | Mirrors |
< > | Mirrors or branches, depending on the incoming direction and the edge's sign |
$ | Skip the next cell |
[ ] # | Switch to the previous IP, the next IP, or IP number (edge mod 6) |
{ } " ' = ^ & | Move the memory pointer, turn it, or copy a neighbour |
@ | Stop |
Example
Print Hi in five characters:
Print Hi in five characters:
H;i;@
Five characters pad to seven, so the hexagon has side 2:
H ;
i ; @
. .
Trace: the IP starts on H, moving east. H sets the edge to 72 and ; prints H. The IP runs off the top row and wraps to the start of the middle row. i sets the same edge to 105, ; prints i, and @ stops. This was traced by hand against the wrapping rules in the reference README, and this implementation agrees.
Quirks
- A run stops after 3,000,000 steps.
Try this
- Lay out
abcdefghijklmnopqrs(19 characters) as a hexagon of side 3 on paper. - Change the example to print
Hey. How many characters do you need now, and what is the side length? - Hard: write a program that prints its input number doubled, using
?,{,}and+.
Patterns it teaches
- Two-dimensional control flow: six directions instead of four, and edges that wrap in pairs.
- Turing completeness: any Brainfuck program can be translated into Hexagony, with the memory grid playing the tape.
- Code golf: a hexagon of side 3 has only 19 cells, so golfers fold code until paths share cells.
Further
- esolangs.org/wiki/Hexagony
- The reference implementation and README by Martin Ender (github.com/m-ender/hexagony), with the full wrapping rules.
- Related: Labyrinth (same author), Stack Cats (co-designed by him), ><>.
See also
Two-dimensional languages
Cell-based
Write your own in Run Convert a program from Hexagony
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: hexagony.
Command line
shib run hexagony -e ' H ; e ;
l ; d ; *
; r ; o ; W
l ; ; o ; * 4
3 3 ; @ . >
; 2 3 < \
4 ; * /'shib manual hexagonySource
- Module
langs/grid/index.js- Interpreter
langs/grid/hexagony.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org