-
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
599faa0
commit c06bd13
Showing
16 changed files
with
182 additions
and
72 deletions.
There are no files selected for viewing
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
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
10 changes: 5 additions & 5 deletions
10
src/main/java/com/belellou/kevin/advent/generic/DaySolver.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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
package com.belellou.kevin.advent.generic; | ||
|
||
public interface DaySolver { | ||
public interface DaySolver<T> { | ||
|
||
int solveFirstStar(); | ||
T solveFirstStar(); | ||
|
||
int getFirstStarSolution(); | ||
T getFirstStarSolution(); | ||
|
||
int solveSecondStar(); | ||
T solveSecondStar(); | ||
|
||
int getSecondStarSolution(); | ||
T getSecondStarSolution(); | ||
} |
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
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
109 changes: 109 additions & 0 deletions
109
src/main/java/com/belellou/kevin/advent/year2015/Day11.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,109 @@ | ||
package com.belellou.kevin.advent.year2015; | ||
|
||
import java.io.BufferedReader; | ||
|
||
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 Day11 extends AbstractDaySolver<String> { | ||
|
||
public Day11() { | ||
super(Year.YEAR_2015, Day.DAY_11); | ||
} | ||
|
||
private static boolean isPasswordInvalid(String password) { | ||
if (password.matches("^.*[iol].*$")) { | ||
return true; | ||
} | ||
|
||
boolean firstPairFound = false; | ||
char firstPairChar = 0; | ||
boolean secondPairFound = false; | ||
|
||
boolean straightFound = false; | ||
|
||
for (int i = 0; i < password.length() - 1; i++) { | ||
char currentChar = password.charAt(i); | ||
char nextChar = password.charAt(i + 1); | ||
|
||
if (nextChar == currentChar) { | ||
if (!firstPairFound) { | ||
firstPairFound = true; | ||
firstPairChar = currentChar; | ||
} else if (nextChar != firstPairChar) { | ||
secondPairFound = true; | ||
} | ||
} else if (nextChar == currentChar + 1 && i < password.length() - 2) { | ||
char nextNextChar = password.charAt(i + 2); | ||
|
||
if (nextNextChar == nextChar + 1) { | ||
straightFound = true; | ||
} | ||
} | ||
|
||
if (firstPairFound && secondPairFound && straightFound) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static String incrementPassword(String password) { | ||
StringBuilder newPassword = new StringBuilder(); | ||
|
||
boolean incrementNextChar = true; | ||
|
||
for (int i = password.length() - 1; i >= 0; i--) { | ||
char currentChar = password.charAt(i); | ||
|
||
if (!incrementNextChar) { | ||
newPassword.append(currentChar); | ||
continue; | ||
} | ||
|
||
if (currentChar == 'z') { | ||
newPassword.append('a'); | ||
} else { | ||
newPassword.append((char) (currentChar + 1)); | ||
incrementNextChar = false; | ||
} | ||
} | ||
|
||
return newPassword.reverse().toString(); | ||
} | ||
|
||
@Override | ||
protected String doSolveFirstStar(BufferedReader reader) { | ||
String password = reader.lines().findFirst().orElseThrow(); | ||
|
||
do { | ||
password = incrementPassword(password); | ||
} while (isPasswordInvalid(password)); | ||
|
||
return password; | ||
} | ||
|
||
@Override | ||
public String getFirstStarSolution() { | ||
return "vzbxxyzz"; | ||
} | ||
|
||
@Override | ||
protected String doSolveSecondStar(BufferedReader reader) { | ||
String password = getFirstStarSolution(); | ||
|
||
do { | ||
password = incrementPassword(password); | ||
} while (isPasswordInvalid(password)); | ||
|
||
return password; | ||
} | ||
|
||
@Override | ||
public String getSecondStarSolution() { | ||
return "vzcaabcc"; | ||
} | ||
} |
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
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
Oops, something went wrong.