Repository navigation
Expand file tree
/
Copy pathApgcode.hpp
More file actions
267 lines (216 loc) · 7.13 KB
/
Copy pathApgcode.hpp
File metadata and controls
267 lines (216 loc) · 7.13 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
#pragma once
#include "LifeAPI.hpp"
#include "Symmetry.hpp"
#include <string>
#include <vector>
#include <array>
#include <algorithm>
namespace Apgcode {
class Encoder {
private:
static inline char GetChar(int index) {
if (index < 10) return '0' + index;
return 'a' + (index - 10);
}
static inline std::string EncodeWechsler(const LifeState& pattern);
static inline std::string EncodeRuns(const std::vector<uint8_t>& strip);
static inline std::vector<uint8_t> PatternToStrip(const LifeState& pattern, int minX, int maxX, int minY, int maxY);
static inline std::string FindCanonical(const LifeState& pattern);
public:
static inline std::string EncodeStillLife(const LifeState& pattern);
static inline std::string EncodeOscillator(const LifeState& pattern, unsigned period);
};
class Decoder {
private:
static inline std::vector<std::vector<uint8_t>> DecodeWechsler(const std::string& suffix);
static inline LifeState StripsToPattern(const std::vector<std::vector<uint8_t>>& strips);
public:
static inline LifeState Decode(const std::string& apgcode);
static inline bool IsValidApgcode(const std::string& apgcode);
};
std::string Encoder::EncodeWechsler(const LifeState& pattern) {
if (pattern.IsEmpty()) return "";
auto [minX, minY, maxX, maxY] = pattern.XYBounds();
std::vector<std::string> strips;
for (int startY = minY; startY <= maxY; startY += 5) {
int endY = std::min(startY + 4, maxY);
auto strip = PatternToStrip(pattern, minX, maxX, startY, endY);
strips.push_back(EncodeRuns(strip));
}
std::string result;
for (size_t i = 0; i < strips.size(); ++i) {
if (i > 0) result += 'z';
result += strips[i];
}
return result;
}
std::vector<uint8_t> Encoder::PatternToStrip(const LifeState& pattern, int minX, int maxX, int minY, int maxY) {
std::vector<uint8_t> result;
for (int x = minX; x <= maxX; ++x) {
uint8_t column = 0;
for (int y = minY; y <= maxY; ++y) {
if (pattern.GetSafe(x, y)) {
column |= (1 << (y - minY));
}
}
result.push_back(column);
}
while (!result.empty() && result.back() == 0) {
result.pop_back();
}
return result;
}
std::string Encoder::EncodeRuns(const std::vector<uint8_t>& strip) {
if (strip.empty()) return "";
std::string result;
size_t i = 0;
while (i < strip.size()) {
if (strip[i] == 0) {
size_t runLength = 0;
while (i < strip.size() && strip[i] == 0) {
runLength++;
i++;
}
if (runLength == 2) {
result += 'w';
} else if (runLength == 3) {
result += 'x';
} else if (runLength >= 4 && runLength <= 39) {
result += 'y';
if (runLength <= 13) {
result += GetChar(runLength - 4);
} else if (runLength <= 35) {
result += GetChar(runLength - 14 + 10);
} else {
result += GetChar(runLength - 36 + 32);
}
} else {
for (size_t j = 0; j < runLength; ++j) {
result += '0';
}
}
} else {
result += GetChar(strip[i]);
i++;
}
}
return result;
}
std::string Encoder::FindCanonical(const LifeState& pattern) {
std::vector<std::string> candidates;
auto orbit = pattern.SymmetryOrbit();
for (const auto& variant : orbit) {
std::string encoded = EncodeWechsler(variant);
if (!encoded.empty()) {
candidates.push_back(encoded);
}
}
if (candidates.empty()) {
return "";
}
std::sort(candidates.begin(), candidates.end(), [](const std::string& a, const std::string& b) {
if (a.length() != b.length()) return a.length() < b.length();
return a < b;
});
return candidates[0];
}
std::string Encoder::EncodeStillLife(const LifeState& pattern) {
unsigned pop = pattern.GetPop();
std::string suffix = FindCanonical(pattern);
if (suffix.empty()) return "";
return "xs" + std::to_string(pop) + "_" + suffix;
}
std::string Encoder::EncodeOscillator(const LifeState& pattern, unsigned period) {
std::string suffix = FindCanonical(pattern);
if (suffix.empty()) return "";
return "xp" + std::to_string(period) + "_" + suffix;
}
std::vector<std::vector<uint8_t>> Decoder::DecodeWechsler(const std::string& suffix) {
std::vector<std::vector<uint8_t>> result;
std::vector<uint8_t> row;
size_t i = 0;
while (i < suffix.length()) {
char c = suffix[i];
if (c == 'z') {
i++;
result.push_back(row);
row = {};
continue;
}
if (c == 'w') {
row.push_back(0);
row.push_back(0);
} else if (c == 'x') {
row.push_back(0);
row.push_back(0);
row.push_back(0);
} else if (c == 'y' && i + 1 < suffix.length()) {
char next = suffix[i + 1];
int runLength = 4;
if (next >= '0' && next <= '9') {
runLength += (next - '0');
} else if (next >= 'a' && next <= 'v') {
runLength += (next - 'a' + 10);
}
for (int j = 0; j < runLength; ++j) {
row.push_back(0);
}
i++;
} else if (c >= '0' && c <= '9') {
row.push_back(c - '0');
} else if (c >= 'a' && c <= 'v') {
row.push_back(c - 'a' + 10);
}
i++;
}
if(row.size() > 0)
result.push_back(row);
return result;
}
LifeState
Decoder::StripsToPattern(const std::vector<std::vector<uint8_t>> &strips) {
LifeState result;
int y = 0;
for (auto &row : strips) {
for (size_t x = 0; x < row.size(); ++x) {
uint8_t column = row[x];
for (int bit = 0; bit < 5; ++bit) {
if (column & (1 << bit)) {
result.SetSafe(x, y + bit, true);
}
}
}
y += 5;
}
return result;
}
LifeState Decoder::Decode(const std::string& apgcode) {
size_t underscorePos = apgcode.find('_');
if (underscorePos == std::string::npos) {
return LifeState();
}
std::string prefix = apgcode.substr(0, underscorePos);
std::string suffix = apgcode.substr(underscorePos + 1);
auto decoded = DecodeWechsler(suffix);
return StripsToPattern(decoded);
}
bool Decoder::IsValidApgcode(const std::string& apgcode) {
size_t underscorePos = apgcode.find('_');
if (underscorePos == std::string::npos) {
return false;
}
std::string prefix = apgcode.substr(0, underscorePos);
return (prefix.substr(0, 2) == "xs" || prefix.substr(0, 2) == "xp") &&
prefix.length() > 2;
}
} // namespace Apgcode
// LifeState method implementations
std::string LifeState::EncodeApgcode() const {
return Apgcode::Encoder::EncodeStillLife(*this);
}
std::string LifeState::EncodeApgcode(unsigned period) const {
return Apgcode::Encoder::EncodeOscillator(*this, period);
}
LifeState LifeState::DecodeApgcode(const std::string& apgcode) {
return Apgcode::Decoder::Decode(apgcode);
}