-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
73 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 52 additions & 10 deletions
62
adventofcode/src/main/java/org/ck/adventofcode/year2024/Day03.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
2 changes: 0 additions & 2 deletions
2
adventofcode/src/test/java/org/ck/adventofcode/year2024/Day03Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
170778545 |
6 changes: 6 additions & 0 deletions
6
adventofcode/src/test/resources/org/ck/adventofcode/year2024/day03/01.txt
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
161 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
82868252 |
6 changes: 6 additions & 0 deletions
6
adventofcode/src/test/resources/org/ck/adventofcode/year2024/day03/02.txt
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
48 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |