-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday08.java
More file actions
53 lines (52 loc) · 1.94 KB
/
Copy pathday08.java
File metadata and controls
53 lines (52 loc) · 1.94 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
import static java.lang.IO.println;
import static java.lang.Long.parseLong;
import static java.lang.System.in;
import static java.util.Comparator.comparingLong;
import static java.util.Comparator.reverseOrder;
void main() {
record Pos(long x, long y, long z) {}
record Pair(Pos a, Pos b) {
long dist() { return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z); }
}
var boxes = new BufferedReader(new InputStreamReader(in)).lines().map(l -> {
var p = l.split(",");
return new Pos(parseLong(p[0]), parseLong(p[1]), parseLong(p[2]));
}).toList();
var pairs = new ArrayList<Pair>();
for (int i = 0; i < boxes.size() - 1; i++) {
for (int j = i + 1; j < boxes.size(); j++) {
pairs.add(new Pair(boxes.get(i), boxes.get(j)));
}
}
pairs.sort(comparingLong(Pair::dist));
long n = 0, res1 = 0;
var circuits = new ArrayList<HashSet<Pos>>();
for (var p : pairs) {
var ca = circuits.stream().filter(c -> c.contains(p.a)).findFirst();
var cb = circuits.stream().filter(c -> c.contains(p.b)).findFirst();
if (ca.isEmpty()) {
if (cb.isEmpty()) {
circuits.add(new HashSet<>(List.of(p.a, p.b)));
} else {
cb.get().add(p.a);
}
} else {
if (cb.isEmpty()) {
ca.get().add(p.b);
} else if (ca.get() != cb.get()) {
ca.get().addAll(cb.get());
cb.get().clear();
}
}
circuits.removeIf(HashSet::isEmpty);
if (++n == 1000) {
var sizes = new ArrayList<>(circuits.stream().map(HashSet::size).toList());
sizes.sort(reverseOrder());
res1 = (long) sizes.get(0) * sizes.get(1) * sizes.get(2);
}
if (circuits.getFirst().size() == boxes.size()) {
println(res1 + " " + p.a.x * p.b.x);
break;
}
}
}