-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd13.java
61 lines (55 loc) · 2.14 KB
/
d13.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
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
54
55
56
57
58
59
60
61
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static java.lang.Integer.parseInt;
import static java.lang.System.out;
public class d13 {
public static void main(String[] args) {
var reader = new BufferedReader(new InputStreamReader(System.in));
var dots = new HashSet<Dot>();
var foldings = new ArrayList<Folding>();
reader.lines().forEach(line -> {
if (line.startsWith("fold"))
foldings.add(new Folding(line));
else if (!line.isBlank())
dots.add(Dot.parse(line));
});
out.println(dots.stream().map(d -> d.fold(foldings.get(0))).collect(Collectors.toSet()).size());
Set<Dot> result = new HashSet<>(dots);
for (var f : foldings)
result = result.stream().map(d -> d.fold(f)).collect(Collectors.toSet());
for (int y = 0; y < result.stream().mapToInt(d -> d.y).max().getAsInt() + 1; y++) {
for (int x = 0; x < result.stream().mapToInt(d -> d.x).max().getAsInt() + 1; x++)
out.print(result.contains(new Dot(x, y)) ? "#" : ".");
out.println();
}
}
record Dot (int x, int y) {
static Dot parse(String s) {
var pair = s.split(",");
return new Dot(parseInt(pair[0]), parseInt(pair[1]));
}
Dot fold(Folding f) {
if (f.axis.equals("x"))
return x > f.pos ? new Dot(f.pos - (x - f.pos), y) : this;
else
return y > f.pos ? new Dot(x, f.pos - (y - f.pos)) : this;
}
}
static class Folding {
static Pattern foldPtn = Pattern.compile("fold along (x|y)\\=(\\d+)");
final String axis;
final int pos;
Folding(String line) {
var matcher = foldPtn.matcher(line);
if (!matcher.matches())
throw new RuntimeException("invalid input " + line);
this.axis = matcher.group(1);
this.pos = parseInt(matcher.group(2));
}
}
}