11
3
BrainFlow
What is BrainFlow?
BrainFlow is an extension of BrainF**k (BFk) with 3 additional commands for added functionality and confusion.
What commands?
In addition to the normal BFk commands, we also have:
^ Jumps to the cell # depending on the value in the cell. Ex: If we are at cell # 0 with a value of 4, ^ will jump us to cell # 4.
= Sets the value at the cell to the index of the cell. Ex: If we are at cell # 4 with a value of 0, = will set our value to 4.
& Will set the value at the current cell equal to the value at the cell based on the value in our current cell. (This one's hard to word, so here's an example!) Ex: We are at cell # 33 and our current value at this cell is 7, & will set our current value at cell # 33 to whatever value is in cell # 7.
Optional Challenges
Accomplishing any of the following will apply the specified bonus to your byte count.
Interpreter written in BrainFlow
(Can be interpreted by the sample and contains at least one meaningful ^ = or &): Score / 3
Interpreter written in BrainF**k:
Score / 2
Doesn't contain any English letters (in either upper or lower case):
Score - 20
Doesn't contain any of the BrainFlow / BFk commands in the interpreter itself:
Score - 50
Example
An example Java interpreter:
import java.util.Scanner;
public class Interpreter {
private String exp;
private int[] values = new int[256];
private int index = 0;
private Scanner in;
public Interpreter(String exp, Scanner in){
this.exp = exp;
this.in = in;
}
public void run(){
//Reset index and values
for(int i = 0; i < values.length; i++){
values[i] = 0;
}
this.index = 0;
System.out.println("Starting...");
this.process(this.exp, false);
System.out.println("\nDone.");
}
private void process(String str, boolean loop){
boolean running = loop;
do{
for(int i = 0; i < str.length(); i++){
switch(str.charAt(i)){
case '>':increaseIndex();break;
case '<':decreaseIndex();break;
case '+':increaseValue();break;
case '-':decreaseValue();break;
case '[':
String s = str.substring(i);
int j = this.getClosingIndex(s);
if(this.values[this.index] == 0){
i +=j;
break;
}
process(s.substring(1, j), true);
i += j;
break;
case '.':
int v = this.values[this.index];
System.out.print((char)v);
break;
case ',':this.values[this.index] = this.in.next().charAt(0);break;
case '^':this.index = this.values[this.index];break;// Jumps to the index specified in the current cell.
case '=':this.values[index] = this.index;break;// Sets the value at cell #x to x
case '&':this.values[index] = this.values[this.values[index]];break;// If cell contains X, makes value of current cell equal to value in cell X
default:
//Ignore others
break;
}
}
if(this.values[this.index] == 0){
running = false;
}
}while(running);
}
private void increaseIndex(){
if(++this.index >= this.values.length){
this.index = 0;
}
}
private void decreaseIndex(){
if(--this.index < 0){
this.index = this.values.length - 1;
}
}
private void increaseValue(){
int newVal = this.values[this.index] + 1;
if(newVal >= this.values.length){
newVal = 0;
}
this.values[this.index] = newVal;
}
private void decreaseValue(){
int newVal = this.values[this.index] - 1;
if(newVal < 0){
newVal = this.values.length - 1;
}
this.values[this.index] = newVal;
}
private int getClosingIndex(String str){
int openings = 0;
int closings = 0;
for(int i = 0; i < str.length(); i++){
char c = str.charAt(i);
if(c == '['){
openings++;
}else if(c == ']'){
closings++;
}
if(openings == closings){
return i;
}
}
return -1;
}
}
Not even close to golfed but should provide a good starting point.
Lowest final score wins, where score is number of bytes in your program after the applicable Challenge reductions have been taken into account.
Testing
The following BrainFlow program should print the specified output after reading a '+' char from stdin:
<<,++++[>++++[>++++<-]<-] Set cell #0 to a value dependent on input
>>>+[[-]&>=]+& Set every other cell to that value
[ Start loop
+^ Add one to current value and jump to that cell index
. Print the value at that cell
& Copy value from specified cell
] End loop
Output:
ðñðòñðòðôóòñóñôóðòõóñõðôôóòñööõôöðóöðõðùõñô÷ùõóñöóùñô÷øôøõôòöõóðòöóñ÷ðõôûôòú÷úø÷öùøöùñøðùúðûðþöûñùýøðòñ
Note that the & allows you to essentially create variables in the lower cells then reference them later.
For example if I store my age in the 2nd cell and the month I was born in the 3rd cell, and I am currently in the 64th cell, I can do
++&
to retrieve my age or+++&
to retrieve the month I was born in. (Assuming of course the 64th cell is at the default value of 0) – spocot – 2014-08-28T19:52:55.0202I think you mean 'superset', not subset. – ɐɔıʇǝɥʇuʎs – 2014-08-28T19:54:58.487
@ɐɔıʇǝɥʇuʎs Changed from
subset
toextension
. Thanks for the feedback. – spocot – 2014-08-28T19:56:21.073score for being written in brainflow is a bad idea - brainfuck is a subset of brainflow, therefore any brainfuck program is a brainflow program. its like saying a c++ program will score better than a C program. OK, my C program is a C++ program, soo.... – pseudonym117 – 2014-08-28T20:22:11.547
@pseudonym117 I'll make it more specific, thanks – spocot – 2014-08-28T20:23:52.160
for challenge #4, does that mean just literals or in the source code at all? – pseudonym117 – 2014-08-28T20:36:34.040
@pseudonym117 None in the source code at all. – spocot – 2014-08-28T20:38:15.093
if anyone manages to do that i will be thoroughly impressed. = and , pretty important in every language... – pseudonym117 – 2014-08-28T20:43:22.377
@pseudonym117 That's why its a -50 to your score. – spocot – 2014-08-28T20:45:40.853
Does it have to be unbounded in both directions or is it okay to not allow negative cell indices? – Ingo Bürk – 2014-08-28T21:02:21.233
@IngoBürk It should work the same way BrainF**k does. Decrementing a cell or value when at 0 sets the value to 255. And incrementing when at 255 should set to 0. – spocot – 2014-08-28T21:03:44.737
Well there are different brainfuck implementations. But we're talking about a wrapping one on both ends here. Got it :) – Ingo Bürk – 2014-08-28T21:07:41.337
@IngoBürk Yah, it's the only implementation that makes sense to me, especially since output can only be 0-255. – spocot – 2014-08-28T21:11:03.407
True, nin-wrapping would cause problems in the new commands. Hadn't thought of that. Thanks! – Ingo Bürk – 2014-08-28T21:12:43.550
1Why is writing an implementation in Brainfuck have a smaller benefit than writing one in Brainflow? It seems like the former would be more challenging, since it is a smaller language. – Peter Olson – 2014-08-28T21:33:34.397
@PeterOlson Because I said so. JK, it's because I wanted to give a strong reason to experiment and use the new commands in your interpreter if writing in a Brain[Flow/F**k] language. – spocot – 2014-08-29T02:12:29.590
Any submission in BFk or BFw would automatically also get the third bonus. In that case, which bonus is applied first? – Martin Ender – 2014-08-31T00:15:41.467
@MartinBüttner Division bonuses will be applied after subtraction bonuses. – spocot – 2014-08-31T02:04:23.053
Can we assume a limited size for the array (e.g. the "standard" 30k cell array?). – Bakuriu – 2014-09-23T16:53:35.707
@Bakuriu There are 255 cells that can hold values up to 255 – spocot – 2014-09-25T03:09:08.453
@spocot According to your description there are at least 255 cells, but you could have an infinite number (although you can only jump to the first 255). Anyway your comment clarifies that you only require exactly 255 cells. – Bakuriu – 2014-09-25T06:49:36.067
@Bakuriu I can assure you... there are only, at max, 255 cells... The indexes and values wrap around. ex: 255 + 1 = 0 – spocot – 2014-09-26T21:31:07.263