-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart2.java
More file actions
48 lines (34 loc) · 1.29 KB
/
Copy pathPart2.java
File metadata and controls
48 lines (34 loc) · 1.29 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
package y2016.d09;
import common.Files;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Part2 {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<String> in = Files.readByLines("src/main/java/y2016/d09/in.txt");
String input = in.get(0);
System.out.println(count(input));
}
private static long count(String packed) {
long count = 0;
Pattern marker = Pattern.compile("^\\((\\d+)x(\\d+)\\)");
for (int i = 0; i < packed.length(); i++) {
if (packed.charAt(i) != '(') {
++count;
continue;
}
if (packed.substring(i).matches("^\\((\\d+)x(\\d+)\\).*")) {
Matcher m = marker.matcher(packed.substring(i, Math.min(packed.length(), i + 100)));
m.find();
int length = Integer.parseInt(m.group(1));
int times = Integer.parseInt(m.group(2));
i += 3 + m.group(1).length() + m.group(2).length();
String clone = packed.substring(i, i+length);
count += times * count(clone);
i += length-1;
}
}
return count;
}
}