HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Brainfuck Brute Force

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
brainfuckforcebrute

Problem

I am trying to brute force code Brainfuck code to get the desired output. For this, I need my code to be as fast as possible. I am fairly new to Java and making my code fast, in general, so don't assume that I have done something for a good reason.

```
import java.util.regex.Pattern;
public class BruteForceBF {
public static class Patterns {
final static Pattern first = Pattern.compile("\\[[^+-]*\\]");
final static Pattern second = Pattern.compile("\\[[+-]\\]");
final static Pattern third = Pattern.compile("\\[[+-]*\\]");
}
public static String interpret(String code) {
final int LENGTH = 255;
final int MAXNUM = 255;
final int MAXLOOP = countOccurrences(code, '[') == 1 ? 256 : 65536;
int[] mem = new int[LENGTH];
int dataPointer= 0;
int l = 0;
int loops = 0;
String output = "";
for(int i = 0; i ') {
dataPointer = (dataPointer == LENGTH-1) ? 0 : dataPointer + 1;
} else if(code.charAt(i) == ' 0 || code.charAt(i) != ']') && i 0 || code.charAt(i) != '[') {
if(code.charAt(i) == ']') l++;
if(code.charAt(i) == '[') l--;
i--;
}
i--;
}
}
}
return output;
}
public static int countOccurrences(String haystack, char needle) {
int count = 0;
for (int i=0; i ") ||
Patterns.first.matcher(code).find() ||
(!Patterns.second.matcher(code).find() &&
Patterns.third.matcher(code).find());
}
public static String BruteForce(String text, int time, int pos) {
System.out.println("Starting to Brute Force " + text);
long start = System.currentTimeMillis();
int i = pos - 1;
String finalCode = "";
while (System.currentTimeMillis() - start ");
code = code.replace("5", " text.

Solution

I am trying to brute force code Brainfuck code to get the desired output.

For this, I need my code to be as fast as possible.

I am sorry, but those two statements are impossible to combine. If you want a fast approach, don't purely brute-force. The shortest program known at the moment to produce "Hello, World" in Brainfuck is 78 bytes, and something that your brute force approach would not be able to produce in a realistic period of time. Instead I would suggest making a mathematical approach to the problem.

As for your code itself, some general suggestions:

  • first, second and third are lousy names. Describe what each pattern is used for instead.



  • You don't need the Patterns class to have your Pattern constants, they can be stored in your outer class.



  • Avoid making everything static. Flexible programs comes from having objects that you manipulate and pass around.



  • Use System.nanoTime to check how much time has passed. System.currentTimeMillis() is dependent on your computer calendar, if you modify your system time during the process you will confuse your program.



  • Avoid those code = code.replace("0", "+"); statements. Searching and replacing in strings is not very fast (although considering the algorithm, you will not notice much difference). As you are the one generating your code in the first place, put a + there instead of a 0.

Context

StackExchange Code Review Q#152149, answer score: 5

Revisions (0)

No revisions yet.