Guest User

Untitled

a guest
Dec 4th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. package adventofcode;
  2.  
  3. import adventofcode.utils.Utils;
  4.  
  5. public class Day4 {
  6.     public static void solve() {
  7.         String input = Utils.getInput(4).get(0);
  8.  
  9.         String[] parts = input.split("-");
  10.         int from = Integer.valueOf(parts[0]);
  11.         int to = Integer.valueOf(parts[1]);
  12.         int n1 = 0;
  13.         int n2 = 0;
  14.  
  15.         for(int i = from ; i <= to; i++) {
  16.             char[] chars = String.valueOf(i).toCharArray();
  17.             boolean twoSame = false;
  18.             boolean onlyTwoSame = false;
  19.             boolean decreases = false;
  20.  
  21.             for(int c = 0 ; c < chars.length ; c++) {
  22.                 if(getChar(chars, c) == getChar(chars, c + 1)) {
  23.                     twoSame = true;
  24.                     if(getChar(chars, c) != getChar(chars, c - 1) && getChar(chars, c) != getChar(chars, c + 2)) {
  25.                         onlyTwoSame = true;
  26.                     }
  27.                 }
  28.                 if(getChar(chars, c) > getChar(chars, c + 1)) {
  29.                     decreases = true;
  30.                     break;
  31.                 }
  32.             }
  33.  
  34.             if(decreases) {
  35.                 continue;
  36.             }
  37.  
  38.             if(twoSame) {
  39.                 n1++;
  40.             }
  41.             if(onlyTwoSame) {
  42.                 n2++;
  43.             }
  44.         }
  45.  
  46.         System.out.println(n1);
  47.         System.out.println(n2);
  48.     }
  49.  
  50.     private static int getChar(char[] chars, int c) {
  51.         if(c < 0 || c >= chars.length) {
  52.             return 255;
  53.         }
  54.  
  55.         return chars[c];
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment