Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Advent4_1 {
- int cached = -1;
- public static void main(String[] args) {
- int possiblePassCount = 0;
- System.out.println(hasAdjacentNums(233222));
- for (int i = 128888; i < 640000; i++) {
- if (hasAdjacentNums(i) && increasing(i))
- possiblePassCount++;
- }
- System.out.println("Answer: " + possiblePassCount);
- }
- private static boolean increasing(int i) {
- String s = Integer.toString(i);
- int[] digits = new int[6];
- for (int j = 0; j < 6; j++) {
- digits[j] = s.charAt(j) - '0';
- }
- if (digits[0] <= digits[1] && digits[1] <= digits[2] && digits[2] <= digits[3] && digits[3] <= digits[4]
- && digits[4] <= digits[5])
- return true;
- return false;
- }
- private static boolean hasAdjacentNums(int i) {
- String s = Integer.toString(i);
- int[] digits = new int[6];
- for (int j = 0; j < 6; j++) {
- digits[j] = s.charAt(j) - '0';
- }
- int[][] test = removeLargeGroups(digits);
- return checkForDoubles(test[0]) && checkForDoubles(test[1]);
- }
- private static boolean checkForDoubles(int[] digits) {
- if(digits.length < 2)
- return false;
- boolean ret = false;
- for(int i = 0; i < digits.length-1; i ++) {
- ret = digits[i] == digits[i+1] || ret;
- }
- return ret;
- }
- private static int[][] removeLargeGroups(int[] digits) {
- if(isTwoTriples(digits))
- return new int[][] {new int[] {}, new int[] {}};
- int cache = digits[0];
- int startLoc = 0;
- int endLoc = 0;
- int count = 0;
- boolean found = false;
- for(int i = 1; i < digits.length; i ++) {
- if(digits[i] == cache) {
- count +=1;
- if(i-2 >= 0 && digits[i-2] != digits[i]) {
- count += 1;
- }
- } else {
- if(!found) {
- endLoc = startLoc = i;
- }
- count = 0;
- cache = digits[i];
- continue;
- }
- if(count == 3) {
- found = true;
- endLoc = startLoc + 3;
- } else if(count > 3) {
- endLoc ++;
- }
- }
- if(endLoc - startLoc == 0) {
- return new int[][] {(int[]) digits, digits};
- }
- int[][] ret = new int[][] {new int[startLoc], new int[digits.length-endLoc]};
- int j = 0;
- for(int i = 0; i < digits.length; i ++) {
- if(i < startLoc) {
- ret[0][j] = digits[i];
- j ++;
- }
- }
- for(int i = 0; i < digits.length; i ++) {
- if(i > endLoc) {
- ret[1][j] = digits[i];
- j ++;
- }
- }
- return ret;
- }
- public static boolean isTwoTriples(int[] ints) {
- if(ints[0] == ints[1] && ints[0] == ints[2] && ints[3] == ints[4] && ints[3] == ints[5]) {
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement