Guest User

Untitled

a guest
Dec 4th, 2019
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. package Advent2019;
  2.  
  3. import util.AdventOfCode;
  4.  
  5. import java.util.List;
  6.  
  7. public class Day4 extends AdventOfCode {
  8.  
  9.     int lower = 271973;
  10.     int upper = 785961;
  11.  
  12.     public Day4(List<String> input) {
  13.         super(input);
  14.         title = "Secure Container";
  15.         part1Description = "Valid passwords: ";
  16.         part2Description = "Valid passwords 2: ";
  17.     }
  18.  
  19.     boolean testNum(int n) {
  20.         boolean adj = false;
  21.         int last = 10;
  22.         while (n > 0) {
  23.             int digit = n % 10;
  24.             if (digit == last) adj = true;
  25.             if (digit > last) return false;
  26.             n /= 10;
  27.             last = digit;
  28.         }
  29.         return adj;
  30.     }
  31.  
  32.     boolean testNum2(int n) {
  33.         int[] countAdj = new int[10];
  34.         int last = 10;
  35.         while (n > 0) {
  36.             int digit = n % 10;
  37.             if (digit == last) countAdj[digit]++;
  38.             n /= 10;
  39.             last = digit;
  40.         }
  41.         for (int each : countAdj) {
  42.             if (each == 1) return true;
  43.         }
  44.         return false;
  45.     }
  46.  
  47.     @Override
  48.     public Object part1() {
  49.         int sum = 0;
  50.         for (int i = lower; i < upper; i++) {
  51.             if (testNum(i)) sum++;
  52.         }
  53.         return sum;
  54.     }
  55.  
  56.     @Override
  57.     public Object part2() {
  58.         int sum = 0;
  59.         for (int i = lower; i < upper; i++) {
  60.             if (testNum(i) && testNum2(i)) sum++;
  61.         }
  62.         return sum;
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment