-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c25d02
commit 02a89e9
Showing
2 changed files
with
1,106 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
src/main/java/com/belellou/kevin/advent/year2015/Day5.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.belellou.kevin.advent.year2015; | ||
|
||
import java.io.BufferedReader; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import com.belellou.kevin.advent.generic.AbstractDaySolver; | ||
import com.belellou.kevin.advent.generic.Day; | ||
import com.belellou.kevin.advent.generic.Year; | ||
|
||
@SuppressWarnings("unused") | ||
public class Day5 extends AbstractDaySolver { | ||
|
||
public Day5() { | ||
super(Year.YEAR_2015, Day.DAY_5); | ||
} | ||
|
||
private static boolean firstStarStringTest(String string) { | ||
int vowelCount = 0; | ||
char lastChar = '0'; | ||
boolean foundDouble = false; | ||
|
||
Set<Character> vowels = Set.of('a', 'e', 'i', 'o', 'u'); | ||
Set<String> forbidden = Set.of("ab", "cd", "pq", "xy"); | ||
|
||
for (int i = 0; i < string.length(); i++) { | ||
char c = string.charAt(i); | ||
|
||
if (forbidden.contains(lastChar + String.valueOf(c))) { | ||
return false; | ||
} | ||
|
||
if (vowels.contains(c)) { | ||
vowelCount++; | ||
} | ||
|
||
if (lastChar == c) { | ||
foundDouble = true; | ||
} | ||
|
||
lastChar = c; | ||
} | ||
|
||
return foundDouble && vowelCount >= 3; | ||
} | ||
|
||
private static boolean secondStarStringTest(String string) { | ||
char lastChar = '0'; | ||
char lastLastChar = '0'; | ||
|
||
Set<String> pairs = new HashSet<>(); | ||
boolean foundPair = false; | ||
boolean lastPairWasDouble = false; | ||
|
||
boolean foundTrio = false; | ||
|
||
for (int i = 0; i < string.length(); i++) { | ||
char c = string.charAt(i); | ||
|
||
String pair = lastChar + String.valueOf(c); | ||
|
||
if (pairs.contains(pair) && !(lastPairWasDouble && lastChar == c)) { | ||
foundPair = true; | ||
} | ||
|
||
if (lastLastChar == c) { | ||
foundTrio = true; | ||
} | ||
|
||
if (foundPair && foundTrio) { | ||
return true; | ||
} | ||
|
||
if (i >= 1) { | ||
pairs.add(pair); | ||
} | ||
|
||
lastPairWasDouble = !lastPairWasDouble && lastChar == c; | ||
|
||
lastLastChar = lastChar; | ||
lastChar = c; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
protected int doSolveFirstStar(BufferedReader reader) { | ||
return reader.lines().filter(Day5::firstStarStringTest).toList().size(); | ||
} | ||
|
||
@Override | ||
public int getFirstStarSolution() { | ||
return 255; | ||
} | ||
|
||
@Override | ||
protected int doSolveSecondStar(BufferedReader reader) { | ||
return reader.lines().filter(Day5::secondStarStringTest).toList().size(); | ||
} | ||
|
||
@Override | ||
public int getSecondStarSolution() { | ||
return 55; | ||
} | ||
} |
Oops, something went wrong.