From 233bf00bc7622a1ccc2ee471e2be970a0fa4cb86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Belellou?= Date: Sat, 23 Nov 2024 21:15:13 +0100 Subject: [PATCH] Simplified new day creation --- .../advent/generic/AbstractDaySolver.java | 19 ++++++++++++++++--- .../belellou/kevin/advent/generic/Day.java | 6 ++++++ .../belellou/kevin/advent/generic/Year.java | 6 ++++++ .../kevin/advent/{generic => year0}/Day0.java | 7 +++++-- .../belellou/kevin/advent/year2015/Day1.java | 4 +--- .../belellou/kevin/advent/year2015/Day10.java | 4 +--- .../belellou/kevin/advent/year2015/Day11.java | 4 +--- .../belellou/kevin/advent/year2015/Day2.java | 4 +--- .../belellou/kevin/advent/year2015/Day3.java | 4 +--- .../belellou/kevin/advent/year2015/Day4.java | 4 +--- .../belellou/kevin/advent/year2015/Day5.java | 4 +--- .../belellou/kevin/advent/year2015/Day6.java | 4 +--- .../belellou/kevin/advent/year2015/Day7.java | 4 +--- .../belellou/kevin/advent/year2015/Day8.java | 4 +--- .../belellou/kevin/advent/year2015/Day9.java | 4 +--- 15 files changed, 44 insertions(+), 38 deletions(-) rename src/main/java/com/belellou/kevin/advent/{generic => year0}/Day0.java (75%) diff --git a/src/main/java/com/belellou/kevin/advent/generic/AbstractDaySolver.java b/src/main/java/com/belellou/kevin/advent/generic/AbstractDaySolver.java index a156f8c..4722407 100644 --- a/src/main/java/com/belellou/kevin/advent/generic/AbstractDaySolver.java +++ b/src/main/java/com/belellou/kevin/advent/generic/AbstractDaySolver.java @@ -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 implements DaySolver { @@ -13,19 +15,30 @@ public abstract class AbstractDaySolver implements DaySolver { 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> 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); diff --git a/src/main/java/com/belellou/kevin/advent/generic/Day.java b/src/main/java/com/belellou/kevin/advent/generic/Day.java index ebd7e7f..ef2714f 100644 --- a/src/main/java/com/belellou/kevin/advent/generic/Day.java +++ b/src/main/java/com/belellou/kevin/advent/generic/Day.java @@ -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; diff --git a/src/main/java/com/belellou/kevin/advent/generic/Year.java b/src/main/java/com/belellou/kevin/advent/generic/Year.java index a70d480..2909212 100644 --- a/src/main/java/com/belellou/kevin/advent/generic/Year.java +++ b/src/main/java/com/belellou/kevin/advent/generic/Year.java @@ -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; diff --git a/src/main/java/com/belellou/kevin/advent/generic/Day0.java b/src/main/java/com/belellou/kevin/advent/year0/Day0.java similarity index 75% rename from src/main/java/com/belellou/kevin/advent/generic/Day0.java rename to src/main/java/com/belellou/kevin/advent/year0/Day0.java index 809557c..d182c40 100644 --- a/src/main/java/com/belellou/kevin/advent/generic/Day0.java +++ b/src/main/java/com/belellou/kevin/advent/year0/Day0.java @@ -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 { public Day0() { - super(Year.YEAR_0, Day.DAY_0); + super(Day0.class); } @Override diff --git a/src/main/java/com/belellou/kevin/advent/year2015/Day1.java b/src/main/java/com/belellou/kevin/advent/year2015/Day1.java index 37a2967..44c555e 100644 --- a/src/main/java/com/belellou/kevin/advent/year2015/Day1.java +++ b/src/main/java/com/belellou/kevin/advent/year2015/Day1.java @@ -6,8 +6,6 @@ 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 { @@ -15,7 +13,7 @@ public class Day1 extends AbstractDaySolver { private static final int STARTING_FLOOR = 0; public Day1() { - super(Year.YEAR_2015, Day.DAY_1); + super(Day1.class); } @Override diff --git a/src/main/java/com/belellou/kevin/advent/year2015/Day10.java b/src/main/java/com/belellou/kevin/advent/year2015/Day10.java index 75bf6cc..3b4f3e9 100644 --- a/src/main/java/com/belellou/kevin/advent/year2015/Day10.java +++ b/src/main/java/com/belellou/kevin/advent/year2015/Day10.java @@ -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 { public Day10() { - super(Year.YEAR_2015, Day.DAY_10); + super(Day10.class); } private static String readLine(String line) { diff --git a/src/main/java/com/belellou/kevin/advent/year2015/Day11.java b/src/main/java/com/belellou/kevin/advent/year2015/Day11.java index b3622ae..4fd3648 100644 --- a/src/main/java/com/belellou/kevin/advent/year2015/Day11.java +++ b/src/main/java/com/belellou/kevin/advent/year2015/Day11.java @@ -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 { public Day11() { - super(Year.YEAR_2015, Day.DAY_11); + super(Day11.class); } private static boolean isPasswordInvalid(String password) { diff --git a/src/main/java/com/belellou/kevin/advent/year2015/Day2.java b/src/main/java/com/belellou/kevin/advent/year2015/Day2.java index 45297b4..bfa24ba 100644 --- a/src/main/java/com/belellou/kevin/advent/year2015/Day2.java +++ b/src/main/java/com/belellou/kevin/advent/year2015/Day2.java @@ -8,8 +8,6 @@ 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 { @@ -17,7 +15,7 @@ public class Day2 extends AbstractDaySolver { 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) { diff --git a/src/main/java/com/belellou/kevin/advent/year2015/Day3.java b/src/main/java/com/belellou/kevin/advent/year2015/Day3.java index a5dae62..aaa0f01 100644 --- a/src/main/java/com/belellou/kevin/advent/year2015/Day3.java +++ b/src/main/java/com/belellou/kevin/advent/year2015/Day3.java @@ -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 { public Day3() { - super(Year.YEAR_2015, Day.DAY_3); + super(Day3.class); } @Override diff --git a/src/main/java/com/belellou/kevin/advent/year2015/Day4.java b/src/main/java/com/belellou/kevin/advent/year2015/Day4.java index 94112c8..93c6c8a 100644 --- a/src/main/java/com/belellou/kevin/advent/year2015/Day4.java +++ b/src/main/java/com/belellou/kevin/advent/year2015/Day4.java @@ -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 { @@ -17,7 +15,7 @@ public class Day4 extends AbstractDaySolver { 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) { diff --git a/src/main/java/com/belellou/kevin/advent/year2015/Day5.java b/src/main/java/com/belellou/kevin/advent/year2015/Day5.java index 44b1d8e..d974566 100644 --- a/src/main/java/com/belellou/kevin/advent/year2015/Day5.java +++ b/src/main/java/com/belellou/kevin/advent/year2015/Day5.java @@ -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 { public Day5() { - super(Year.YEAR_2015, Day.DAY_5); + super(Day5.class); } private static boolean firstStarStringTest(String string) { diff --git a/src/main/java/com/belellou/kevin/advent/year2015/Day6.java b/src/main/java/com/belellou/kevin/advent/year2015/Day6.java index cba7fa3..4a70627 100644 --- a/src/main/java/com/belellou/kevin/advent/year2015/Day6.java +++ b/src/main/java/com/belellou/kevin/advent/year2015/Day6.java @@ -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 { @@ -32,7 +30,7 @@ public class Day6 extends AbstractDaySolver { 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) { diff --git a/src/main/java/com/belellou/kevin/advent/year2015/Day7.java b/src/main/java/com/belellou/kevin/advent/year2015/Day7.java index 906abd9..e28c626 100644 --- a/src/main/java/com/belellou/kevin/advent/year2015/Day7.java +++ b/src/main/java/com/belellou/kevin/advent/year2015/Day7.java @@ -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 { @@ -60,7 +58,7 @@ public class Day7 extends AbstractDaySolver { "$"); public Day7() { - super(Year.YEAR_2015, Day.DAY_7); + super(Day7.class); } @SuppressWarnings("DuplicatedCode") diff --git a/src/main/java/com/belellou/kevin/advent/year2015/Day8.java b/src/main/java/com/belellou/kevin/advent/year2015/Day8.java index ace0b24..f3f6c1e 100644 --- a/src/main/java/com/belellou/kevin/advent/year2015/Day8.java +++ b/src/main/java/com/belellou/kevin/advent/year2015/Day8.java @@ -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 { @@ -23,7 +21,7 @@ public class Day8 extends AbstractDaySolver { private static final Map memAgain = new HashMap<>(); public Day8() { - super(Year.YEAR_2015, Day.DAY_8); + super(Day8.class); } private static int countMemoryLengthAgain(String line) { diff --git a/src/main/java/com/belellou/kevin/advent/year2015/Day9.java b/src/main/java/com/belellou/kevin/advent/year2015/Day9.java index 775adc1..af30135 100644 --- a/src/main/java/com/belellou/kevin/advent/year2015/Day9.java +++ b/src/main/java/com/belellou/kevin/advent/year2015/Day9.java @@ -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 { @@ -33,7 +31,7 @@ public class Day9 extends AbstractDaySolver { "$"); public Day9() { - super(Year.YEAR_2015, Day.DAY_9); + super(Day9.class); } @Override