-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay3Part1.java
More file actions
33 lines (25 loc) · 982 Bytes
/
Copy pathDay3Part1.java
File metadata and controls
33 lines (25 loc) · 982 Bytes
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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Day3Part1 {
public static void main(String[] args) throws FileNotFoundException {
Pattern pattern = Pattern.compile("mul\\((\\d{1,3}),(\\d{1,3})\\)");
Scanner input = new Scanner(new File("day3-input.txt"));
int answer = 0;
while (input.hasNextLine()) {
String line = input.nextLine();
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
String op = matcher.group(0);
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);
}
}