BF-RLE

From Esolang
Jump to: navigation, search

One of the more useful ways of extending brainfuck is to use Run-length encoding to reduce the number of characters needed to enter a brainfuck program.

However, there are many minor variations on the exact methodology. Generally this is only useful for the cell editing commands >, <, + and -.

Base 10, prefix

This is probably the most obvious as it uses decimal numbers and reads in the normal English order "Twelve plusses". The decimal numbers are used to prefix the standard brainfuck commands, so the sequence ++++++++++++ is encoded as 12+.

This gives a trivial "Hello World!" as below.

[-]72+.[-]101+.[-]108+.[-]108+.[-]111+.[-]32+.[-]87+.[-]111+.[-]114+.[-]108+.[-]100+.[-]33+.[-]10+.

Base 10, suffix

Instead of prefixing the command it's also reasonable to make the instruction count a suffix to the relevant command character. The sequence ++++++++++++ would be encoded as +12.

[-]+72.[-]+101.[-]+108.[-]+108.[-]+111.[-]+32.[-]+87.[-]+111.[-]+114.[-]+108.[-]+100.[-]+33.[-]+10.

While, initially, this may seem less closely related to the English description, it is sometimes considered more reminiscent of mathematical nomenclature. This may be more obvious if a * symbol is included to explicitly describe the multiplication concept as in BF_Joust.

Other bases

Any base can be used, a base 62 numbering system with the digits going from 0-9, A-Z, a-z achieves the highest reasonable density with plain ASCII. However, base36 or below can use case-insensitive letters.

Standard base64 uses + as a digit, so only non-standard variations (eg using ~ and _) are usable.

The compression system BFC uses hexadecimal prefix style and the character _ as a replacement for [-].

Base 2

Base two allows an interesting variation. This example has = mapping to [-], another rather obvious construction. But the first character printed would usually be something like [-]1001000+., in this case rather than using two digits the binary number has been transformed to +**+***, the * character is a zero while the command character is used as a one.

=+**+***.=++**+*+.=++*++**.=++*++**.=++*++++.=+*****.=+*+*+++.=++*++++.=
+++**+*.=++*++**.=++**+**.=+****+.=+*+*.

Offsetting counts

Using a count of zero, one or two is obviously not useful, as it doesn't shrink the size of the code. This means a minor optimisation might be to assume that a count of zero actually means that the command character is repeated three times, with all the other counts shifted appropriately. This reduces the final size a little more.

Brainfuck to BF-RLE

When converting to these encodings non-command characters have to be stripped before conversion. It is reasonable that the generated number should be directly beside the command character (no line breaks between them), but otherwise it is reasonable to include line-breaks so the lines don't get too long and perhaps other whitespace.

External resources