Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package adventofcode;
- import adventofcode.utils.Utils;
- public class Day4 {
- public static void solve() {
- String input = Utils.getInput(4).get(0);
- String[] parts = input.split("-");
- int from = Integer.valueOf(parts[0]);
- int to = Integer.valueOf(parts[1]);
- int n1 = 0;
- int n2 = 0;
- for(int i = from ; i <= to; i++) {
- char[] chars = String.valueOf(i).toCharArray();
- boolean twoSame = false;
- boolean onlyTwoSame = false;
- boolean decreases = false;
- for(int c = 0 ; c < chars.length ; c++) {
- if(getChar(chars, c) == getChar(chars, c + 1)) {
- twoSame = true;
- if(getChar(chars, c) != getChar(chars, c - 1) && getChar(chars, c) != getChar(chars, c + 2)) {
- onlyTwoSame = true;
- }
- }
- if(getChar(chars, c) > getChar(chars, c + 1)) {
- decreases = true;
- break;
- }
- }
- if(decreases) {
- continue;
- }
- if(twoSame) {
- n1++;
- }
- if(onlyTwoSame) {
- n2++;
- }
- }
- System.out.println(n1);
- System.out.println(n2);
- }
- private static int getChar(char[] chars, int c) {
- if(c < 0 || c >= chars.length) {
- return 255;
- }
- return chars[c];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment