diff --git a/src/main/java/com/adventofcode/flashk/day03/MullItOver.java b/src/main/java/com/adventofcode/flashk/day03/MullItOver.java index 124d7a0..4db5304 100644 --- a/src/main/java/com/adventofcode/flashk/day03/MullItOver.java +++ b/src/main/java/com/adventofcode/flashk/day03/MullItOver.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; public class MullItOver { @@ -35,28 +36,21 @@ public long solveA() { 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()); - } + 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 = input; - - Matcher matcher = IGNORE_MULTIPLICATIONS.matcher(modifiedInput); - if(matcher.find()) { - modifiedInput = modifiedInput.replaceAll("don't\\(\\).*?do\\(\\)", ""); - } + String modifiedInput = IGNORE_MULTIPLICATIONS.matcher(input).replaceAll(""); if(!modifiedInput.contains("don't()")) { return modifiedInput; @@ -64,26 +58,6 @@ private String removeMultiplications(String input) { 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) { @@ -92,8 +66,8 @@ private long multiply(String operation) { long op1; long op2; if(matcher.find()){ - op1 = Long.valueOf(matcher.group(1)); - op2 = Long.valueOf(matcher.group(2)); + op1 = Long.parseLong(matcher.group(1)); + op2 = Long.parseLong(matcher.group(2)); return op1 * op2; } return 0; diff --git a/src/test/java/com/adventofcode/flashk/day03/Day03Test.java b/src/test/java/com/adventofcode/flashk/day03/Day03Test.java index fb1fe42..2bab1fe 100644 --- a/src/test/java/com/adventofcode/flashk/day03/Day03Test.java +++ b/src/test/java/com/adventofcode/flashk/day03/Day03Test.java @@ -57,6 +57,60 @@ public void testSolvePart1Input() { assertEquals(160672468, mullItOver.solveA()); } + + @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) + @Tag(TestTag.PART_TWO) + @Tag(TestTag.INPUT) + @DisplayName(TestDisplayName.PART_TWO_DEBUG) + void testSolvePart2InputByLines() { + + 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(26546863, mullItOver.solveB()); + + } @Test @Order(3) @@ -87,16 +141,9 @@ public void testSolvePart2Input() { 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()); + assertEquals(84893551, mullItOver.solveB()); } + }