package hashfinder;

/** @author Daniel "3ICE" Berezvai */
public class HashFinder {
  static final String[] codes = {
    "A32BO2", "BPWDH8", "IYIH94", "AMSAKO", "N7OKDZ", "DJQ16O", "WWVK21",
    "JKR7ZR", "DBIOC9", "QG2CBR", "HM02MI", "B21KES", "F3D3D4", "CF2RHU",
    "Q0WQSH", "EYBSWV", "K7FONH", "JP7A43", "ILMJRU"
  };
  static String[] sorted;
  static int solutionCount = 0;
  static long iterationCount = 0;
  static int codeCount = codes.length;
  static int upper = 99;
  static boolean noCollision;
  static int s;
  static int a, b, c, d, e, f;

  public static void main(String[] args) {
    for (int A = 0; solutionCount < 3 && A < upper; A++) {
      for (int B = 0; solutionCount < 3 && B < upper; B++) {
        for (int C = 0; solutionCount < 3 && C < upper; C++) {
          for (int D = 0; solutionCount < 3 && D < upper; D++) {
            for (int E = 0; solutionCount < 3 && E < upper; E++) {
              for (int F = 0; solutionCount < 3 && F < upper; F++) {
                iterationCount++;
                if (iterationCount % 10000000 == 0) {
                  System.out.println("Tested " + iterationCount
                                     + " combinations.");
                }

                noCollision = true;
                sorted = new String[codeCount];

                for (int i = 0; i < codeCount; i++) {
                  a = (int) codes[i].charAt(0);
                  b = (int) codes[i].charAt(1);
                  c = (int) codes[i].charAt(2);
                  d = (int) codes[i].charAt(3);
                  e = (int) codes[i].charAt(4);
                  f = (int) codes[i].charAt(5);

                  s = (a * A + b * B + c * C + d * D + e * E + f * F)
                      % codeCount;
                  if (sorted[s] == null) {
                    sorted[s] = codes[i];
                  } else {
                    noCollision = false;
                  }
                }

                if (noCollision) {
                  solutionCount++;
                  System.out.println("Solution: (" + A + "a+" + B + "b+" + C
                                     + "c+" + D + "d+" + E + "e+" + F
                                     + "f) (mod " + codeCount + ")");
                  for (int i = 0; i < codeCount; i++) {
                    if (sorted[i] != null) {
                      System.out.print(sorted[i] + " → " + i + "; ");
                    } else {
                      System.out.print(i + " is unused; ");//3ICE: Dead code
                    }
                  }
                  System.out.println();
                }
              }
            }
          }
        }
      }
    }
    System.out.println("Done. Found " + solutionCount + " solutions after "
                       + iterationCount + " iterations.");
  }
}
