package de.sebhn.algorithm.excercise2;

import java.math.BigInteger;

public class Quaternstrings {

  public static void main(String[] args) {
    Quaternstrings.calculate(3);
  }

  public static void calculate(int n) {
    int radix = 4;
    BigInteger maxNumber = new BigInteger("4").pow(n);
    BigInteger counter = BigInteger.ZERO;
    System.out.println("maxnbr " + maxNumber);

    for (BigInteger i = BigInteger.ZERO; i.compareTo(maxNumber) < 0; i = i.add(BigInteger.ONE)) {
      String numberAsRadix4 = i.toString(radix);
      numberAsRadix4 = addLeadingZeros(n, numberAsRadix4);

      if (canBePrinted(numberAsRadix4)) {
        counter = counter.add(BigInteger.ONE);
        // System.out.println(numberAsRadix4);
      }
    }

    System.out.println("Amount of possibleNumbers " + counter);
  }

  // TODO: make following code faster
  private static boolean canBePrinted(String numberAsRadix4) {
    boolean print = true;
    char[] numbers = numberAsRadix4.toCharArray();
    for (int j = 0; j < numbers.length; j++) {
      char currentChar = numbers[j];
      int nextIndex = j + 1;
      if (hasNextIndex(numbers, nextIndex)) {
        char nextChar = numbers[nextIndex];
        int currentCharAsNumber = currentChar;
        int nextCharAsNumber = nextChar;
        if (nextCharAsNumber == currentCharAsNumber + 1) {
          print = false;
        }
      }
    }
    return print;
  }

  private static String addLeadingZeros(int length, String numberAsRadix4) {
    while (numberAsRadix4.length() < length) {
      numberAsRadix4 = "0" + numberAsRadix4;
    }
    return numberAsRadix4;
  }

  private static boolean hasNextIndex(char[] numbers, int nextIndex) {
    return nextIndex < numbers.length;
  }
}

@@ -547,68 +547,8 @@
 x);%0D
-%0A      numberAsRadix4 = addLeadingZeros(n, numberAsRadix4);%0D
 %0A%0D%0A 
@@ -1353,223 +1353,36 @@
     
-%7D%0D%0A      %7D%0D%0A    %7D%0D%0A    return print;%0D%0A  %7D%0D%0A%0D%0A  private static String addLeadingZeros(int length, String numberAsRadix4) %7B%0D%0A    while (numberAsRadix4.length() %3C length) %7B%0D%0A      numberAsRadix4 = %220%22 + numberAsRadix4;
+  break;%0D%0A        %7D%0D%0A      %7D
 %0D%0A  
@@ -1397,30 +1397,21 @@
  return 
-numberAsRadix4
+print
 ;%0D%0A  %7D%0D%0A

add missing break, remove addleadingzeros (for more performance)