Skip to content

Commit

Permalink
feat: solve day 3 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 3, 2024
1 parent 5d59e4b commit 4d75341
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 11 deletions.
101 changes: 101 additions & 0 deletions src/main/java/com/adventofcode/flashk/day03/MullItOver.java
Original file line number Diff line number Diff line change
@@ -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<String> inputs;

public MullItOver(List<String> 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;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/adventofcode/flashk/day03/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Day 3:
# Day 3: Mull It Over

[https://adventofcode.com/2024/day/3](https://adventofcode.com/2024/day/3)
35 changes: 26 additions & 9 deletions src/test/java/com/adventofcode/flashk/day03/Day03Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -37,7 +35,10 @@ public void testSolvePart1Sample() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);


MullItOver mullItOver = new MullItOver(inputs);

assertEquals(161, mullItOver.solveA());
}

@Test
Expand All @@ -51,7 +52,10 @@ public void testSolvePart1Input() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);

MullItOver mullItOver = new MullItOver(inputs);

assertEquals(160672468, mullItOver.solveA());

}

@Test
Expand All @@ -65,7 +69,9 @@ public void testSolvePart2Sample() {

// Read input file
List<String> 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
Expand All @@ -79,7 +85,18 @@ public void testSolvePart2Input() {

// Read input file
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());

}

}
2 changes: 1 addition & 1 deletion src/test/resources/inputs

0 comments on commit 4d75341

Please sign in to comment.