Repository navigation
Expand file tree
/
Copy pathComponent.hpp
More file actions
591 lines (477 loc) · 18.2 KB
/
Copy pathComponent.hpp
File metadata and controls
591 lines (477 loc) · 18.2 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#pragma once
#include <string>
#include <vector>
#include <tuple>
#include <stdexcept>
#include <sstream>
#include <unordered_map>
#include "LifeAPI.hpp"
#include "LifeHistory.hpp"
#include "Apgcode.hpp"
#include "Symmetry.hpp"
class GliderSet {
public:
LifeState se;
LifeState sw;
LifeState nw;
LifeState ne;
GliderSet() = default;
inline GliderSet(const LifeState& se, const LifeState& sw, const LifeState& nw, const LifeState& ne);
// TODO: It is dumb to be recomputing this each time
unsigned Cost() const { return (se.GetPop() + sw.GetPop() + nw.GetPop() + ne.GetPop())/5; }
inline GliderSet operator|(const GliderSet& other) const;
inline bool operator==(const GliderSet& other) const;
inline uint64_t GetHash() const;
inline LifeState Realise() const;
inline GliderSet Stepped(int n) const;
inline GliderSet Rewind(int generations) const;
inline GliderSet Transformed(SymmetryTransform t) const;
inline GliderSet Moved(std::pair<int, int> p) const;
// Try to shift glider set to fit in 64x64 torus without wrapping
inline std::pair<int, int> OffsetToFitTorus() const;
static inline GliderSet FromSJK(const std::string& gliderData, unsigned rewind = 4);
inline std::string ToSJK() const;
};
struct Component {
LifeState base;
GliderSet gliderSet;
LifeState out;
inline unsigned Cost() const { return gliderSet.Cost(); }
inline bool SanityCheck() const;
inline Component Moved(std::pair<int, int> p) const;
inline void ShiftToFitTorus();
inline LifeState Realise() const;
inline static Component FromSJK(const std::string& compStr);
inline std::string ToSJK() const;
inline std::string RLE() const;
inline static std::tuple<std::string, std::string, std::string> SplitSJKLine(const std::string& compStr);
inline static std::tuple<std::string, unsigned, std::string> SplitSJKLineCost(const std::string& compStr);
inline static std::pair<std::string, int> ParseApgcodeWithPhase(const std::string& inData);
inline static LifeState BaseGlider(int steps);
};
GliderSet::GliderSet(const LifeState& se, const LifeState& sw, const LifeState& nw, const LifeState& ne)
: se(se), sw(sw), nw(nw), ne(ne) {
}
GliderSet GliderSet::operator|(const GliderSet& other) const {
return GliderSet(se | other.se, sw | other.sw, nw | other.nw, ne | other.ne);
}
bool GliderSet::operator==(const GliderSet& other) const {
return se == other.se && sw == other.sw && nw == other.nw && ne == other.ne;
}
uint64_t GliderSet::GetHash() const {
uint64_t hash = se.GetHash();
hash = combine_hashes(hash, sw.GetHash());
hash = combine_hashes(hash, nw.GetHash());
hash = combine_hashes(hash, ne.GetHash());
return hash;
}
LifeState GliderSet::Realise() const {
return se | sw | nw | ne;
}
GliderSet GliderSet::Stepped(int n) const {
return GliderSet(se.Stepped(n), sw.Stepped(n), nw.Stepped(n), ne.Stepped(n));
}
GliderSet GliderSet::Rewind(int n) const {
int offset = (n + 3) / 4; // ceil(generations/4)
int forwardStep = 0;
if (n % 4 != 0) {
forwardStep = 4 - (n % 4);
}
LifeState rewoundSE = se.Moved(-offset, -offset);
LifeState rewoundSW = sw.Moved(offset, -offset);
LifeState rewoundNW = nw.Moved(offset, offset);
LifeState rewoundNE = ne.Moved(-offset, offset);
if (forwardStep > 0) {
rewoundSE = rewoundSE.Stepped(forwardStep);
rewoundSW = rewoundSW.Stepped(forwardStep);
rewoundNW = rewoundNW.Stepped(forwardStep);
rewoundNE = rewoundNE.Stepped(forwardStep);
}
return GliderSet(rewoundSE, rewoundSW, rewoundNW, rewoundNE);
}
GliderSet GliderSet::Transformed(SymmetryTransform t) const {
LifeState tSe = se.Transformed(t);
LifeState tSw = sw.Transformed(t);
LifeState tNw = nw.Transformed(t);
LifeState tNe = ne.Transformed(t);
switch(t) {
case SymmetryTransform::Identity:
return GliderSet(tSe, tSw, tNw, tNe);
case SymmetryTransform::Rotate90:
case SymmetryTransform::Rotate90Even:
return GliderSet(tNe, tSe, tSw, tNw);
case SymmetryTransform::Rotate180OddBoth:
case SymmetryTransform::Rotate180EvenHorizontal:
case SymmetryTransform::Rotate180EvenVertical:
case SymmetryTransform::Rotate180EvenBoth:
return GliderSet(tNw, tNe, tSe, tSw);
case SymmetryTransform::Rotate270:
case SymmetryTransform::Rotate270Even:
return GliderSet(tSw, tNw, tNe, tSe);
case SymmetryTransform::ReflectAcrossX:
case SymmetryTransform::ReflectAcrossXEven:
return GliderSet(tNe, tNw, tSw, tSe);
case SymmetryTransform::ReflectAcrossY:
case SymmetryTransform::ReflectAcrossYEven:
return GliderSet(tSw, tSe, tNe, tNw);
case SymmetryTransform::ReflectAcrossYeqX:
return GliderSet(tSe, tNe, tNw, tSw);
case SymmetryTransform::ReflectAcrossYeqNegX:
case SymmetryTransform::ReflectAcrossYeqNegXP1:
return GliderSet(tNw, tSw, tSe, tNe);
}
}
GliderSet GliderSet::Moved(std::pair<int, int> p) const {
return {
se.Moved(p), sw.Moved(p), nw.Moved(p), ne.Moved(p),
};
}
std::pair<int, int> GliderSet::OffsetToFitTorus() const {
const int DANGER_ZONE = 16;
LifeState south = se | sw;
LifeState north = nw | ne;
LifeState east = se | ne;
LifeState west = sw | nw;
LifeState southZone = LifeState::SolidRect(-32, 32 - DANGER_ZONE, 64, DANGER_ZONE);
LifeState northZone = LifeState::SolidRect(-32, -32, 64, DANGER_ZONE);
LifeState eastZone = LifeState::SolidRect(32 - DANGER_ZONE, -32, DANGER_ZONE, 64);
LifeState westZone = LifeState::SolidRect(-32, -32, DANGER_ZONE, 64);
bool southProblematic = !(south & southZone).IsEmpty() && (north & southZone).IsEmpty();
bool northProblematic = !(north & northZone).IsEmpty() && (south & northZone).IsEmpty();
bool eastProblematic = !(east & eastZone).IsEmpty() && (west & eastZone).IsEmpty();
bool westProblematic = !(west & westZone).IsEmpty() && (east & westZone).IsEmpty();
int shiftX = 0;
int shiftY = 0;
if (eastProblematic && !westProblematic)
shiftX = DANGER_ZONE;
if (!eastProblematic && westProblematic)
shiftX = -DANGER_ZONE;
if (southProblematic && !northProblematic)
shiftY = DANGER_ZONE;
if (!southProblematic && northProblematic)
shiftY = -DANGER_ZONE;
return {shiftX, shiftY};
}
GliderSet GliderSet::FromSJK(const std::string& gliderData, unsigned rewind) {
// Map orientation codes to SymmetryTransform
static const std::unordered_map<char, SymmetryTransform> orientationMap = {
{'F', SymmetryTransform::Identity},
{'L', SymmetryTransform::Rotate270},
{'B', SymmetryTransform::Rotate180OddBoth},
{'R', SymmetryTransform::Rotate90},
{'f', SymmetryTransform::ReflectAcrossX},
{'l', SymmetryTransform::ReflectAcrossYeqNegXP1},
{'b', SymmetryTransform::ReflectAcrossY},
{'r', SymmetryTransform::ReflectAcrossYeqX}
};
// Parse fields and transformation
size_t atPos = gliderData.find('@');
std::string fields = (atPos != std::string::npos) ? gliderData.substr(0, atPos) : gliderData;
std::string transStr = (atPos != std::string::npos) ? gliderData.substr(atPos + 1) : "";
// Process transformation
int t = 0;
SymmetryTransform orientation = SymmetryTransform::Identity;
int shiftX = 0, shiftY = 0;
if (!transStr.empty()) {
// Find orientation code
for (char c : "FLBRflbr") {
size_t pos = transStr.find(c);
if (pos != std::string::npos) {
t = std::stoi(transStr.substr(0, pos));
orientation = orientationMap.at(c);
// Parse shift values
std::string shiftPart = transStr.substr(pos + 1);
std::istringstream iss(shiftPart);
iss >> shiftX >> shiftY;
break;
}
}
}
// Parse fields and create salvos
std::vector<LifeState> salvos(4);
std::istringstream fieldStream(fields);
std::string field;
int salvoIndex = 0;
while (std::getline(fieldStream, field, '/') && salvoIndex < 4) {
LifeState salvo;
std::istringstream timeStream(field);
std::string timeStr, laneStr;
// Parse time/lane pairs
while (timeStream >> timeStr >> laneStr) {
int time = std::stoi(timeStr);
int lane = std::stoi(laneStr);
// Create glider at position and advance by time
salvo |= Component::BaseGlider(-time - t - 4 - rewind).Moved(0, -lane);
}
// Apply rotations based on salvo index (SE, SW, NW, NE)
if (salvoIndex == 1) {
salvo.Transform(SymmetryTransform::Rotate90);
} else if (salvoIndex == 2) {
salvo.Transform(SymmetryTransform::Rotate180OddBoth);
} else if (salvoIndex == 3) {
salvo.Transform(SymmetryTransform::Rotate270);
}
salvos[salvoIndex] = salvo;
salvoIndex++;
}
// Create glider set and apply transformation
GliderSet result(salvos[0], salvos[1], salvos[2], salvos[3]);
return result.Transformed(orientation).Moved({shiftX, shiftY});
}
LifeState Component::Realise() const {
return base | gliderSet.Realise();
}
// TODO: This could be better
bool Component::SanityCheck() const {
if (Realise().Stepped(200) != out)
return false;
// Rewind in steps of 16 until all salvos are outside the base pattern bounding box
auto [minX, minY, maxX, maxY] = base.XYBounds();
GliderSet rewoundGliders = gliderSet;
int totalRewind = 0;
const int rewindStep = 16;
const int maxRewindSteps = 20; // Safety limit
for (int step = 0; step < maxRewindSteps; step++) {
// Check that each individual salvo is outside the base pattern bounding box
bool allSalvosOutside = true;
for (const LifeState& salvo : {rewoundGliders.se, rewoundGliders.sw, rewoundGliders.nw, rewoundGliders.ne}) {
if (!salvo.IsEmpty()) {
auto [sMinX, sMinY, sMaxX, sMaxY] = salvo.XYBounds();
if (!(sMaxX < minX || sMinX > maxX || sMaxY < minY || sMinY > maxY)) {
allSalvosOutside = false;
break;
}
}
}
if (allSalvosOutside) {
break;
}
rewoundGliders = rewoundGliders.Rewind(rewindStep);
totalRewind += rewindStep;
}
Component rewoundComp;
rewoundComp.base = base;
rewoundComp.gliderSet = rewoundGliders;
rewoundComp.out = out;
if (rewoundComp.Realise().Stepped(totalRewind) != Realise()) {
return false;
}
// Check that each salvo can get there without colliding with the base
const int minDistance = 1; // Minimum gap between gliders and base
// SE gliders should be north or west of base
if (!rewoundGliders.se.IsEmpty()) {
unsigned seRewind = 0;
LifeState startingSE = rewoundGliders.se;
for (int step = 0; step < maxRewindSteps; step++) {
auto [sMinX, sMinY, sMaxX, sMaxY] = rewoundGliders.se.XYBounds();
if (sMaxY < minY - minDistance || sMaxX < minX - minDistance) {
break;
}
rewoundGliders.se = rewoundGliders.Rewind(rewindStep).se;
seRewind += rewindStep;
}
if (seRewind > 0 && (rewoundGliders.se | base).Stepped(seRewind) != (startingSE | base))
return false;
}
// SW gliders should be north or east of base
if (!rewoundGliders.sw.IsEmpty()) {
unsigned swRewind = 0;
LifeState startingSW = rewoundGliders.sw;
for (int step = 0; step < maxRewindSteps; step++) {
auto [sMinX, sMinY, sMaxX, sMaxY] = rewoundGliders.sw.XYBounds();
if (sMaxY < minY - minDistance || sMinX > maxX + minDistance) {
break;
}
rewoundGliders.sw = rewoundGliders.Rewind(rewindStep).sw;
swRewind += rewindStep;
}
if (swRewind > 0 && (rewoundGliders.sw | base).Stepped(swRewind) != (startingSW | base))
return false;
}
// NW gliders should be south or east of base
if (!rewoundGliders.nw.IsEmpty()) {
unsigned nwRewind = 0;
LifeState startingNW = rewoundGliders.nw;
for (int step = 0; step < maxRewindSteps; step++) {
auto [sMinX, sMinY, sMaxX, sMaxY] = rewoundGliders.nw.XYBounds();
if (sMinY > maxY + minDistance || sMinX > maxX + minDistance) {
break;
}
rewoundGliders.nw = rewoundGliders.Rewind(rewindStep).nw;
nwRewind += rewindStep;
}
if (nwRewind > 0 && (rewoundGliders.nw | base).Stepped(nwRewind) != (startingNW | base))
return false;
}
// NE gliders should be south or west of base
if (!rewoundGliders.ne.IsEmpty()) {
unsigned neRewind = 0;
LifeState startingNE = rewoundGliders.ne;
for (int step = 0; step < maxRewindSteps; step++) {
auto [sMinX, sMinY, sMaxX, sMaxY] = rewoundGliders.ne.XYBounds();
if (sMinY > maxY + minDistance || sMaxX < minX - minDistance) {
break;
}
rewoundGliders.ne = rewoundGliders.Rewind(rewindStep).ne;
neRewind += rewindStep;
}
if (neRewind > 0 && (rewoundGliders.ne | base).Stepped(neRewind) != (startingNE | base))
return false;
}
// TODO: Torus wrap?
return true;
}
Component Component::Moved(std::pair<int, int> p) const {
return {
base.Moved(p),
gliderSet.Moved(p),
out.Moved(p),
};
}
void Component::ShiftToFitTorus() {
std::array<int, 4> bounds = base.XYBounds();
if (base.IsEmpty())
bounds = out.XYBounds();
*this = Moved({-bounds[0], -bounds[1]});
auto offset = gliderSet.OffsetToFitTorus();
*this = Moved(offset);
}
std::tuple<std::string, std::string, std::string> Component::SplitSJKLine(const std::string& compStr) {
size_t firstPos = compStr.find('>');
size_t secondPos = compStr.find('>', firstPos + 1);
if (firstPos == std::string::npos || secondPos == std::string::npos) {
throw std::invalid_argument("Invalid component string format");
}
std::string inData = compStr.substr(0, firstPos);
std::string gliderData = compStr.substr(firstPos + 1, secondPos - firstPos - 1);
std::string outData = compStr.substr(secondPos + 1);
return std::make_tuple(inData, gliderData, outData);
}
std::tuple<std::string, unsigned, std::string> Component::SplitSJKLineCost(const std::string& line) {
auto [inData, gliderData, outData] = SplitSJKLine(line);
if (outData == "xs0_0") outData = "";
// Extract apgcode (before '+' if present)
std::string apgcode = inData;
size_t plusPos = inData.find('+');
if (plusPos != std::string::npos) {
apgcode = inData.substr(0, plusPos);
}
// Count gliders in glider data
std::string fields = gliderData;
size_t atPos = gliderData.find('@');
if (atPos != std::string::npos) {
fields = gliderData.substr(0, atPos);
}
int nGliders = 0;
std::stringstream fieldStream(fields);
std::string field;
while (std::getline(fieldStream, field, '/')) {
std::stringstream tokens(field);
std::string token;
int tokenCount = 0;
while (tokens >> token) {
tokenCount++;
}
nGliders += tokenCount / 2;
}
return std::make_tuple(apgcode, nGliders, outData);
}
std::pair<std::string, int> Component::ParseApgcodeWithPhase(const std::string& inData) {
size_t plusPos = inData.find('+');
if (plusPos == std::string::npos) {
return std::make_pair(inData, 0);
}
std::string apgcode = inData.substr(0, plusPos);
std::string phaseStr = inData.substr(plusPos + 1);
int phase = 0;
if (!phaseStr.empty()) {
try {
phase = std::stoi(phaseStr);
} catch (const std::exception&) {
phase = 0;
}
}
return std::make_pair(apgcode, phase);
}
LifeState Component::BaseGlider(int steps) {
static const std::array<LifeState, 4> gliderPhases = {
LifeState::ConstantParse("bo$2bo$3o!"),
LifeState::ConstantParse("obo$b2o$bo!").Moved(0, 1),
LifeState::ConstantParse("2bo$obo$b2o!").Moved(0, 1),
LifeState::ConstantParse("o$b2o$2o!").Moved(1, 1)
};
return gliderPhases[steps & 0b11].Moved(steps >> 2, steps >> 2);
}
Component Component::FromSJK(const std::string& compStr) {
auto [inData, gliderData, outData] = SplitSJKLine(compStr);
auto [apgcode, phase] = ParseApgcodeWithPhase(inData);
Component result;
result.base = LifeState::DecodeApgcode(apgcode);
if (phase > 0) {
result.base = result.base.Stepped(phase);
}
result.gliderSet = GliderSet::FromSJK(gliderData, 2);
// We can't use the provided Apgcode for the output because we don't
// know what orientation and offset to place it at, so we just have
// to run the step
result.out = result.base | result.gliderSet.Realise();
unsigned gen = 0;
bool done = false;
while (!done) {
LifeState prev = result.out;
result.out.Step();
gen++;
if (result.out == prev)
done = true;
if (gen > 200)
throw std::runtime_error("Component " + (result.base | result.gliderSet.Realise()).RLE() + " took too long");
}
// Double-check that we got the right output
// (Torus wrap means we might not)
if (result.out.EncodeApgcode() != outData)
throw std::runtime_error("Component " + (result.base | result.gliderSet.Realise()).RLE() + " gave the wrong result");
return result;
}
std::string GliderSet::ToSJK() const {
std::vector<std::vector<std::pair<int, int>>> pairs(4);
LifeState salvos[4] = {se, sw.Transformed(SymmetryTransform::Rotate90),
nw.Transformed(SymmetryTransform::Rotate180OddBoth),
ne.Transformed(SymmetryTransform::Rotate270)};
for (int dir = 0; dir < 4; dir++) {
LifeState salvo = salvos[dir];
// Find gliders in each phase
for (int phase = 0; phase < 4; phase++) {
LifeState glider = Component::BaseGlider(phase);
LifeState matches = salvo.MatchLive(glider);
auto coords = matches.OnCells();
for (auto [x, y] : coords) {
int time = 1 - 4 * (x + 1) - (phase + 1) % 4;
int lane = x - y + (phase > 0 && phase < 3 ? 1 : 0);
pairs[dir].push_back({time, lane});
}
}
std::sort(pairs[dir].begin(), pairs[dir].end());
}
// Convert to string format
std::string result;
for (int dir = 0; dir < 4; dir++) {
if (dir > 0) result += "/";
for (size_t i = 0; i < pairs[dir].size(); i++) {
if (i > 0) result += " ";
result += std::to_string(pairs[dir][i].first) + " " + std::to_string(pairs[dir][i].second);
}
}
return result;
}
std::string Component::ToSJK() const {
std::string baseApgcode = base.EncodeApgcode();
std::string gliderData = gliderSet.ToSJK();
std::string outApgcode = out.EncodeApgcode();
if (outApgcode == "xs0_0") outApgcode = "";
return baseApgcode + ">" + gliderData + ">" + outApgcode;
}
std::string Component::RLE() const {
LifeState original = base & out;
LifeHistory history(base | gliderSet.Realise(), LifeState(), LifeState(), original);
return history.RLEWHeader();
}