/* ===================== Time Trials ===================== Info: Running 10000 times x3. Info: These are the pre-compile results! Debug 1: 30787523 nanoseconds. Debug 2: 19068954 nanoseconds. Debug 3: 20151062 nanoseconds. Info: These are the post-compile results! Debug 1: 4624483 nanoseconds. Debug 2: 4946737 nanoseconds. Debug 3: 5837299 nanoseconds. Info: These are the post-compile results! Debug 1: 4603702 nanoseconds. Debug 2: 4856686 nanoseconds. Debug 3: 5056965 nanoseconds. ==================== Quality Check ==================== Debug: +++++++++[->+++++++++<]>+++.<++++[->++++<]>+.++++++++++++++.+.-----------.+++++.-------.<++++++++[-> Debug: 9+[->9+<]>3+.<4+[->4+<]>+.14+.+.11-.5+.7-.<8+[-> Debug: +++++++++[->+++++++++<]>+++.<++++[->++++<]>+.++++++++++++++.+.-----------.+++++.-------.<++++++++[-> Success: Test Passed */ package com.genuineminecraft.bf; public class BrainFuckery { public static String test = "+++++++++[->+++++++++<]>+++.<++++[->++++<]>+.++++++++++++++.+.-----------.+++++.-------.<++++++++[->"; public static void main(String[] args) { System.out.println("===================== Time Trials ====================="); timeTrials(); System.out.println("==================== Quality Check ===================="); qualityCheck(); } public static void qualityCheck() { String min = encode(test); String mag = decode(min); System.out.println("Debug: " + test); System.out.println("Debug: " + min); System.out.println("Debug: " + mag); if (!test.equals(mag)) System.out.println("Error: Test Failed"); else System.out.println("Success: Test Passed"); } public static void timeTrials() { long start, stop; int count = 10000; int dCount = 3; for (int j = 0; j < dCount; j++) { if (j == 0) { System.out.println("Info: Running " + count + " times x" + dCount + "."); System.out.println("Info: These are the pre-compile results!"); } else System.out.println("Info: These are the post-compile results!"); start = System.nanoTime(); for (int i = 0; i < count; i++) encode_1(test); stop = System.nanoTime(); System.out.println("Debug 1: " + (stop - start) + " nanoseconds."); start = System.nanoTime(); for (int i = 0; i < count; i++) encode_2(test); stop = System.nanoTime(); System.out.println("Debug 2: " + (stop - start) + " nanoseconds."); start = System.nanoTime(); for (int i = 0; i < count; i++) encode_3(test); stop = System.nanoTime(); System.out.println("Debug 3: " + (stop - start) + " nanoseconds."); } } public static String encode(String in) { return encode_1(in); } public static String decode(String in) { return decode_1(in); } private static String encode_1(String in) { StringBuilder out = new StringBuilder(in.length()); int counter = 0; int check = 1; char compare = in.charAt(0); for (int place = 0; place < in.length(); place++) { compare = in.charAt(place); check = place + 1; if (in.length() > check && compare == in.charAt(check)) { counter++; continue; } if (counter == 0) out.append(compare); else if (counter == 1) { out.append(compare); out.append(compare); } else { out.append(counter + 1); out.append(compare); } counter = 0; } return out.toString(); } private static String encode_2(String in) { StringBuilder out = new StringBuilder(in.length()); int counter = 0; int check = 1; char compare = in.charAt(0); for (int place = 0; place < in.length(); place++) { compare = in.charAt(place); check = place + 1; if (in.length() > check && compare == in.charAt(check)) { counter++; continue; } switch (counter) { case 0: out.append(compare); break; case 1: out.append(compare); out.append(compare); break; default: out.append(counter + 1); out.append(compare); break; } counter = 0; } return out.toString(); } private static String encode_3(String in) { StringBuilder out = new StringBuilder(); int counter = 0; int check = 1; char compare = in.charAt(0); for (int place = 0; place < in.length(); place++) { compare = in.charAt(place); check = place + 1; if (in.length() > check && compare == in.charAt(check)) { counter++; continue; } switch (counter) { case 0: out.append(compare); break; case 1: out.append(compare); out.append(compare); break; default: out.append(counter + 1); out.append(compare); break; } counter = 0; } return out.toString(); } private static String encode_4(String in) { String out = ""; int counter = 0; for (int place = 0; place < in.length(); place++) { char compare = in.charAt(place); if (in.length() > place + 1 && compare == in.charAt(place + 1)) { counter++; continue; } switch (counter) { case 0: out += Character.toString(compare); break; case 1: out += Character.toString(compare) + Character.toString(compare); break; default: out += Integer.toString(counter + 1) + Character.toString(compare); break; } counter = 0; } return out; } private static String encode_5(String in) { String out = ""; int counter = 0; for (int place = 0; place < in.length(); place++) { char compare = in.charAt(place); if (in.length() > place + 1 && compare == in.charAt(place + 1)) { counter++; continue; } out += (counter == 0 ? "" : counter == 1 ? Character.toString(compare) : Integer.toString(counter + 1)) + Character.toString(compare); counter = 0; } return out; } private static String encode_6(String in) { String out = ""; while (in.length() > 0) { int counter = 0; char compare = in.charAt(0); while (counter < in.length() && in.charAt(counter) == compare) counter++; in = in.substring(counter - (counter == 2 ? 1 : 0)); if (counter > 2) out += counter; out += compare; } return out; } private static String decode_1(String in) { String out = ""; for (int i = 0; i < in.length(); i++) { String t = ""; while (Character.isDigit(in.charAt(i))) t += in.charAt(i++); int n = 1; if (!t.isEmpty()) n = Integer.parseInt(t, 10); for (int j = 0; j < n; j++) out += in.charAt(i); } return out; } }