Skip to content

Commit

Permalink
Simplified new day creation
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-belellou committed Nov 23, 2024
1 parent c06bd13 commit 233bf00
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

Expand All @@ -13,19 +15,30 @@ public abstract class AbstractDaySolver<T> implements DaySolver<T> {

private static final String FIRST_SOLUTION = " - First solution: ";
private static final String SECOND_SOLUTION = " - Second solution: ";
private static final String REGEX_NUMBER = "(\\d+)";

private final Year year;
private final Day day;

private final String input;

protected AbstractDaySolver(Year year, Day day) {
this.year = year;
this.day = day;
protected AbstractDaySolver(Class<? extends AbstractDaySolver<T>> clazz) {
this.year = Year.getYear(getNumberFrom(clazz.getPackageName()));
this.day = Day.getDay(getNumberFrom(clazz.getSimpleName()));

input = INPUT_FOLDER + year.toString() + SEPARATOR + day.toString() + INPUT_FILE_NAME;
}

private static String getNumberFrom(String input) {
Matcher matcher = Pattern.compile(REGEX_NUMBER).matcher(input);

if (!matcher.find()) {
throw new IllegalArgumentException("Invalid input: " + input);
}

return matcher.group(1);
}

private BufferedReader getReader() {
try {
FileReader fileReader = new FileReader(input);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/belellou/kevin/advent/generic/Day.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ public enum Day {
DAY_24("24"),
DAY_25("25");

private static final String PREFIX = "DAY_";

private final String day;

Day(final String day) {
this.day = day;
}

public static Day getDay(String day) {
return Day.valueOf(PREFIX + day);
}

@Override
public String toString() {
return day;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/belellou/kevin/advent/generic/Year.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ public enum Year {
YEAR_2023("2023"),
YEAR_2024("2024");

private static final String PREFIX = "YEAR_";

private final String year;

Year(final String year) {
this.year = year;
}

public static Year getYear(String year) {
return Year.valueOf(PREFIX + year);
}

@Override
public String toString() {
return year;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.belellou.kevin.advent.generic;
package com.belellou.kevin.advent.year0;

import java.io.BufferedReader;

import com.belellou.kevin.advent.generic.AbstractDaySolver;
import com.belellou.kevin.advent.generic.DisableTest;

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

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

@Override
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/belellou/kevin/advent/year2015/Day1.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
import org.apache.commons.lang3.StringUtils;

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 Day1 extends AbstractDaySolver<Integer> {

private static final int STARTING_FLOOR = 0;

public Day1() {
super(Year.YEAR_2015, Day.DAY_1);
super(Day1.class);
}

@Override
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/belellou/kevin/advent/year2015/Day10.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
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 Day10 extends AbstractDaySolver<Integer> {

public Day10() {
super(Year.YEAR_2015, Day.DAY_10);
super(Day10.class);
}

private static String readLine(String line) {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/belellou/kevin/advent/year2015/Day11.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
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);
super(Day11.class);
}

private static boolean isPasswordInvalid(String password) {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/belellou/kevin/advent/year2015/Day2.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@
import org.apache.commons.lang3.math.NumberUtils;

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 Day2 extends AbstractDaySolver<Integer> {

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

public Day2() {
super(Year.YEAR_2015, Day.DAY_2);
super(Day2.class);
}

private static int findAreaNeeded(String line) {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/belellou/kevin/advent/year2015/Day3.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
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 Day3 extends AbstractDaySolver<Integer> {

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

@Override
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/belellou/kevin/advent/year2015/Day4.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import org.apache.commons.codec.digest.DigestUtils;

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 Day4 extends AbstractDaySolver<Integer> {
Expand All @@ -17,7 +15,7 @@ public class Day4 extends AbstractDaySolver<Integer> {
private static final String SIX_ZEROES_PATTERN = "000000";

public Day4() {
super(Year.YEAR_2015, Day.DAY_4);
super(Day4.class);
}

private static int findNumber(String line, String pattern) {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/belellou/kevin/advent/year2015/Day5.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
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<Integer> {

public Day5() {
super(Year.YEAR_2015, Day.DAY_5);
super(Day5.class);
}

private static boolean firstStarStringTest(String string) {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/belellou/kevin/advent/year2015/Day6.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import java.util.regex.Pattern;

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 Day6 extends AbstractDaySolver<Integer> {
Expand All @@ -32,7 +30,7 @@ public class Day6 extends AbstractDaySolver<Integer> {
private static final String COMMAND_TOGGLE = "toggle";

public Day6() {
super(Year.YEAR_2015, Day.DAY_6);
super(Day6.class);
}

private static Instruction matcherToInstruction(Matcher matcher) {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/belellou/kevin/advent/year2015/Day7.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import org.jgrapht.traverse.TopologicalOrderIterator;

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 Day7 extends AbstractDaySolver<Integer> {
Expand Down Expand Up @@ -60,7 +58,7 @@ public class Day7 extends AbstractDaySolver<Integer> {
"$");

public Day7() {
super(Year.YEAR_2015, Day.DAY_7);
super(Day7.class);
}

@SuppressWarnings("DuplicatedCode")
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/belellou/kevin/advent/year2015/Day8.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import java.util.regex.Pattern;

import com.belellou.kevin.advent.generic.AbstractDaySolver;
import com.belellou.kevin.advent.generic.Day;
import com.belellou.kevin.advent.generic.DisableTest;
import com.belellou.kevin.advent.generic.Year;

@SuppressWarnings("unused")
public class Day8 extends AbstractDaySolver<Integer> {
Expand All @@ -23,7 +21,7 @@ public class Day8 extends AbstractDaySolver<Integer> {
private static final Map<String, Integer> memAgain = new HashMap<>();

public Day8() {
super(Year.YEAR_2015, Day.DAY_8);
super(Day8.class);
}

private static int countMemoryLengthAgain(String line) {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/belellou/kevin/advent/year2015/Day9.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
import org.jgrapht.graph.SimpleWeightedGraph;

import com.belellou.kevin.advent.generic.AbstractDaySolver;
import com.belellou.kevin.advent.generic.Day;
import com.belellou.kevin.advent.generic.DisableTest;
import com.belellou.kevin.advent.generic.Year;

@SuppressWarnings("unused")
public class Day9 extends AbstractDaySolver<Integer> {
Expand All @@ -33,7 +31,7 @@ public class Day9 extends AbstractDaySolver<Integer> {
"$");

public Day9() {
super(Year.YEAR_2015, Day.DAY_9);
super(Day9.class);
}

@Override
Expand Down

0 comments on commit 233bf00

Please sign in to comment.