Advertisement
GenuineSounds

BrainFuckery

Dec 7th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.98 KB | None | 0 0
  1. package com.genuineminecraft.bf;
  2.  
  3. public class BrainFuckeryTest {
  4.  
  5.     public static String test = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
  6.  
  7.     public static void main(String[] args) {
  8.         if (args.length > 0)
  9.             test = args[0];
  10.         test = test.replaceAll("[^\\,\\.\\<\\>\\[\\]\\-\\+]", "");
  11.         System.out.println("===================== Time Trials =====================");
  12.         timeTrials();
  13.         System.out.println("==================== Quality Check ====================");
  14.         qualityCheck();
  15.     }
  16.  
  17.     public static void qualityCheck() {
  18.         String min = encode(test);
  19.         String mag = decode(min);
  20.         System.out.println("Debug: " + test);
  21.         System.out.println("Debug: " + min);
  22.         System.out.println("Debug: " + mag);
  23.         if (!test.equals(mag))
  24.             System.out.println("Error: Test Failed");
  25.         else
  26.             System.out.println("Success: Test Passed");
  27.     }
  28.  
  29.     public static void timeTrials() {
  30.         long start, stop;
  31.         int count = 10000;
  32.         int dCount = 50;
  33.         for (int j = 0; j < dCount; j++) {
  34.             if (j == 0) {
  35.                 System.out.println("Info: Running " + count + " times x" + dCount + ".");
  36.                 System.out.println("Info: These are the pre-compile results!");
  37.             } else
  38.                 System.out.println("Info: These are the post-compile results!");
  39.             start = System.nanoTime();
  40.             for (int i = 0; i < count; i++)
  41.                 encode_1(test);
  42.             stop = System.nanoTime();
  43.             System.out.println("Debug Encode 1: " + (stop - start) + " nanoseconds.");
  44.             start = System.nanoTime();
  45.             for (int i = 0; i < count; i++)
  46.                 encode_2(test);
  47.             stop = System.nanoTime();
  48.             System.out.println("Debug Encode 2: " + (stop - start) + " nanoseconds.");
  49.             start = System.nanoTime();
  50.             for (int i = 0; i < count; i++)
  51.                 encode_3(test);
  52.             stop = System.nanoTime();
  53.             System.out.println("Debug Encode 3: " + (stop - start) + " nanoseconds.");
  54.             start = System.nanoTime();
  55.             for (int i = 0; i < count; i++)
  56.                 decode_1(test);
  57.             stop = System.nanoTime();
  58.             System.out.println("Debug Decode 1: " + (stop - start) + " nanoseconds.");
  59.             start = System.nanoTime();
  60.             for (int i = 0; i < count; i++)
  61.                 decode_2(test);
  62.             stop = System.nanoTime();
  63.             System.out.println("Debug Decode 2: " + (stop - start) + " nanoseconds.");
  64.         }
  65.     }
  66.  
  67.     public static String encode(String in) {
  68.         return encode_1(in);
  69.     }
  70.  
  71.     public static String decode(String in) {
  72.         return decode_1(in);
  73.     }
  74.  
  75.     private static String encode_1(String in) {
  76.         int length = in.length();
  77.         if (length == 0)
  78.             return in;
  79.         StringBuilder out = new StringBuilder(length);
  80.         int counter = 0;
  81.         int check = 1;
  82.         char compare = in.charAt(0);
  83.         for (int place = 0; place < length; place++) {
  84.             compare = in.charAt(place);
  85.             check = place + 1;
  86.             if (length > check && compare == in.charAt(check)) {
  87.                 counter++;
  88.                 continue;
  89.             }
  90.             if (counter == 0)
  91.                 out.append(compare);
  92.             else if (counter == 1) {
  93.                 out.append(compare);
  94.                 out.append(compare);
  95.             } else {
  96.                 out.append(counter + 1);
  97.                 out.append(compare);
  98.             }
  99.             counter = 0;
  100.         }
  101.         return out.toString();
  102.     }
  103.  
  104.     private static String encode_2(String in) {
  105.         int length = in.length();
  106.         if (length == 0)
  107.             return in;
  108.         StringBuilder out = new StringBuilder(length);
  109.         int counter = 0;
  110.         int check = 1;
  111.         char compare = in.charAt(0);
  112.         for (int place = 0; place < length; place++) {
  113.             compare = in.charAt(place);
  114.             check = place + 1;
  115.             if (length > check && compare == in.charAt(check)) {
  116.                 counter++;
  117.                 continue;
  118.             }
  119.             switch (counter) {
  120.                 case 0:
  121.                     out.append(compare);
  122.                     break;
  123.                 case 1:
  124.                     out.append(compare);
  125.                     out.append(compare);
  126.                     break;
  127.                 default:
  128.                     out.append(counter + 1);
  129.                     out.append(compare);
  130.                     break;
  131.             }
  132.             counter = 0;
  133.         }
  134.         return out.toString();
  135.     }
  136.  
  137.     private static String encode_3(String in) {
  138.         int length = in.length();
  139.         if (length == 0)
  140.             return in;
  141.         StringBuilder out = new StringBuilder();
  142.         int counter = 0;
  143.         int check = 1;
  144.         char compare = in.charAt(0);
  145.         for (int place = 0; place < length; place++) {
  146.             compare = in.charAt(place);
  147.             check = place + 1;
  148.             if (length > check && compare == in.charAt(check)) {
  149.                 counter++;
  150.                 continue;
  151.             }
  152.             switch (counter) {
  153.                 case 0:
  154.                     out.append(compare);
  155.                     break;
  156.                 case 1:
  157.                     out.append(compare);
  158.                     out.append(compare);
  159.                     break;
  160.                 default:
  161.                     out.append(counter + 1);
  162.                     out.append(compare);
  163.                     break;
  164.             }
  165.             counter = 0;
  166.         }
  167.         return out.toString();
  168.     }
  169.  
  170.     private static String encode_4(String in) {
  171.         String out = "";
  172.         int counter = 0;
  173.         for (int place = 0; place < in.length(); place++) {
  174.             char compare = in.charAt(place);
  175.             if (in.length() > place + 1 && compare == in.charAt(place + 1)) {
  176.                 counter++;
  177.                 continue;
  178.             }
  179.             switch (counter) {
  180.                 case 0:
  181.                     out += Character.toString(compare);
  182.                     break;
  183.                 case 1:
  184.                     out += Character.toString(compare) + Character.toString(compare);
  185.                     break;
  186.                 default:
  187.                     out += Integer.toString(counter + 1) + Character.toString(compare);
  188.                     break;
  189.             }
  190.             counter = 0;
  191.         }
  192.         return out;
  193.     }
  194.  
  195.     private static String encode_5(String in) {
  196.         String out = "";
  197.         int counter = 0;
  198.         for (int place = 0; place < in.length(); place++) {
  199.             char compare = in.charAt(place);
  200.             if (in.length() > place + 1 && compare == in.charAt(place + 1)) {
  201.                 counter++;
  202.                 continue;
  203.             }
  204.             out += (counter == 0 ? "" : counter == 1 ? Character.toString(compare) : Integer.toString(counter + 1)) + Character.toString(compare);
  205.             counter = 0;
  206.         }
  207.         return out;
  208.     }
  209.  
  210.     private static String encode_6(String in) {
  211.         String out = "";
  212.         while (in.length() > 0) {
  213.             int counter = 0;
  214.             char compare = in.charAt(0);
  215.             while (counter < in.length() && in.charAt(counter) == compare)
  216.                 counter++;
  217.             in = in.substring(counter - (counter == 2 ? 1 : 0));
  218.             if (counter > 2)
  219.                 out += counter;
  220.             out += compare;
  221.         }
  222.         return out;
  223.     }
  224.  
  225.     private static String decode_1(String in) {
  226.         int length = in.length();
  227.         if (length == 0)
  228.             return in;
  229.         StringBuilder out = new StringBuilder(length * 4);
  230.         for (int count = 0; count < length; count++) {
  231.             if (Character.isDigit(in.charAt(count))) {
  232.                 int start = count;
  233.                 while (Character.isDigit(in.charAt(count)))
  234.                     count++;
  235.                 for (int j = 0; j < Integer.parseInt(in.substring(start, count), 10); j++)
  236.                     out.append(in.charAt(count));
  237.             } else
  238.                 out.append(in.charAt(count));
  239.         }
  240.         return out.toString();
  241.     }
  242.  
  243.     private static String decode_2(String in) {
  244.         int length = in.length();
  245.         if (length == 0)
  246.             return in;
  247.         StringBuilder out = new StringBuilder();
  248.         for (int count = 0; count < in.length(); count++) {
  249.             if (Character.isDigit(in.charAt(count))) {
  250.                 int start = count;
  251.                 while (Character.isDigit(in.charAt(count)))
  252.                     count++;
  253.                 for (int j = 0; j < Integer.parseInt(in.substring(start, count), 10); j++)
  254.                     out.append(in.charAt(count));
  255.             } else
  256.                 out.append(in.charAt(count));
  257.         }
  258.         return out.toString();
  259.     }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement