Skip to content

Commit

Permalink
aoc 2024 day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCK committed Dec 3, 2024
1 parent cc9678c commit 64214f1
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 15 deletions.
6 changes: 3 additions & 3 deletions adventofcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1422,16 +1422,16 @@
[20232402tests]: src/test/java/org/ck/adventofcode/year2023/Day24Test.java
[20232501tests]: src/test/java/org/ck/adventofcode/year2023/Day25Test.java

# 2024 (4/49)
# 2024 (6/49)

| # | Name | Solution | Test |
|---------:|------------------------------------------------|:------------------------------------:|:---------------------------------:|
| 20240101 | [Day 1: Historian Hysteria][20240101] | ✅[💾][20240101solution] | ✅[💾][20240101tests] |
| 20240102 | [Day 1: Historian Hysteria - Part 2][20240102] | ✅[💾][20240102solution] | ✅[💾][20240102tests] |
| 20240201 | [Day 2: Red-Nosed Reports][20240201] | ✅[💾][20240201solution] | ✅[💾][20240201tests] |
| 20240202 | [Day 2: Red-Nosed Reports - Part 2][20240202] | ✅[💾][20240202solution] | ✅[💾][20240202tests] |
| 20240301 | [Day 3: ?][20240301] | [💾][20240301solution] | [💾][20240301tests] |
| 20240302 | [Day 3: ? - Part 2][20240302] | [💾][20240302solution] | [💾][20240302tests] |
| 20240301 | [Day 3: Mull It Over][20240301] | ✅[💾][20240301solution] | ✅[💾][20240301tests] |
| 20240302 | [Day 3: Mull It Over - Part 2][20240302] | ✅[💾][20240302solution] | ✅[💾][20240302tests] |
| 20240401 | [Day 4: ?][20240401] | [💾][20240401solution] | [💾][20240401tests] |
| 20240402 | [Day 4: ? - Part 2][20240402] | [💾][20240402solution] | [💾][20240402tests] |
| 20240501 | [Day 5: ?][20240501] | [💾][20240501solution] | [💾][20240501tests] |
Expand Down
62 changes: 52 additions & 10 deletions adventofcode/src/main/java/org/ck/adventofcode/year2024/Day03.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,76 @@
package org.ck.adventofcode.year2024;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.ck.adventofcode.util.AOCSolution;
import org.ck.codechallengelib.annotation.Solution;

@Solution(
id = 20240301,
name = "Day 3: ?",
name = "Day 3: Mull It Over",
url = "https://adventofcode.com/2024/day/3",
category = "2024",
solved = false)
category = "2024")
@Solution(
id = 20240302,
name = "Day 3: ? - Part 2",
name = "Day 3: Mull It Over - Part 2",
url = "https://adventofcode.com/2024/day/3#part2",
category = "2024",
solved = false)
category = "2024")
public class Day03 extends AOCSolution {
private static final Pattern PATTERN =
Pattern.compile("mul\\((?<one>\\d{1,3}),(?<two>\\d{1,3})\\)");
private static final Pattern DEACTIVATION_PATTERN = Pattern.compile("don't\\(\\)");
private static final Pattern ACTIVATION_PATTERN = Pattern.compile("do\\(\\)");

@Override
protected void runPartOne(final Scanner in) {
run(in);
run(in, false);
}

@Override
protected void runPartTwo(final Scanner in) {
run(in);
run(in, true);
}

private void run(final Scanner in) {
print("Whoopsie");
private void run(final Scanner in, final boolean enableDisable) {
final String input = readInput(in);

int index = 0;
final Matcher matcher = PATTERN.matcher(input);
final Matcher deactivationMatcher = DEACTIVATION_PATTERN.matcher(input);
final Matcher activeMatcher = ACTIVATION_PATTERN.matcher(input);

long sum = 0;
while (matcher.find(index)) {
if (enableDisable && deactivationMatcher.find(index)) {
if (deactivationMatcher.start() < matcher.start()) {
if (activeMatcher.find(deactivationMatcher.start())) {
index = activeMatcher.end();
continue;
}

break;
}
}

final long one = Long.parseLong(matcher.group("one"));
final long two = Long.parseLong(matcher.group("two"));
sum += one * two;

index = matcher.end();
}

print(sum);
}

private static String readInput(final Scanner in) {
final StringBuilder builder = new StringBuilder();

while (in.hasNextLine()) {
builder.append(in.nextLine());
}

final String input = builder.toString();
return input;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package org.ck.adventofcode.year2024;

import org.ck.adventofcode.util.BaseAOCTest;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@Disabled
class Day03Test extends BaseAOCTest {
@ParameterizedTest
@ValueSource(strings = {"01", "01a"})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
170778545

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
161
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
82868252

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
48
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))

0 comments on commit 64214f1

Please sign in to comment.