aboutsummaryrefslogtreecommitdiff
path: root/2021/day19/day19.cpp
blob: f45d7ca9bfe14932440e14e43829061b542653e3 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <iostream>
#include <fstream>
#include <string>
#include <vector>


class Beacon {
	public:
	Beacon() { x=y=z=0; }
	Beacon(int x, int y, int z): x(x), y(y), z(z) { }
	Beacon(const Beacon &b) { x=b.x; y=b.y; z=b.z; }

	int x,y,z;

	bool operator==(const Beacon& r) { 
		return x==r.x && y==r.y && z==r.z;
	}
	friend bool operator==(const Beacon& l, const Beacon& r) { 
		return l.x==r.x && l.y==r.y && l.z==r.z;
	}

	std::string show () {
		std::string rep = 
			std::to_string(x) + "," +
			std::to_string(y) + "," +
			std::to_string(z) + "\n";
		return rep;
	}
};

class Scanner {
	public:
	Scanner() { }
	Scanner(const Scanner &s) { 
		for (Beacon b : s.beacons) beacons.emplace_back(b);
	}

	int x,y,z;
	std::vector<Beacon> beacons;
	bool fixed = false;

	bool operator==(const Scanner& r) { 
		return x==r.x && y==r.y && z==r.z && beacons == r.beacons;
	}

	std::string show () {
		std::string rep = "scanner\n";
		for (Beacon b : beacons) {
			rep += "  beacon at " + b.show();
		}
		return rep;
	}

	Scanner scale (int x, int y, int z) {
		Scanner s;
		for (Beacon& b : beacons) {
			s.beacons.emplace_back(b.x*x, b.y*y, b.z*z);
		}
		return s;
	}

	Scanner rotate (int axis, int pi_halves) {
		Scanner s;
		int sin = pi_halves < 2 ? pi_halves : -pi_halves+2;
		pi_halves = (pi_halves+1) % 4;
		int cos = pi_halves < 2 ? pi_halves : -pi_halves+2;
		for (Beacon& b : beacons) {
			if (axis == 2)
				s.beacons.emplace_back(
					b.x*cos - b.y*sin, 
					b.x*sin + b.y*cos,
					b.z);
			if (axis == 1)
				s.beacons.emplace_back(
					b.x*cos + b.z*sin, 
					b.y,
					-b.x*sin + b.z*cos);
			if (axis == 0)
				s.beacons.emplace_back(
					b.x, 
					b.y*cos - b.z*sin,
					b.y*sin + b.z*cos);
		}
		return s;
	}

	Scanner rebase (bool swapxy, bool swapyz, bool swapxz) {
		Scanner s;
		int tmp;
		for (Beacon& b : beacons) {
			Beacon a { b };
			if (swapxy) { tmp = a.x; a.x = a.y; a.y = tmp; }
			if (swapyz) { tmp = a.y; a.y = a.z; a.z = tmp; }
			if (swapxz) { tmp = a.x; a.x = a.z; a.z = tmp; }
			s.beacons.push_back(a);
		}
		return s;
	}

	Scanner translate (int x, int y, int z) {
		Scanner s;
		for (Beacon& b : beacons) {
			s.beacons.emplace_back(b.x+x, b.y+y, b.z+z);
		}
		return s;
	}

	int overlap (Scanner oth) {
		int sum = 0;
		for (Beacon &b : beacons) {
			if (std::find(std::begin(oth.beacons), std::end(oth.beacons), b)
					!= std::end(oth.beacons)) {
				sum++;
			}
		}
		//std::cout << sum << " ";
		return sum;
	}
};


std::vector<Scanner> orientations (Scanner source) {
	std::vector<Scanner> orientations;
	for (int i=0; i<4; i++) {
		Scanner rotated = source.rotate(0, i);
		for (int j=0; j<6; j++) {
			Scanner based { rotated };
			if (j==1) {
				based = rotated.rotate(1, 1);
			}
			if (j==2) {
				based = rotated.rotate(1, 2);
			}
			if (j==3) {
				based = rotated.rotate(1, 3);
			}
			if (j==4) {
				based = rotated.rotate(2, 1);
			}
			if (j==5) {
				based = rotated.rotate(2, 3);
			}
			orientations.push_back(based);
		}
	}
	return orientations;
}

Beacon parse_beacon (std::string raw) {
	Beacon b;

	auto comma = raw.find(",");
	b.x = std::stoi(raw.substr(0, comma));
	raw = raw.substr(comma+1);

	comma = raw.find(",");
	b.y = std::stoi(raw.substr(0, comma));
	raw = raw.substr(comma+1);

	b.z = std::stoi(raw);
	
	return b;
}

std::vector<Scanner> parse_scanners (std::string raw) {
	std::vector<Scanner> scanners;
	while (raw.size() > 0) {
		auto newline = raw.find('\n');
		if (newline != std::string::npos) {
			std::string line = raw.substr(0, newline);
			raw = raw.substr(newline+1);
			if (line.find("---") != std::string::npos) {
				scanners.push_back(Scanner());
			} else if (line.size() > 1) {
				Beacon b = parse_beacon(line);
				scanners[scanners.size()-1].beacons.push_back(b);
			}
		} else {
			Beacon b = parse_beacon(raw);
			scanners[scanners.size()-1].beacons.push_back(b);
		}
	}
	return scanners;
}

int main (int argc, char *argv[]) {
	std::string raw; std::getline(std::ifstream(argv[1]), raw, '\0');
	
	std::vector<Scanner> scanners = parse_scanners(raw);

	std::vector<Beacon> cloud;
	for (Beacon b : scanners[0].beacons) {
		if (std::find(std::begin(cloud), std::end(cloud), b) 
				== std::end(cloud))
			cloud.push_back(b);
	}
	scanners[0].fixed = true;
	scanners[0].x = 0;
	scanners[0].y = 0;
	scanners[0].z = 0;

	while (true) {
		int fixed_count = 0;
		bool overlap = false;
		for (int i=0; i<scanners.size(); i++) {
			if (scanners[i].fixed) { 
				fixed_count ++;
				continue; 
			}
			for (int j=0; j<scanners.size(); j++) {
				if (i == j) continue;
				if (!scanners[j].fixed) continue;
				Scanner base = scanners[j];
				std::cout << "compare " << i << " against " << j << std::endl;
				auto rebaseds = orientations(scanners[i]);
				for (Scanner rebased : rebaseds) {
					for (Beacon as : base.beacons) {
						for (Beacon bas : rebased.beacons) {
							int dx = as.x - bas.x;
							int dy = as.y - bas.y;
							int dz = as.z - bas.z;
							int dist = std::abs(scanners[j].x-dx) 
								+ std::abs(scanners[j].y-dy)
							   	+ std::abs(scanners[j].z-dz);
							if (dist > 2000) continue;
							Scanner translated = rebased.translate(dx, dy, dz);
							if (base.overlap(translated) >= 12) {
								std::cout << "overlaps: " << base.overlap(translated) << std::endl;
								scanners[i].x = dx;
								scanners[i].y = dy;
								scanners[i].z = dz;
								std::cout << "  at: " <<dx<<" "<<dy<<" "<<dz << std::endl;
								std::cout << "  dist: " <<dist<< std::endl;
								scanners[i].beacons.clear();
								for (Beacon b : translated.beacons) {
									scanners[i].beacons.emplace_back(b);
									if (std::find(std::begin(cloud), std::end(cloud), b) 
											== std::end(cloud))
										cloud.push_back(b);
								}
								scanners[i].fixed = true;
								overlap = true;
								break;
							}
						}
						if (overlap) break;
					}
					if (overlap) break;
				}
				if (overlap) break;
			}
			if (overlap) break;
		}
		if (fixed_count == scanners.size()) break;
	}
	std::cout << "beacons: " << cloud.size() << std::endl;
	int maxdist = 0;
	for (int i=0; i<scanners.size(); i++) {
		for (int j=0; j<scanners.size(); j++) {
			if (i==j) continue;
			int dist = std::abs(scanners[i].x-scanners[j].x) + 
				std::abs(scanners[i].y-scanners[j].y) + 
				std::abs(scanners[i].z-scanners[j].z);
			maxdist = std::max(dist, maxdist);
		}
	}
	std::cout << "max dist: " << maxdist << std::endl;
	Scanner s;
	for (Beacon b : cloud) { s.beacons.push_back(b); }
	//std::cout << s.show() << std::endl;
	return 0;
}