Skip to content

Commit add4e92

Browse files
committed
Solved day 5 part 2
1 parent e94a919 commit add4e92

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2015'
7-
version '0.4.4'
7+
version '0.4.5'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {

day05/src/main/java/de/havox_design/aoc2015/day05/NiceStrings.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public int solvePart1() {
2929
if (
3030
countVowels(string) >= 3 &&
3131
containsDoubleLetter(string) &&
32-
!containsForbiddenStrings(string)
32+
!containsAnyString(string)
3333
) {
3434
numberNiceStrings++;
3535
}
@@ -39,7 +39,18 @@ public int solvePart1() {
3939
}
4040

4141
public int solvePart2() {
42-
return 0;
42+
int numberNiceStrings = 0;
43+
44+
for (String string : input) {
45+
if (
46+
containsTwoLetterStringAtLeastTwice(string) &&
47+
containsDoubleLetterWithOneLetterInBetween(string)
48+
) {
49+
numberNiceStrings++;
50+
}
51+
}
52+
53+
return numberNiceStrings;
4354
}
4455

4556
private int countVowels(String string) {
@@ -62,7 +73,31 @@ private boolean containsDoubleLetter(String string) {
6273
return false;
6374
}
6475

65-
private boolean containsForbiddenStrings(String string) {
76+
private boolean containsTwoLetterStringAtLeastTwice(String string) {
77+
for (int i = 0; i < string.length() - 1; i++) {
78+
String substring = string.substring(i, i + 2);
79+
80+
if (StringUtils.countMatches(string, substring) >= 2 &&
81+
string.lastIndexOf(substring) - string.indexOf(substring) >= 2
82+
) {
83+
return true;
84+
}
85+
}
86+
87+
return false;
88+
}
89+
90+
private boolean containsDoubleLetterWithOneLetterInBetween(String string) {
91+
for (int i = 0; i < string.length() - 2; i++) {
92+
if (string.charAt(i) == string.charAt(i + 2)) {
93+
return true;
94+
}
95+
}
96+
97+
return false;
98+
}
99+
100+
private boolean containsAnyString(String string) {
66101
return StringUtils.containsAny(string, "ab", "cd", "pq", "xy");
67102
}
68103

0 commit comments

Comments
 (0)