-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd19.java
154 lines (138 loc) · 5.84 KB
/
d19.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static java.lang.Integer.parseInt;
import static java.lang.System.out;
public class d19 {
public static void main(String[] args) throws IOException {
var reader = new BufferedReader(new InputStreamReader(System.in));
var scanners = new ArrayList<BeaconsScanner>();
while (reader.ready()) {
scanners.add(BeaconsScanner.parse(reader));
}
var unique = new HashSet<>(scanners.remove(0).beacons);
var positions = new Position[scanners.size()];
var processed = new int[scanners.size()];
Arrays.fill(processed, 0);
while (Arrays.stream(processed).filter(p -> p > 0).count() < scanners.size()) {
for (var s : scanners) {
if (processed[s.n - 1] > 0) {
continue;
}
Optional<Position> pos = matchAll(unique, s);
if (pos.isPresent()) {
processed[s.n - 1] = 1;
positions[s.n - 1] = pos.get();
}
}
out.printf("processed %d of %d, unique beacons %d\n",
Arrays.stream(processed).filter(p -> p > 0).count(),
scanners.size(), unique.size());
}
out.println(unique.size());
var distances = new ArrayList<Integer>();
for (int i = 0; i < scanners.size(); ++i) {
for (int j = i+1; j < scanners.size(); ++j) {
distances.add(positions[i].manhattan(positions[j]));
}
}
out.println(distances.stream().max(Integer::compareTo).get());
}
static Optional<Position> matchAll(Set<Position> unique, BeaconsScanner scanner) {
for (int rot = 0; rot < 4; rot++) {
for (int dir = 0; dir < 6; dir++) {
int _rot = rot;
int _dir = dir;
var s = scanner.transform(p -> p.rotate(_rot).direct(_dir));
Optional<Position> pos = match(unique, s);
if (pos.isPresent()) {
return pos;
}
}
}
return Optional.empty();
}
static Optional<Position> match(Set<Position> beacons, BeaconsScanner scanner) {
for (Position beacon : beacons) {
for (Position p : scanner.beacons) {
var mapped = scanner.beacons.stream().map(b -> b.move(beacon.x - p.x, beacon.y - p.y, beacon.z - p.z)).toList();
if (mapped.stream().filter(beacons::contains).count() >= 12) {
beacons.addAll(mapped);
return Optional.of(new Position(beacon.x - p.x, beacon.y - p.y, beacon.z - p.z));
}
}
}
return Optional.empty();
}
static class BeaconsScanner {
private static final Pattern num = Pattern.compile("\\-?\\d+");
final int n;
final List<Position> beacons;
BeaconsScanner(int n, List<Position> beacons) {
this.n = n;
this.beacons = beacons;
}
static BeaconsScanner parse(BufferedReader reader) throws IOException {
var s = reader.readLine();
if (s.startsWith("---")) {
Matcher m = num.matcher(s);
m.find();
var n = parseInt(m.group());
var beacons = new ArrayList<Position>();
s = reader.readLine();
while (s != null && !s.isBlank()) {
beacons.add(Position.parse(s));
s = reader.readLine();
}
return new BeaconsScanner(n, beacons);
}
throw new RuntimeException("invalid input \"" + s + "\"");
}
BeaconsScanner transform(Function<Position, Position> f) {
return new BeaconsScanner(this.n, this.beacons.stream().map(f).collect(Collectors.toList()));
}
}
static class Position {
final int x, y, z;
Position(int x, int y, int z) {
this.x = x; this.y = y; this.z = z;
}
static Position parse(String s) {
var triple = s.split(",");
return new Position(parseInt(triple[0]), parseInt(triple[1]), parseInt(triple[2]));
}
Position move(int dx, int dy, int dz) {
return new Position(this.x + dx, this.y + dy, this.z + dz);
}
Position rotate(int times) {
switch (times) {
case 0: return this;
case 1: return new Position(-this.y, this.x, this.z);
case 2: return new Position(-this.x, -this.y, this.z);
case 3: return new Position(this.y, -this.x, this.z);
}
throw new RuntimeException("incorrect rotation " + times);
}
Position direct(int to) {
switch (to) {
case 0: return this;
case 1: return new Position(this.x, -this.y, -this.z);
case 2: return new Position(this.x, -this.z, this.y);
case 3: return new Position(-this.y, -this.z, this.x);
case 4: return new Position(-this.x, -this.z, -this.y);
case 5: return new Position(this.y, -this.z, -this.x);
}
throw new RuntimeException("incorrect direction " + to);
}
int manhattan(Position other) {
return Math.abs(this.x - other.x) + Math.abs(this.y - other.y) + Math.abs(this.z - other.z);
}
public boolean equals(Object other) { return this.x == ((Position) other).x && this.y == ((Position) other).y && this.z == ((Position) other).z; }
public int hashCode() { return Objects.hash(x, y, z); }
}
}