What is Brainfuck?
Brainfuck is an esoteric programming language, meaning that it's not very useful for any practical purposes, but it can be a great source of programming puzzles. It is an extremely minimalistic cellular automaton, and is meant to be similar to a Turing Machine. A brainfuck-executing machine consists only of a series of cells arranged linearly, each of which can store numerical values, and a pointer that can move between adjacent cells. This system can be programmed using combinations of only eight basic commands:
- + increments the numerical value in the current cell.
- - decrements the numerical value in the current cell.
- > moves the pointer to the right.
- < moves the pointer to the left.
- , accepts input and stores it in the current cell.
- . outputs the value of the current cell.
- [ and ] are used to create loops. The code between two brackets is executed repeatedly until the currently selected cell contains a value of zero. More specifically, [ translates to 'if the current cell is zero, skip ahead to the corresponding ], else proceed to next command' and ] translates to 'if the current cell is zero, proceed to next command, else jump back to the corresponding ['.
There are multiple different ways of implementing Brainfuck. This interpreter supports two different versions of Brainfuck: numerical mode and ASCII mode. In numerical mode (the default), input consists of a series of comma-separated numbers, and cells can store any arbitrarily large positive integer value (but cannot go below zero). In ASCII mode, any ASCII string will be accepted as input and the characters are translated into numbers and entered into cells one-by-one, and numerical output is also translated back into ASCII before being displayed. Additionally, since there are only 256 ASCII characters, ASCII mode uses "byte cells" that can only contain integers between 0 and 255. Attempting to decrement 0 will produce an error in numerical mode, but it will "wrap around" to 255 in ASCII mode.
Here are a couple more conventions that this interpreter uses:
- There are finitely many cells, and the number of cells must be specified before code is executed.
- Attempting to move left from the leftmost cell or right from the rightmost cell results in an error.
- The pointer starts on the leftmost cell.
- If the program requests input and none is given, '0' is taken as the input.
Also, be wary of running your Brainfuck code with 'hide steps' turned on. If your code enters an infinite loop, it could temporarily crash the intepreter. p>
What the heck am I supposed to do with this?
Here are some simple, easy-to-deconstruct programs you can try in numerical mode:
- ,. echoes back your input. (1 cell)
- ,[-.] counts down to zero starting from the input number. (1 cell)
- ,>,[-<+>]<. adds two inputs. (2 cells)
- ,[->++<]>. returns double the input. (2 cells)
...and here are a few for ASCII mode:
- ,[.,] echoes back your input. (1 cell)
- ,[..,] echoes every character of your input twice. (1 cell)
- ,[.,,] echoes every other character of your input. (1 cell)
- ,[.,]++++++[->+++++<]>+++. adds an exclamation point to the end of your input. (2 cells)
Here are two more complicated examples. The code below (to be run in numerical mode with two cells) prints out the powers of two in increasing order until it either crashes or is aborted:
This code uses 13 cells in ASCII mode to print the text 'BRAINFUCK!'
Want to try your hand at writing Brainfuck code? Here are four types of fun challenges you might want to try:
Pick a simple function and try to implement it with the shortest code possible.
- Return the number 365. Or pick a random number between 100 and 1000 and write a program with as few commands as possible that returns that number. (numerical)
- Print 'Hello World!' (ASCII)
- Write a function that takes arbitrarily many inputs and returns the maximum value from among them. (numerical)
- Given a string, remove all of the spaces from it and return the result. (ASCII)
Implement a simple function using only some small fixed number of cells.
- Given a numerical input and using only three cells, count up to that input, returning all of the positive integers less than or equal to it. (numerical)
- Calculate the square of a number using only three cells. (numerical)
- Using four cells, create a function that accepts two numerical inputs and checks whether they are the same, returning 1 if they are equal and 0 otherwise. (numerical)
Build a function that isn't quite so simple - complex algorithms get ugly fast in Brainfuck.
- Given a list of positive integers, return them in order from least to greatest. (numerical)
- Given a string of mixed uppercase and lowercase alphabetical characters as input, convert them all to uppercase. (ASCII)
- Return the binary digits of an input number. (numerical)
- Given two base-ten numbers separated by spaces, return their sum (in base ten). (ASCII)
Given some pre-written Brainfuck code, try to figure out what it does and understand how it works.
- >,[>,]<[.<](ASCII, input string of upper/lowercase alphabetical characters, at least 11 cells)
- ,+[[>+>+<<-]>>-[-<<+>>]<[->++<]>[-<+>]<<]>--[----<+>]<.(numerical, one input, at least 3 cells)