Advertisement
GenuineSounds

BrainFuckery

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