-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay4.java
43 lines (36 loc) · 1.47 KB
/
Day4.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package aoc2019.day04;
import java.util.Arrays;
import java.util.stream.IntStream;
public class Day4 {
static int findPasswordCombinations(int min, int max) {
return (int) IntStream.rangeClosed(min, max).filter(i -> isValidPassword(i + "")).count();
}
static int findPasswordCombinationsPart2(int min, int max) {
return (int) IntStream.rangeClosed(min, max).filter(i -> isValidPasswordPart2(i + "")).count();
}
// Valid if there's at least one couple of two or more identical numbers
static boolean isValidPassword(String input) {
char[] chars = input.toCharArray();
boolean theresACouple = false;
for (int i = 1; i < chars.length; i++) {
if (chars[i] < chars[i - 1]) {
return false;
}
if (chars[i] == chars[i - 1]) {
theresACouple = true;
}
}
return theresACouple;
}
// Valid if there's at least one couple of exactly two identical numbers (e.g. 112222 will succeed, 111222 will fail)
static boolean isValidPasswordPart2(String input) {
char[] chars = input.toCharArray();
for (int i = 1; i < chars.length; i++) {
if (chars[i] < chars[i - 1]) {
return false;
}
}
// Split input in groups of identical characters (e.g. "122444" -> [1, 22, 444])
return Arrays.stream(input.split("(?<=(.))(?!\\1)")).anyMatch(groups -> groups.length() == 2);
}
}