From 4d75341527014fc55071b6dd490857100ea745f7 Mon Sep 17 00:00:00 2001 From: Flashk Date: Tue, 3 Dec 2024 07:47:52 +0100 Subject: [PATCH] feat: solve day 3 part 1 --- .../adventofcode/flashk/day03/MullItOver.java | 101 ++++++++++++++++++ .../com/adventofcode/flashk/day03/README.md | 2 +- .../adventofcode/flashk/day03/Day03Test.java | 35 ++++-- src/test/resources/inputs | 2 +- 4 files changed, 129 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/adventofcode/flashk/day03/MullItOver.java diff --git a/src/main/java/com/adventofcode/flashk/day03/MullItOver.java b/src/main/java/com/adventofcode/flashk/day03/MullItOver.java new file mode 100644 index 0000000..124d7a0 --- /dev/null +++ b/src/main/java/com/adventofcode/flashk/day03/MullItOver.java @@ -0,0 +1,101 @@ +package com.adventofcode.flashk.day03; + +import org.apache.commons.lang3.StringUtils; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +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 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 List inputs; + + public MullItOver(List inputs) { + this.inputs = inputs; + } + + public long solveA() { + + long result = 0; + for(String input : inputs) { + Matcher matcher = PATTERN.matcher(input); + + while(matcher.find()){ + result += multiply(matcher.group()); + } + } + + return result; + } + + public long solveB() { + long result = 0; + for(String input : inputs) { + + String cleanedInput = removeMultiplications(input); + + Matcher matcher = PATTERN.matcher(cleanedInput); + + while(matcher.find()){ + result += multiply(matcher.group()); + } + } + + return result; + } + + private String removeMultiplications(String input) { + + String modifiedInput = input; + + Matcher matcher = IGNORE_MULTIPLICATIONS.matcher(modifiedInput); + if(matcher.find()) { + modifiedInput = modifiedInput.replaceAll("don't\\(\\).*?do\\(\\)", ""); + } + + if(!modifiedInput.contains("don't()")) { + return modifiedInput; + } + + return modifiedInput.substring(0, modifiedInput.indexOf("don't()")); + + /* + while(modifiedInput.contains("don't()")) { + //int startRemoveIndex = modifiedInput.indexOf("don't()"); + //int endRemoveIndex = modifiedInput.indexOf("do()") + "do()".length(); + + // Ojo: + // Y si tienes...: don't() ... do() ... do() ... don't() ? + + Matcher matcher = IGNORE_MULTIPLICATIONS.matcher(modifiedInput); + if(matcher.find()) { + modifiedInput = modifiedInput.replaceAll("don't\\(\\).*?do\\(\\)", ""); + } else { + modifiedInput = modifiedInput.replaceAll("don't\\(\\)", ""); + } + + // Y si tienes un don't() sin cerrar? + //modifiedInput = modifiedInput.substring(0, startRemoveIndex) + modifiedInput.substring(endRemoveIndex, modifiedInput.length()); + } + + return modifiedInput;*/ + } + + private long multiply(String operation) { + Matcher matcher = OPERATORS_PATTERN.matcher(operation); + + long op1; + long op2; + if(matcher.find()){ + op1 = Long.valueOf(matcher.group(1)); + op2 = Long.valueOf(matcher.group(2)); + return op1 * op2; + } + return 0; + } +} diff --git a/src/main/java/com/adventofcode/flashk/day03/README.md b/src/main/java/com/adventofcode/flashk/day03/README.md index 5acae42..bf20bad 100644 --- a/src/main/java/com/adventofcode/flashk/day03/README.md +++ b/src/main/java/com/adventofcode/flashk/day03/README.md @@ -1,3 +1,3 @@ -# Day 3: +# Day 3: Mull It Over [https://adventofcode.com/2024/day/3](https://adventofcode.com/2024/day/3) diff --git a/src/test/java/com/adventofcode/flashk/day03/Day03Test.java b/src/test/java/com/adventofcode/flashk/day03/Day03Test.java index ece1dee..fb1fe42 100644 --- a/src/test/java/com/adventofcode/flashk/day03/Day03Test.java +++ b/src/test/java/com/adventofcode/flashk/day03/Day03Test.java @@ -2,7 +2,6 @@ import java.util.List; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import org.junit.jupiter.api.Order; @@ -17,15 +16,14 @@ import com.adventofcode.flashk.common.test.utils.PuzzleTest; import com.adventofcode.flashk.common.test.utils.Input; +import static org.junit.jupiter.api.Assertions.assertEquals; + @DisplayName(TestDisplayName.DAY_03) @TestMethodOrder(OrderAnnotation.class) -@Disabled // TODO Remove comment when implemented public class Day03Test extends PuzzleTest { - private final static String INPUT_FOLDER = TestFolder.DAY_03; + private static final String INPUT_FOLDER = TestFolder.DAY_03; - - @Test @Order(1) @Tag(TestTag.PART_ONE) @@ -37,7 +35,10 @@ public void testSolvePart1Sample() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE); - + + MullItOver mullItOver = new MullItOver(inputs); + + assertEquals(161, mullItOver.solveA()); } @Test @@ -51,7 +52,10 @@ public void testSolvePart1Input() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE); - + MullItOver mullItOver = new MullItOver(inputs); + + assertEquals(160672468, mullItOver.solveA()); + } @Test @@ -65,7 +69,9 @@ public void testSolvePart2Sample() { // 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))")); + + assertEquals(48, mullItOver.solveB()); } @Test @@ -79,7 +85,18 @@ public void testSolvePart2Input() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE); - + MullItOver mullItOver = new MullItOver(inputs); + + System.out.println("Sol: "+mullItOver.solveB()); + + // Incorrectas: + // 49347471 + // 112946746 -> Too high + // 112946746 + // 93733733 -> Too high + + //assertEquals(160672468, mullItOver.solveB()); + } } diff --git a/src/test/resources/inputs b/src/test/resources/inputs index 5449c50..a073024 160000 --- a/src/test/resources/inputs +++ b/src/test/resources/inputs @@ -1 +1 @@ -Subproject commit 5449c504153b1c57ca7aca20bdce5e8894bd1d0e +Subproject commit a07302456b5a35431eb5d661dc660d9500df5215