Skip to content

Commit

Permalink
feat: solve day 3 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 3, 2024
1 parent 4d75341 commit 644e6bb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 45 deletions.
46 changes: 10 additions & 36 deletions src/main/java/com/adventofcode/flashk/day03/MullItOver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -35,55 +36,28 @@ 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;
}

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) {
Expand All @@ -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;
Expand Down
65 changes: 56 additions & 9 deletions src/test/java/com/adventofcode/flashk/day03/Day03Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);


MullItOver mullItOver = new MullItOver(List.of(inputs.get(0)));

assertEquals(26546863, mullItOver.solveB());

}

@Test
@Order(3)
Expand Down Expand Up @@ -87,16 +141,9 @@ public void testSolvePart2Input() {
List<String> 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());

}


}

0 comments on commit 644e6bb

Please sign in to comment.