-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2024_03.java
84 lines (69 loc) · 2.42 KB
/
AoC2024_03.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import static java.lang.Integer.parseInt;
import static java.util.stream.Collectors.joining;
import java.util.List;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.github.pareronia.aoc.solution.Sample;
import com.github.pareronia.aoc.solution.Samples;
import com.github.pareronia.aoc.solution.SolutionBase;
public final class AoC2024_03
extends SolutionBase<String, Integer, Integer> {
private final Pattern REGEX = Pattern.compile
("(do(n't)?)\\(\\)|mul\\((\\d{1,3}),(\\d{1,3})\\)");
private AoC2024_03(final boolean debug) {
super(debug);
}
public static AoC2024_03 create() {
return new AoC2024_03(false);
}
public static AoC2024_03 createDebug() {
return new AoC2024_03(true);
}
@Override
protected String parseInput(final List<String> inputs) {
return inputs.stream().collect(joining());
}
private int solve(final String input, final boolean useConditionals) {
int ans = 0;
boolean enabled = true;
final Matcher matcher = REGEX.matcher(input);
while (matcher.find()) {
final MatchResult m = matcher.toMatchResult();
if (m.group(0).equals("do()")) {
enabled = true;
} else if (m.group(0).equals("don't()")) {
enabled = false;
} else {
if (!useConditionals || enabled) {
ans += parseInt(m.group(3)) * parseInt(m.group(4));
}
}
}
return ans;
}
@Override
public Integer solvePart1(final String input) {
return solve(input, false);
}
@Override
public Integer solvePart2(final String input) {
return solve(input, true);
}
@Override
@Samples({
@Sample(method = "part1", input = TEST1, expected = "161"),
@Sample(method = "part2", input = TEST2, expected = "48"),
})
public void samples() {
}
public static void main(final String[] args) throws Exception {
AoC2024_03.create().run();
}
private static final String TEST1 = """
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
""";
private static final String TEST2 = """
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
""";
}