Shorten your Brainfuck code

From Esolang
Jump to navigation Jump to search

These are quick and dirty golfing techniques for brainfuck.

Use Overflow

Although not specifically a part of the specification, most brainfuck implementations use 8-bit wrapping cells. This means that incrementing 255 gives 0, and decrementing 0 gives 255.

For example, using a simple method to print `ÿ`, codepoint 255, the code is:

>+++++++++++++++++[<+++++++++++++++>-]<.

However, if wrapping is allowed the code can be shortened only 2 bytes!

-.

There are many techniques that wrapping allows, like [-] or [+] which both equivalently set a cell to 0 regardless of the sign of the current value.

Combine small numbers in your code

Golf Question: Print @ in the console.
Actual question is at this link.
That is on Anarchy Golf.
If you had that stupid brain, you'd have a code like this:
++[->++[->++[->++[->++[->++<]<]<]<]<]>>>>>.
Then, your brain will explode, and you will lose that contest.
A better way to do that is to combine the 2s and it will be a lot shorter(Don't forget to delete the loops!):
++++[->++++[->++++<]<]>>.
"Uh oh, I still lost that contest!!!"
Your brain probably isn't working!
There is a better way to do that:
++++++++[->++++++++<]>.
2 bytes shorter!!
Like what you may say, the code can still combined!
But, if we combine that, the code will be like this:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
You'd be f*cked(I mean curse) by that contest!
You will lose!
Actually, using overflow, the code can be a lot shorter:
----[---->+<]>+.
m[n>+<] is the way to divide m by n;

Read the question carefully

You'd better read the question yourself.
You should never set it by yourself!
A better way to do that is:
,.

Use the visualizer

Link:Brainfuck Visualizer
If you use that visualizer, all Brainfuck codes will be understood! Why didn't anyone use that simple tool?
Or, you can use paper and pencil(And an eraser) to keep track with the cells.
You also need an ASCII list (shown in the bottom of the page).
  • Draw an array.
  • Concentrate on the 1st cell.
  • Look at the Brainfuck code.
    • If the code is +:
      • Erase the cell you are concentrating.
      • Change the cell into the cell state before + 1.
    • Else if the code is -:
      • Erase the cell you are concentrating.
      • Change the cell into the cell state before - 1. are concentrating
    • Else if the code is >:
      • Concentrate on the next cell.
    • Else if the code is <:
      • Concentrate on the cell before.
    • Else if the code is ,:
      • Look in the input buffer.
      • Search for the char in the ASCII list.
      • Erase the cell you are concentrating, and write it as the ASCII number of the char.
    • Else if the code is .:
      • Search the ASCII number avisiblend write the char in the output buffer.(For invisible chars, if it is not newline or tab or space, just write '?'.)
    • Else if the code is [:
      • If the cell you are concentrating is 0:
        • Jump to the nearest same-level ].
    • Else if the code is ]:
      • If the cell you are concentrating is not 0:
        • Jump to the nearest same-level [.
  • Increment the code concentration.
  • repeat until you get the end of the code.

Give up

Giving up is the simplest way to do that.
When you give up, another way may pop into your Brain!
You can ask others about it! They may give a better answer!

The classic multiplication

n[->n<]>
n is the number multiplied.
You can even do this:
n[>n<-]
I concluded it from Brainfuck constants.(For more help, see that.)
Well, for prime numbers:
For example 17.
No numbers except for 1 and 17 get 17.
Let's think of it for another way.
17-1=16, it isn't a prime!
Use the multiplication and add one!
++++[->++++<]>+
Then, you knew how to print any number in the shortest way!
(Some may not work, but they are only 1 or 2 bytes.)
Talk to administrators: I didn't copy anything. Please don't block me if you read something similar before.

The PHP Golfer's advice

The PHP Golfer: Never continue to program Brainfuck again. That @ question, using PHP, has only one byte!
The Brainfuck Golfer: What!? That is too like cheating, actually.
Maybe you should really give up Brainfuck and use PHP.

Understand the Brainfuck Commads

Why would you be not able to write even 1 byte?
Because, you forgot the commands!
Putting a chart here would help you.
>	increment the data pointer (to point to the next cell to the right).
<	decrement the data pointer (to point to the next cell to the left).
+	increment (increase by one) the byte at the data pointer.
-	decrement (decrease by one) the byte at the data pointer.
.	output the byte at the data pointer.
,	accept one byte of input, storing its value in the byte at the data pointer.
[	if the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] command.
]	if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [ command.
How can you shorten it if you didn't even understand the commands?

Don't Repeat Yourself(DRY)

Repeating is never good for golfing. Take this simple example which prints ROBB:
++++++++[>++++++++++<-]>++.>++++++++[>++++++++++<-]>-.>++++++[>++++++++++<-]>++++++.>++++++[>++++++++++<-]>++++++.
But, if you don't repeat yourself, the code would be:
++++++++[>++++++++++<-]>++.>++++++++[>++++++++++<-]>-.>++++++[>++++++++++<-]>++++++..
A lot shorter!
Take this as another example:
--------------------------------
And my brain almost explodes!
It clears the cell vith the value 32.
If you don't repeat yourself, and put the - inside a loop, then:
[-]
A lot shorter!
So, never ever repeat yourself!!

Big width loops(for easier byte printing)

Golf question:Print "HhX ".
Normal one:
++++++++[->++++++++<]>++++++++.<+++++[->+++++<]>+++++++.<++++[->----<]>.<+++++++[->-------<]>-------.
Big Loop version:
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>.>.>.
A lot shorter, huh?
To make it simpler, try this:
++++++++[>+++++++++>+++++++++++++>+++++++++++>++++<<<<-]>.>.>.>.
That's still a lot shorter!

But you'd still lose.
++++++++[->+++++++++<]>.--[--<+++>]<-.>++++[<---->-]<.-[--->+<]>+++.
or
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>-<<<<-]>>.>.>.>.
(nested loops are better, but don't nest too much.)
or
+++++++++++++++[>+++++>+++++++>++++++>++<<<<-]>---.>-.>--.>++.
(Try to make the starting number, or the loop counter larger---it makes the code shorter)
or
-[[+>]+[+++<]>-]>>>>>.<<.>.>>>--------.
(check "Brainfuck Constants"---many good golfed code there)

Average Numbers

You should better find an average number.
Like, what is the average of 35, 57,41, and 50?
We round them up. 35 is to 30, 57 to 60, 41 to 40, and 50 to 50.
Remember to Round to 10s! (Or it will be difficult to implement)
Pick the nearest number.
Golf question: print #9)2.
What!?
Normal answer:
+++++[->+++++<]>++++++++++.<++++[->++++<]>++++++.<++++[->----<]>.+++++++++.
Shortened answer:
++++++++++[>+++>++++++>++++>+++++<<<<-]>+++++.>---.>+.>.
A lot shorter!!!

Golf question

Truth-machine!
,.------------------------------------------------[++++++++++++++++++++++++++++++++++++++++++++++++[.]]
What!? So long!!
Indeed! We do have a shorter one!!!
,.>++++++[-<-------->]<[>++++++[-<-------->]<[.]]
Oops! That is too long.
What? So bad that you lost that contest again! What the f*ck!
Indeed, we can shift the input to the next bytes:
,.[->+>+<<]++++++[->--------<]>[>[.]]
What? I still lost the contest!!

Really? A shorter one

,[.-[-->++<]>+]
Wow! So short!
Don't be too excited(and make you laugh to death)!
Let us knoe what are the new commands:
- has another use: to change even values odd, and to change odd values even.
Also,[-->++<] is to move a value to the next byte--only if the value is even. If the value is odd, it will enter an infinite loop.
That is written by myself.
Unluckily, that does not terminate on 0. T_T

An improvement

Wise Ais523 wrote a better version.
>>>,.[[->+<<+>]>-]<<<[<<]>[.]
Explanation ready...

Explanation

First, for
>>>
The 1st is for even, the second is for odd, the third is for "Middlle Distribution".
Anyway, what is that??
That is:
[->+<<+>]
It distributes the number to the sides.
Also:
[-<+>>+<]
is also okay.
What for:
[[->+<<+>]>-]
In the middle, it IS the "Middle Distribution!!"
Then, for ">-" ?
That is, mode right and subtract 1(or it cannot perform the middle distribution, and if you don't subtract 1, that will run forever.)
The middle distribution always leaves another space in the middle.

Jumping

<<<
That is jumping back 3 bytes.
[<<]
Repeat:Jump back 2 bytes.
Jumps back to odd or even bytes.
>
move right.
[.]
repeatedly print the current byte.
When that is 0, jumps over the byte.

External Links

Need help? [1]

See also

Also, for cheating:Brainfuck constants And Brainfuck algorithms Or maybe even look at the brainfuck page!