-
Notifications
You must be signed in to change notification settings - Fork 0
/
day03.java
28 lines (26 loc) · 994 Bytes
/
day03.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
import java.io.*;
import java.util.*;
import java.util.regex.*;
import static java.lang.Integer.parseInt;
import static java.lang.System.in;
import static java.lang.System.out;
public class day03 {
public static void main(String[] args) {
var cmd = Pattern.compile("mul\\((\\d+),(\\d+)\\)|do\\(\\)|don't\\(\\)");
var muls = new int[]{0, 0};
var _do = new boolean[]{true};
new BufferedReader(new InputStreamReader(in)).lines().forEach(line -> {
new Scanner(line).findAll(cmd).forEach(match -> {
if (match.group(0).startsWith("mul")) {
var n1 = parseInt(match.group(1));
var n2 = parseInt(match.group(2));
muls[0] += n1 * n2;
if (_do[0]) { muls[1] += n1 * n2; }
} else {
_do[0] = "do()".equals(match.group(0));
}
});
});
out.printf("%d %d\n", muls[0], muls[1]);
}
}