Skip to content

Commit

Permalink
refactor: refactor day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 3, 2024
1 parent 644e6bb commit bad17cf
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 84 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 12 additions & 40 deletions src/main/java/com/adventofcode/flashk/day03/MullItOver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> inputs;
private final List<String> inputs;

public MullItOver(List<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 9 additions & 42 deletions src/test/java/com/adventofcode/flashk/day03/Day03Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,7 +39,7 @@ public void testSolvePart1Sample() {

MullItOver mullItOver = new MullItOver(inputs);

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

@Test
Expand All @@ -54,44 +55,10 @@ public void testSolvePart1Input() {
List<String> 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<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)
Expand All @@ -108,7 +75,7 @@ void testSolvePart2InputByLines() {

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

assertEquals(26546863, mullItOver.solveB());
assertEquals(26546863, mullItOver.solve(true));

}

Expand All @@ -122,10 +89,10 @@ public void testSolvePart2Sample() {
System.out.print("2 | sample | ");

// 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))"));
List<String> 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
Expand All @@ -141,7 +108,7 @@ public void testSolvePart2Input() {
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
MullItOver mullItOver = new MullItOver(inputs);

assertEquals(84893551, mullItOver.solveB());
assertEquals(84893551, mullItOver.solve(true));

}

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

0 comments on commit bad17cf

Please sign in to comment.