Skip to content

Commit

Permalink
Added 2015 Day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-belellou committed Nov 23, 2024
1 parent 599faa0 commit c06bd13
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.io.FileReader;
import java.io.IOException;

public abstract class AbstractDaySolver implements DaySolver {
public abstract class AbstractDaySolver<T> implements DaySolver<T> {

private static final String SEPARATOR = "/";
private static final String INPUT_FOLDER = "src/main/resources/";
Expand Down Expand Up @@ -36,9 +36,9 @@ private BufferedReader getReader() {
}

@Override
public int solveFirstStar() {
public T solveFirstStar() {
try (BufferedReader reader = getReader()) {
int firstSolution = doSolveFirstStar(reader);
T firstSolution = doSolveFirstStar(reader);

System.out.println(this + FIRST_SOLUTION + firstSolution);

Expand All @@ -49,9 +49,9 @@ public int solveFirstStar() {
}

@Override
public int solveSecondStar() {
public T solveSecondStar() {
try (BufferedReader reader = getReader()) {
int secondSolution = doSolveSecondStar(reader);
T secondSolution = doSolveSecondStar(reader);

System.out.println(this + SECOND_SOLUTION + secondSolution);

Expand All @@ -61,9 +61,9 @@ public int solveSecondStar() {
}
}

protected abstract int doSolveFirstStar(BufferedReader reader) throws IOException;
protected abstract T doSolveFirstStar(BufferedReader reader) throws IOException;

protected abstract int doSolveSecondStar(BufferedReader reader) throws IOException;
protected abstract T doSolveSecondStar(BufferedReader reader) throws IOException;

@Override
public String toString() {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/belellou/kevin/advent/generic/Day0.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@
import java.io.BufferedReader;

@SuppressWarnings("unused")
public class Day0 extends AbstractDaySolver {
public class Day0 extends AbstractDaySolver<Integer> {

public Day0() {
super(Year.YEAR_0, Day.DAY_0);
}

@Override
protected int doSolveFirstStar(BufferedReader reader) {
protected Integer doSolveFirstStar(BufferedReader reader) {
return 0;
}

@DisableTest
@Override
public int getFirstStarSolution() {
public Integer getFirstStarSolution() {
return 0;
}

@Override
protected int doSolveSecondStar(BufferedReader reader) {
protected Integer doSolveSecondStar(BufferedReader reader) {
return 0;
}

@DisableTest
@Override
public int getSecondStarSolution() {
public Integer getSecondStarSolution() {
return 0;
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/belellou/kevin/advent/generic/DaySolver.java
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();
}
10 changes: 5 additions & 5 deletions src/main/java/com/belellou/kevin/advent/year2015/Day1.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.belellou.kevin.advent.generic.Year;

@SuppressWarnings("unused")
public class Day1 extends AbstractDaySolver {
public class Day1 extends AbstractDaySolver<Integer> {

private static final int STARTING_FLOOR = 0;

Expand All @@ -19,7 +19,7 @@ public Day1() {
}

@Override
protected int doSolveFirstStar(BufferedReader reader) throws IOException {
protected Integer doSolveFirstStar(BufferedReader reader) throws IOException {
String line = reader.readLine();

int up = StringUtils.countMatches(line, "(");
Expand All @@ -29,12 +29,12 @@ protected int doSolveFirstStar(BufferedReader reader) throws IOException {
}

@Override
public int getFirstStarSolution() {
public Integer getFirstStarSolution() {
return 74;
}

@Override
protected int doSolveSecondStar(BufferedReader reader) throws IOException {
protected Integer doSolveSecondStar(BufferedReader reader) throws IOException {
String line = reader.readLine();

int currentFloor = STARTING_FLOOR;
Expand All @@ -56,7 +56,7 @@ protected int doSolveSecondStar(BufferedReader reader) throws IOException {
}

@Override
public int getSecondStarSolution() {
public Integer getSecondStarSolution() {
return 1_795;
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/belellou/kevin/advent/year2015/Day10.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.belellou.kevin.advent.generic.Year;

@SuppressWarnings("unused")
public class Day10 extends AbstractDaySolver {
public class Day10 extends AbstractDaySolver<Integer> {

public Day10() {
super(Year.YEAR_2015, Day.DAY_10);
Expand All @@ -34,7 +34,7 @@ private static String readLine(String line) {
}

@Override
protected int doSolveFirstStar(BufferedReader reader) {
protected Integer doSolveFirstStar(BufferedReader reader) {
String line = reader.lines().findFirst().orElseThrow();

for (int i = 0; i < 40; i++) {
Expand All @@ -45,12 +45,12 @@ protected int doSolveFirstStar(BufferedReader reader) {
}

@Override
public int getFirstStarSolution() {
public Integer getFirstStarSolution() {
return 252_594;
}

@Override
protected int doSolveSecondStar(BufferedReader reader) {
protected Integer doSolveSecondStar(BufferedReader reader) {
String line = reader.lines().findFirst().orElseThrow();

for (int i = 0; i < 50; i++) {
Expand All @@ -61,7 +61,7 @@ protected int doSolveSecondStar(BufferedReader reader) {
}

@Override
public int getSecondStarSolution() {
public Integer getSecondStarSolution() {
return 3_579_328;
}
}
109 changes: 109 additions & 0 deletions src/main/java/com/belellou/kevin/advent/year2015/Day11.java
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";
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/belellou/kevin/advent/year2015/Day2.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.belellou.kevin.advent.generic.Year;

@SuppressWarnings("unused")
public class Day2 extends AbstractDaySolver {
public class Day2 extends AbstractDaySolver<Integer> {

private static final Pattern PATTERN = Pattern.compile("^(\\d+)x(\\d+)x(\\d+)$");

Expand Down Expand Up @@ -52,22 +52,22 @@ private static Dimensions getDimensions(String line) {
}

@Override
protected int doSolveFirstStar(BufferedReader reader) {
protected Integer doSolveFirstStar(BufferedReader reader) {
return reader.lines().map(Day2::findAreaNeeded).reduce(Integer::sum).orElseThrow();
}

@Override
public int getFirstStarSolution() {
public Integer getFirstStarSolution() {
return 1_588_178;
}

@Override
protected int doSolveSecondStar(BufferedReader reader) {
protected Integer doSolveSecondStar(BufferedReader reader) {
return reader.lines().map(Day2::findLengthNeeded).reduce(Integer::sum).orElseThrow();
}

@Override
public int getSecondStarSolution() {
public Integer getSecondStarSolution() {
return 3_783_758;
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/belellou/kevin/advent/year2015/Day3.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
import com.belellou.kevin.advent.generic.Year;

@SuppressWarnings("unused")
public class Day3 extends AbstractDaySolver {
public class Day3 extends AbstractDaySolver<Integer> {

public Day3() {
super(Year.YEAR_2015, Day.DAY_3);
}

@Override
protected int doSolveFirstStar(BufferedReader reader) throws IOException {
protected Integer doSolveFirstStar(BufferedReader reader) throws IOException {
String line = reader.readLine();
int length = line.length();

Expand Down Expand Up @@ -49,12 +49,12 @@ protected int doSolveFirstStar(BufferedReader reader) throws IOException {
}

@Override
public int getFirstStarSolution() {
public Integer getFirstStarSolution() {
return 2_081;
}

@Override
protected int doSolveSecondStar(BufferedReader reader) throws IOException {
protected Integer doSolveSecondStar(BufferedReader reader) throws IOException {
String line = reader.readLine();
int length = line.length();

Expand Down Expand Up @@ -105,7 +105,7 @@ protected int doSolveSecondStar(BufferedReader reader) throws IOException {
}

@Override
public int getSecondStarSolution() {
public Integer getSecondStarSolution() {
return 2_341;
}

Expand Down
Loading

0 comments on commit c06bd13

Please sign in to comment.