-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart1.java
More file actions
53 lines (41 loc) · 1.56 KB
/
Copy pathPart1.java
File metadata and controls
53 lines (41 loc) · 1.56 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
package y2016.d09;
import common.Files;
import java.io.FileNotFoundException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Part1 {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<String> in = Files.readByLines("src/main/java/y2016/d09/in.txt");
for (String packed: in) {
System.out.print(packed + " " + packed.length() + " ");
System.out.println(unpack(packed));
}
}
private static int unpack(String packed) {
StringBuilder unpacked = new StringBuilder();
Pattern marker = Pattern.compile("^\\((\\d+)x(\\d+)\\)");
for (int i = 0; i < packed.length(); i++) {
if (packed.charAt(i) != '(') {
unpacked.append(packed.charAt(i));
continue;
}
if (packed.substring(i).matches("^\\((\\d+)x(\\d+)\\).*")) {
Matcher m = marker.matcher(packed.substring(i));
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);
for (int c = 0; c < times; c++) {
unpacked.append(clone);
}
i += length-1;
}
}
System.out.print(unpacked + " ");
return unpacked.length();
}
}