B-ZAR
B-ZAR is a two-dimensional programming language. Each character in a program is a command, and these commands are set in a grid. On this grid moves a hypothetical turtle. Each command is executed when the turtle moves over it. Some commands affect the turtle's movement.
Other commands affect the variables attached to the language. B-ZAR keeps track of an array of eight integer variables. These variables are labeled [1] through [8]. B-ZAR also maintains a pointer. The value of the pointer is the name of the "current" integer. The notion of a current integer is important to several of the commands.
Still with me? Excellent.
Commands
S: Start the program here. Release the turtle, facing it to the right.
F: Stop the turtle here. Output the current integer.
V, <, ^, >;: Arrows. Turn the turtle to face the direction of the arrow.
: Do nothing. The turtle will travel straight through this space.
+: Add 1 to the current integer.
-: Subtract 1 from the current integer.
]: Add 1 to the pointer. If the pointer would otherwise become 9, set the pointer equal to 1.
[: Subtract 1 from the pointer. If the pointer would otherwise become 0, set the pointer equal to 8.
?: This is the only branching command in the language:
If the current integer is positive, turn the turtle ninety degrees to its right.
If the current integer is negative, turn the turtle ninety degrees to its left.
If the current integer is zero, let the turtle pass straight through.
Notes: in each program, the pointer begins at 1. All unspecified variables default to 0. The "cost" of a program - essentially the file size - is the area of the smallest rectangle that follows grid lines and encompasses the entire program.
Example
Here's a simple example. [1] is some integer. Output the absolute value of [1].
V< +]F [+] >?^ S?F F
See how it works? If [1] is zero or more, the program ends immediately. If not, the program increases [1] and [2] together until [1] = 0, and it then outputs [2]. The cost is 3*6, or 18.
Applet
B-ZAR applet - This is a Java applet to help create and execute B-ZAR programs.
Puzzles
Try writing programs with the following effects. The puzzles are listed roughly in order of difficulty. To make life more interesting, minimize the cost. The lowest cost of a functioning program I have seen, and the author of that program, is shown in parentheses. If you can beat it, absolutely send me your answer.
[1] and [2] are integers. Output their sum. (21, Matt Elder)
[1] is an integer. Output 2 times that integer. (25, Matt Elder)
[1] is n, an integer between 1 and 7 inclusive. Output the sum of the next n variables in the array. (Variables [2] through [n+1]) (40, Michael Bennett)
[1] is a positive integer. Output the sum of all positive integers less than or equal to [1]. (48, Matt Elder)
[1] and [2] are integers. Output their product. (143, The Dark and CrystyB)
[1] is n, a positive integer. Output n! (143, Matt Elder)
[1] is a and [2] is b, both integers. Output a, discarding the remainder. (247, CrystyB)