-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubgridsCounter.java
More file actions
69 lines (52 loc) · 1.79 KB
/
SubgridsCounter.java
File metadata and controls
69 lines (52 loc) · 1.79 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class SubgridsCounter {
static class Point {
public int x;
public int y;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
if (x != point.x) return false;
return y == point.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
}
public static int howMany(int[] x, int[] y) {
Set<Point> points = new HashSet();
for (int i=0; i < x.length; i++) {
Point p = new Point();
p.x = x[i];
p.y = y[i];
points.add(p);
}
int smallestDimension = Math.min(
Arrays.stream(x).max().getAsInt() - Arrays.stream(x).min().getAsInt(),
Arrays.stream(y).max().getAsInt() - Arrays.stream(y).min().getAsInt());
int maxGridSize = smallestDimension / 2;
Point other = new Point();
int gridsFound = 0;
for (Point topLeft : points) {
for (int gridSize = 1; gridSize <= maxGridSize; gridSize++) {
boolean isGrid = true;
for (int dx = 0; isGrid && dx < 3; dx++) {
for (int dy = 0; isGrid && dy < 3; dy++) {
other.x = topLeft.x + (dx * gridSize);
other.y = topLeft.y + (dy * gridSize);
isGrid &= points.contains(other);
}
}
if (isGrid) gridsFound++;
}
}
return gridsFound;
}
}