From bad17cf1bbd35d2d9f6b7e53fbc501ded2041400 Mon Sep 17 00:00:00 2001 From: Flashky Date: Tue, 3 Dec 2024 09:56:37 +0100 Subject: [PATCH] refactor: refactor day 3 --- README.md | 2 +- .../adventofcode/flashk/day03/MullItOver.java | 52 +++++-------------- .../common/test/constants/TestFilename.java | 1 + .../adventofcode/flashk/day03/Day03Test.java | 51 ++++-------------- src/test/resources/inputs | 2 +- 5 files changed, 24 insertions(+), 84 deletions(-) diff --git a/README.md b/README.md index a5d2663..42183a5 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ - [Day 1 - Historian Hysteria](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day01) - [Day 2 - Red-Nosed Reports](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day02) -- [Day 3](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day03) +- [Day 3 - Mull It Over](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day03) - [Day 4](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day04) - [Day 5](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day05) - [Day 6](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day06) diff --git a/src/main/java/com/adventofcode/flashk/day03/MullItOver.java b/src/main/java/com/adventofcode/flashk/day03/MullItOver.java index 4db5304..f344b0c 100644 --- a/src/main/java/com/adventofcode/flashk/day03/MullItOver.java +++ b/src/main/java/com/adventofcode/flashk/day03/MullItOver.java @@ -5,71 +5,43 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.stream.Collectors; public class MullItOver { - // [0-9][0-9]?[0-9]?,[0-9][0-9]?[0-9]? - private static final Pattern PATTERN = Pattern.compile("(mul\\([0-9][0-9]?[0-9]?,[0-9][0-9]?[0-9]?\\))"); + private static final Pattern MULTIPLY_PATTERN = Pattern.compile("(mul\\([0-9][0-9]?[0-9]?,[0-9][0-9]?[0-9]?\\))"); private static final Pattern OPERATORS_PATTERN = Pattern.compile("([0-9][0-9]?[0-9]?),([0-9][0-9]?[0-9]?)"); private static final Pattern IGNORE_MULTIPLICATIONS = Pattern.compile("don't\\(\\).*?do\\(\\)"); + private static final String DONT = "don't()"; - private List inputs; + private final List inputs; public MullItOver(List inputs) { this.inputs = inputs; } - public long solveA() { + public long solve(boolean cleanMemory) { - long result = 0; - for(String input : inputs) { - Matcher matcher = PATTERN.matcher(input); - - while(matcher.find()){ - result += multiply(matcher.group()); - } - } + // The input is just a single word, even when the input is a list of lines. + String input = String.join(StringUtils.EMPTY, inputs); + String memory = cleanMemory ? clean(input) : input; - return result; - } - - public long solveB() { long result = 0; + Matcher matcher = MULTIPLY_PATTERN.matcher(memory); - String complete = String.join("", inputs); - String cleanedInput = removeMultiplications(complete); - Matcher matcher = PATTERN.matcher(cleanedInput); while(matcher.find()){ result += multiply(matcher.group()); } - return result; } - private String removeMultiplications(String input) { - - String modifiedInput = IGNORE_MULTIPLICATIONS.matcher(input).replaceAll(""); - - if(!modifiedInput.contains("don't()")) { - return modifiedInput; - } - - return modifiedInput.substring(0, modifiedInput.indexOf("don't()")); - + private String clean(String input) { + String modifiedInput = IGNORE_MULTIPLICATIONS.matcher(input).replaceAll(StringUtils.EMPTY); + return modifiedInput.contains(DONT) ? modifiedInput.substring(0, modifiedInput.indexOf(DONT)) : modifiedInput; } private long multiply(String operation) { Matcher matcher = OPERATORS_PATTERN.matcher(operation); - - long op1; - long op2; - if(matcher.find()){ - op1 = Long.parseLong(matcher.group(1)); - op2 = Long.parseLong(matcher.group(2)); - return op1 * op2; - } - return 0; + return matcher.find() ? Long.parseLong(matcher.group(1)) * Long.parseLong(matcher.group(2)) : 0; } } diff --git a/src/test/java/com/adventofcode/flashk/common/test/constants/TestFilename.java b/src/test/java/com/adventofcode/flashk/common/test/constants/TestFilename.java index 4c01789..6a6c88a 100644 --- a/src/test/java/com/adventofcode/flashk/common/test/constants/TestFilename.java +++ b/src/test/java/com/adventofcode/flashk/common/test/constants/TestFilename.java @@ -7,6 +7,7 @@ private TestFilename() {} // Common filenames used by all tests public final static String INPUT_FILE = "data.input"; public final static String INPUT_FILE_SAMPLE = "sample.input"; + public final static String INPUT_FILE_SAMPLE_PART_2 = "sample_part_2.input"; public final static String INPUT_FILE_SINGLE_SAMPLE = "single_sample.input"; // Other tests diff --git a/src/test/java/com/adventofcode/flashk/day03/Day03Test.java b/src/test/java/com/adventofcode/flashk/day03/Day03Test.java index 2bab1fe..55560e8 100644 --- a/src/test/java/com/adventofcode/flashk/day03/Day03Test.java +++ b/src/test/java/com/adventofcode/flashk/day03/Day03Test.java @@ -16,11 +16,12 @@ import com.adventofcode.flashk.common.test.utils.PuzzleTest; import com.adventofcode.flashk.common.test.utils.Input; +import static com.adventofcode.flashk.common.test.constants.TestFilename.INPUT_FILE_SAMPLE_PART_2; import static org.junit.jupiter.api.Assertions.assertEquals; @DisplayName(TestDisplayName.DAY_03) @TestMethodOrder(OrderAnnotation.class) -public class Day03Test extends PuzzleTest { +class Day03Test extends PuzzleTest { private static final String INPUT_FOLDER = TestFolder.DAY_03; @@ -38,7 +39,7 @@ public void testSolvePart1Sample() { MullItOver mullItOver = new MullItOver(inputs); - assertEquals(161, mullItOver.solveA()); + assertEquals(161, mullItOver.solve(false)); } @Test @@ -54,44 +55,10 @@ public void testSolvePart1Input() { List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE); MullItOver mullItOver = new MullItOver(inputs); - assertEquals(160672468, mullItOver.solveA()); + assertEquals(160672468, mullItOver.solve(false)); } - @Test - @Order(2) - @Tag(TestTag.PART_ONE) - @Tag(TestTag.INPUT) - @DisplayName(TestDisplayName.PART_ONE_DEBUG) - public void testSolvePart1InputByLines() { - - System.out.print("1 | input | "); - - // Read input file - List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE); - - - MullItOver mullItOver = new MullItOver(List.of(inputs.get(0))); - - assertEquals(30243322, mullItOver.solveA()); - - mullItOver = new MullItOver(List.of(inputs.get(1))); - assertEquals(29041171, mullItOver.solveA()); - - mullItOver = new MullItOver(List.of(inputs.get(2))); - assertEquals(22674551, mullItOver.solveA()); - - mullItOver = new MullItOver(List.of(inputs.get(3))); - assertEquals(26491961, mullItOver.solveA()); - - mullItOver = new MullItOver(List.of(inputs.get(4))); - assertEquals(24532437, mullItOver.solveA()); - - mullItOver = new MullItOver(List.of(inputs.get(5))); - assertEquals(27689026, mullItOver.solveA()); - - - } @Test @Order(2) @@ -108,7 +75,7 @@ void testSolvePart2InputByLines() { MullItOver mullItOver = new MullItOver(List.of(inputs.get(0))); - assertEquals(26546863, mullItOver.solveB()); + assertEquals(26546863, mullItOver.solve(true)); } @@ -122,10 +89,10 @@ public void testSolvePart2Sample() { System.out.print("2 | sample | "); // Read input file - List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE); - MullItOver mullItOver = new MullItOver(List.of("xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))")); + List inputs = Input.readStringLines(INPUT_FOLDER, INPUT_FILE_SAMPLE_PART_2); + MullItOver mullItOver = new MullItOver(inputs); - assertEquals(48, mullItOver.solveB()); + assertEquals(48, mullItOver.solve(true)); } @Test @@ -141,7 +108,7 @@ public void testSolvePart2Input() { List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE); MullItOver mullItOver = new MullItOver(inputs); - assertEquals(84893551, mullItOver.solveB()); + assertEquals(84893551, mullItOver.solve(true)); } diff --git a/src/test/resources/inputs b/src/test/resources/inputs index a073024..df8c9c4 160000 --- a/src/test/resources/inputs +++ b/src/test/resources/inputs @@ -1 +1 @@ -Subproject commit a07302456b5a35431eb5d661dc660d9500df5215 +Subproject commit df8c9c4b09846914a69f29bea5677e804c69f7c1