-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day2.java
47 lines (34 loc) · 1.27 KB
/
Day2.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
44
45
46
47
package aoc2018.day02;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Day2 {
static int computeCheckSum(String[] idList) {
int doubleCount = 0, tripleCount = 0;
for (String idString : idList) {
char[] idLetters = idString.toCharArray();
Arrays.sort(idLetters);
boolean doubleOnceCheck = false, tripleOnceCheck = false;
List<Character> onceCheck = new ArrayList<>();
for (char idLetter : idLetters) {
if (!onceCheck.contains(idLetter)) {
onceCheck.add(idLetter);
int count = StringUtils.countMatches(idString, idLetter);
if (!doubleOnceCheck && count == 2) {
doubleOnceCheck = true;
doubleCount += 1;
}
if (!tripleOnceCheck && count == 3) {
tripleOnceCheck = true;
tripleCount += 1;
}
if (doubleOnceCheck && tripleOnceCheck) {
break;
}
}
}
}
return doubleCount * tripleCount;
}
}