Guest User

Untitled

a guest
Mar 16th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class alchemization {
  4.     private final static char[] CHARS = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','l','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z','?','!'};
  5.  
  6.     /**
  7.      * Punch card alchemy go go go!
  8.      * @param code1 8 character string using only letters of either case, numbers, and ! or ?
  9.      * @param code2 8 character string using only letters of either case, numbers, and ! or ?
  10.      * @param mode use 1 to && the codes and 2 to || the codes
  11.      * @return the final captcha string
  12.      */
  13.     public static String calculateCaptcha(String code1, String code2, int mode){
  14.         String toreturn = "";
  15.         int[] c0de1 = new int[8];
  16.         for(int i = 0; i < 8; i++){
  17.             int a = 0;
  18.             for(int j = 0; j < 64; j++){
  19.                 if(code1.charAt(i) == CHARS[j])
  20.                     a = j;
  21.             }
  22.             c0de1[i] = a;
  23.         }
  24.         int[] c0de2 = new int[8];
  25.         for(int i = 0; i < 8; i++){
  26.             int a = 0;
  27.             for(int j = 0; j < 64; j++){
  28.                 if(code2.charAt(i) == CHARS[j])
  29.                     a = j;
  30.             }
  31.             c0de2[i] = a;
  32.         }
  33.         int[] finalchode = new int[8];
  34.         if(mode == 1)
  35.             for(int i = 0; i < 8; i++)
  36.                 finalchode[i] = c0de1[i] & c0de2[i];
  37.         else
  38.             for(int i = 0; i < 8; i++)
  39.                 finalchode[2] = c0de1[i] | c0de2[i];
  40.         for(int i = 0; i < 8; i++){
  41.             toreturn += CHARS[finalchode[i]];
  42.         }
  43.         return toreturn;
  44.     }
  45.  
  46.     /**
  47.      * Punch card designix go go go!
  48.      * @param code1 8 character string using only letters of either case, numbers, and ! or ?
  49.      * @return a boolean[][], with true being punched.
  50.      */
  51.     public static boolean[][] punchPattern(String code){
  52.         while(true){
  53.             if(code.equals("#Invalid#"))
  54.                     break;
  55.             String tocard = "";
  56.             int[] c0de = new int[8];
  57.             for(int i = 0; i < 8; i++){
  58.                 int a = 0;
  59.                 for(int j = 0; j < 64; j++){
  60.                     if(code.charAt(i) == CHARS[j])
  61.                         a = j;
  62.                 }
  63.                 c0de[i] = a;
  64.             }
  65.             for(int x:c0de){
  66.                 String toadd = Integer.toBinaryString(x);
  67.                 while(toadd.length() < 6){
  68.                     toadd = 0 + toadd;
  69.                 }
  70.                 tocard += toadd;
  71.             }
  72.             boolean[][] toreturn = new boolean[4][12];
  73.             int cardplacer = 0;
  74.             for(int y = 0; y < 4; y++){
  75.                 for(int x = 0; x < 12; x++){
  76.                     if(Integer.parseInt(tocard.charAt(cardplacer)+"") == 1)
  77.                         toreturn[y][x] = true;
  78.                     else
  79.                         toreturn[y][x] = false;
  80.                     cardplacer++;
  81.                 }
  82.             }
  83.             return toreturn;
  84.         }
  85.         return new boolean[4][12];
  86.     }
  87.  
  88.     /**
  89.      * Inverse punch card designix go go go!
  90.      * @param card a boolean[4][12], such as comes from punchPattern(), to find the code of
  91.      * @return the8 character code that matches the card pattern
  92.      */
  93.     public static String deriveCode(boolean[][] card){
  94.         while(true){
  95.             if(card.length != 4 && card[0].length != 12)
  96.                 break;
  97.             String toreturn = "";
  98.             String[] binarycode = new String[8];
  99.             int bcplacer = 0;
  100.             int bcaplacer = 0;
  101.             for(int i = 0; i < 8; i++){
  102.                 binarycode[i] = "";
  103.             }
  104.             for(int x = 0; x < 4; x++){
  105.                 for(int y = 0; y < 12; y++){
  106.                     if(card[x][y])
  107.                         binarycode[bcaplacer] += 1;
  108.                     else
  109.                         binarycode[bcaplacer] += 0;
  110.                     bcplacer++;
  111.                     if(bcplacer == 6){
  112.                         bcaplacer++;
  113.                         bcplacer = 0;
  114.                     }
  115.                 }
  116.             }
  117.             for(int i = 0; i < 8; i++){
  118.                 toreturn += CHARS[Integer.parseInt(binarycode[i], 2)];
  119.             }
  120.             return toreturn;
  121.         }
  122.         return "#Invalid#";
  123.     }
  124.  
  125.     /**
  126.      * For testing purposes only! Don't even bother!
  127.      * @param card the array you get from punchPattern
  128.      * @return String that, when printed to the console, looks like the final card pattern
  129.      */
  130.     public static String prettyCard(boolean[][] card){
  131.         String toreturn  = "";
  132.         for(int y = 0; y < 12; y++){
  133.             for(int x = 0; x < 4; x++){
  134.                 if(card[x][y])
  135.                     toreturn += "[ ]";
  136.                 else
  137.                     toreturn += "   ";
  138.             }
  139.             toreturn += "\n";
  140.         }
  141.         return toreturn;
  142.     }
  143. }
Add Comment
Please, Sign In to add comment