-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay3Part2.java
More file actions
53 lines (40 loc) · 1.47 KB
/
Copy pathDay3Part2.java
File metadata and controls
53 lines (40 loc) · 1.47 KB
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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Day3Part2 {
public static void main(String[] args) throws FileNotFoundException {
Pattern pattern = Pattern.compile("mul\\((\\d{1,3}),(\\d{1,3})\\)" +
"|(do\\(\\))" +
"|(don't\\(\\))");
Scanner input = new Scanner(new File("day3-input.txt"));
boolean ignore = false;
int answer = 0;
while (input.hasNextLine()) {
String line = input.nextLine();
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
String op = matcher.group(0);
switch (op) {
case "do()":
ignore = false;
break;
case "don't()":
ignore = true;
default: {
if (ignore) {
break;
}
String num1 = matcher.group(1);
String num2 = matcher.group(2);
int mulResult = Integer.parseInt(num1) * Integer.parseInt(num2);
System.out.println(op + " = " + mulResult);
answer += mulResult;
}
}
}
}
System.out.println(answer);
}
}