Skip to content

Commit 88201f3

Browse files
day03 for AoC 2023 (part 2)
Took 11 minutes
1 parent 50b3add commit 88201f3

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/main/java/aminetti/adventofcode2024/day03/Day03.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,34 @@ public long solvePart1() {
3838
}
3939

4040
public long solvePart2() {
41+
Pattern p = Pattern.compile("(?:mul\\((\\d{1,3}),(\\d{1,3})\\))|(?:do\\(\\))|(?:don't\\(\\))");
4142

42-
return 0;
43+
long sum = 0L;
44+
boolean mulEnabled = true;
45+
for (String s : input) {
46+
Matcher matcher = p.matcher(s);
47+
48+
while (matcher.find()) {
49+
String fullMatch = matcher.group(0);
50+
if (fullMatch.startsWith("mul")) {
51+
if (mulEnabled) {
52+
LOGGER.info("Executing {}", fullMatch);
53+
Long a = Long.valueOf(matcher.group(1));
54+
Long b = Long.valueOf(matcher.group(2));
55+
sum += a * b;
56+
} else {
57+
LOGGER.info("Skipping {}", fullMatch);
58+
}
59+
} else if (fullMatch.startsWith("do()")) {
60+
LOGGER.info("Enabling {}", fullMatch);
61+
mulEnabled = true;
62+
} else if (fullMatch.startsWith("don't()")) {
63+
LOGGER.info("Disabling {}", fullMatch);
64+
mulEnabled = false;
65+
}
66+
}
67+
}
68+
69+
return sum;
4370
}
4471
}

src/test/java/aminetti/adventofcode2024/day03/Day03Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,21 @@ void actualInputPart2() throws IOException {
5252
long l = solver.solvePart2();
5353

5454
// then
55-
assertThat(l, is(0L));
55+
assertThat(l, is(80570939L));
5656
}
5757

5858
@Test
5959
void testInputPart2() throws IOException {
6060
// given
61-
List<String> input = readLines(resourceToString("/day03/day03_input_test.txt", UTF_8));
61+
List<String> input = readLines(resourceToString("/day03/day03_input_test2.txt", UTF_8));
6262

6363
// when
6464
Day03 solver = new Day03();
6565
solver.parseInput(input);
6666
long l = solver.solvePart2();
6767

6868
// then
69-
assertThat(l, is(0L));
69+
assertThat(l, is(48L));
7070
}
7171

7272

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))

0 commit comments

Comments
 (0)