@@ -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