Skip to content

Commit 4d75341

Browse files
committed
feat: solve day 3 part 1
1 parent 5d59e4b commit 4d75341

File tree

4 files changed

+129
-11
lines changed

4 files changed

+129
-11
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.adventofcode.flashk.day03;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
import java.util.List;
6+
import java.util.regex.Matcher;
7+
import java.util.regex.Pattern;
8+
9+
public class MullItOver {
10+
11+
// [0-9][0-9]?[0-9]?,[0-9][0-9]?[0-9]?
12+
private static final Pattern PATTERN = Pattern.compile("(mul\\([0-9][0-9]?[0-9]?,[0-9][0-9]?[0-9]?\\))");
13+
private static final Pattern OPERATORS_PATTERN = Pattern.compile("([0-9][0-9]?[0-9]?),([0-9][0-9]?[0-9]?)");
14+
private static final Pattern IGNORE_MULTIPLICATIONS = Pattern.compile("don't\\(\\).*?do\\(\\)");
15+
16+
private List<String> inputs;
17+
18+
public MullItOver(List<String> inputs) {
19+
this.inputs = inputs;
20+
}
21+
22+
public long solveA() {
23+
24+
long result = 0;
25+
for(String input : inputs) {
26+
Matcher matcher = PATTERN.matcher(input);
27+
28+
while(matcher.find()){
29+
result += multiply(matcher.group());
30+
}
31+
}
32+
33+
return result;
34+
}
35+
36+
public long solveB() {
37+
long result = 0;
38+
for(String input : inputs) {
39+
40+
String cleanedInput = removeMultiplications(input);
41+
42+
Matcher matcher = PATTERN.matcher(cleanedInput);
43+
44+
while(matcher.find()){
45+
result += multiply(matcher.group());
46+
}
47+
}
48+
49+
return result;
50+
}
51+
52+
private String removeMultiplications(String input) {
53+
54+
String modifiedInput = input;
55+
56+
Matcher matcher = IGNORE_MULTIPLICATIONS.matcher(modifiedInput);
57+
if(matcher.find()) {
58+
modifiedInput = modifiedInput.replaceAll("don't\\(\\).*?do\\(\\)", "");
59+
}
60+
61+
if(!modifiedInput.contains("don't()")) {
62+
return modifiedInput;
63+
}
64+
65+
return modifiedInput.substring(0, modifiedInput.indexOf("don't()"));
66+
67+
/*
68+
while(modifiedInput.contains("don't()")) {
69+
//int startRemoveIndex = modifiedInput.indexOf("don't()");
70+
//int endRemoveIndex = modifiedInput.indexOf("do()") + "do()".length();
71+
72+
// Ojo:
73+
// Y si tienes...: don't() ... do() ... do() ... don't() ?
74+
75+
Matcher matcher = IGNORE_MULTIPLICATIONS.matcher(modifiedInput);
76+
if(matcher.find()) {
77+
modifiedInput = modifiedInput.replaceAll("don't\\(\\).*?do\\(\\)", "");
78+
} else {
79+
modifiedInput = modifiedInput.replaceAll("don't\\(\\)", "");
80+
}
81+
82+
// Y si tienes un don't() sin cerrar?
83+
//modifiedInput = modifiedInput.substring(0, startRemoveIndex) + modifiedInput.substring(endRemoveIndex, modifiedInput.length());
84+
}
85+
86+
return modifiedInput;*/
87+
}
88+
89+
private long multiply(String operation) {
90+
Matcher matcher = OPERATORS_PATTERN.matcher(operation);
91+
92+
long op1;
93+
long op2;
94+
if(matcher.find()){
95+
op1 = Long.valueOf(matcher.group(1));
96+
op2 = Long.valueOf(matcher.group(2));
97+
return op1 * op2;
98+
}
99+
return 0;
100+
}
101+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Day 3:
1+
# Day 3: Mull It Over
22

33
[https://adventofcode.com/2024/day/3](https://adventofcode.com/2024/day/3)

src/test/java/com/adventofcode/flashk/day03/Day03Test.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.List;
44

5-
import org.junit.jupiter.api.Disabled;
65
import org.junit.jupiter.api.DisplayName;
76
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
87
import org.junit.jupiter.api.Order;
@@ -17,15 +16,14 @@
1716
import com.adventofcode.flashk.common.test.utils.PuzzleTest;
1817
import com.adventofcode.flashk.common.test.utils.Input;
1918

19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
2021
@DisplayName(TestDisplayName.DAY_03)
2122
@TestMethodOrder(OrderAnnotation.class)
22-
@Disabled // TODO Remove comment when implemented
2323
public class Day03Test extends PuzzleTest {
2424

25-
private final static String INPUT_FOLDER = TestFolder.DAY_03;
25+
private static final String INPUT_FOLDER = TestFolder.DAY_03;
2626

27-
28-
2927
@Test
3028
@Order(1)
3129
@Tag(TestTag.PART_ONE)
@@ -37,7 +35,10 @@ public void testSolvePart1Sample() {
3735

3836
// Read input file
3937
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
40-
38+
39+
MullItOver mullItOver = new MullItOver(inputs);
40+
41+
assertEquals(161, mullItOver.solveA());
4142
}
4243

4344
@Test
@@ -51,7 +52,10 @@ public void testSolvePart1Input() {
5152

5253
// Read input file
5354
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
54-
55+
MullItOver mullItOver = new MullItOver(inputs);
56+
57+
assertEquals(160672468, mullItOver.solveA());
58+
5559
}
5660

5761
@Test
@@ -65,7 +69,9 @@ public void testSolvePart2Sample() {
6569

6670
// Read input file
6771
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
68-
72+
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))"));
73+
74+
assertEquals(48, mullItOver.solveB());
6975
}
7076

7177
@Test
@@ -79,7 +85,18 @@ public void testSolvePart2Input() {
7985

8086
// Read input file
8187
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
82-
88+
MullItOver mullItOver = new MullItOver(inputs);
89+
90+
System.out.println("Sol: "+mullItOver.solveB());
91+
92+
// Incorrectas:
93+
// 49347471
94+
// 112946746 -> Too high
95+
// 112946746
96+
// 93733733 -> Too high
97+
98+
//assertEquals(160672468, mullItOver.solveB());
99+
83100
}
84101

85102
}

src/test/resources/inputs

0 commit comments

Comments
 (0)