Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Advent2019;
- import util.AdventOfCode;
- import java.util.List;
- public class Day4 extends AdventOfCode {
- int lower = 271973;
- int upper = 785961;
- public Day4(List<String> input) {
- super(input);
- title = "Secure Container";
- part1Description = "Valid passwords: ";
- part2Description = "Valid passwords 2: ";
- }
- boolean testNum(int n) {
- boolean adj = false;
- int last = 10;
- while (n > 0) {
- int digit = n % 10;
- if (digit == last) adj = true;
- if (digit > last) return false;
- n /= 10;
- last = digit;
- }
- return adj;
- }
- boolean testNum2(int n) {
- int[] countAdj = new int[10];
- int last = 10;
- while (n > 0) {
- int digit = n % 10;
- if (digit == last) countAdj[digit]++;
- n /= 10;
- last = digit;
- }
- for (int each : countAdj) {
- if (each == 1) return true;
- }
- return false;
- }
- @Override
- public Object part1() {
- int sum = 0;
- for (int i = lower; i < upper; i++) {
- if (testNum(i)) sum++;
- }
- return sum;
- }
- @Override
- public Object part2() {
- int sum = 0;
- for (int i = lower; i < upper; i++) {
- if (testNum(i) && testNum2(i)) sum++;
- }
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment