-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay3.java
41 lines (35 loc) · 1.11 KB
/
Day3.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
import com.horstmann.adventofcode.*;
String input;
void parse(Path path) throws IOException {
input = Files.readString(path);
}
Object part1() {
Pattern mul = Pattern.compile("mul\\(([0-9]+),([0-9]+)\\)");
return mul.matcher(input).results().mapToInt(r -> Integer.parseInt(r.group(1)) * Integer.parseInt(r.group(2))).sum();
}
Object part2() {
Pattern mul = Pattern.compile("mul\\(([0-9]+),([0-9]+)\\)|do\\(\\)|don't\\(\\)");
var results = mul.matcher(input).results().toList();
long s = 0;
boolean enabled = true;
for (var r : results) {
if (r.group().equals("do()"))
enabled = true;
else if (r.group().equals("don't()"))
enabled = false;
else if (enabled)
s += Integer.parseInt(r.group(1)) * Integer.parseInt(r.group(2));
}
return s;
}
void main() throws IOException {
Util.time(() -> {
parse(Util.inputPath("a"));
IO.println(part1());
parse(Util.inputPath("b"));
IO.println(part2());
parse(Util.inputPath("z"));
IO.println(part1());
IO.println(part2());
});
}