-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd5.java
66 lines (60 loc) · 2.37 KB
/
d5.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
62
63
64
65
66
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.Pattern;
import static java.lang.Integer.parseInt;
import static java.lang.System.out;
import static java.util.Arrays.stream;
public class d5 {
public static void main(String[] args) {
var reader = new BufferedReader(new InputStreamReader(System.in));
var num = Pattern.compile("(\\d+),(\\d+)\\s\\->\\s(\\d+),(\\d+)");
var board = new Board();
var board2 = new Board();
reader.lines().forEach(line -> {
var matcher = num.matcher(line);
if (!matcher.matches()) {
throw new RuntimeException("invalid input " + line);
}
int x1 = parseInt(matcher.group(1)), y1 = parseInt(matcher.group(2)),
x2 = parseInt(matcher.group(3)), y2 = parseInt(matcher.group(4));
if (x1 == x2 || y1 == y2) {
board.mark(x1, y1, x2, y2);
board2.mark(x1, y1, x2, y2);
}
if (Math.abs(x1 - x2) == Math.abs(y1 - y2)) {
board2.mark(x1, y1, x2, y2);
}
});
out.println(board.points());
out.println(board2.points());
}
static class Board {
static final int CAPACITY = 1000;
final int[] numbers;
Board() {
this.numbers = new int[CAPACITY * CAPACITY];
}
void mark(int x1, int y1, int x2, int y2) {
if (x1 == x2) {
for (int y = Math.min(y1, y2); y <= Math.max(y1, y2); y++) {
this.numbers[y * CAPACITY + x1]++;
}
} else if (y1 == y2) {
for (int x = Math.min(x1, x2); x <= Math.max(x1, x2); x++) {
this.numbers[y1 * CAPACITY + x]++;
}
} else if (x1 - x2 == y1 - y2) {
for (int x = Math.min(x1, x2); x <= Math.max(x1, x2); x++) {
this.numbers[(Math.min(y1, y2) + (x - Math.min(x1, x2))) * CAPACITY + x]++;
}
} else if (x1 - x2 == y2 - y1) {
for (int x = Math.min(x1, x2); x <= Math.max(x1, x2); x++) {
this.numbers[(Math.max(y1, y2) - (x - Math.min(x1, x2))) * CAPACITY + x]++;
}
}
}
int points() {
return (int) stream(this.numbers).filter(v -> v > 1).count();
}
}
}