-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
296 lines (242 loc) · 7.49 KB
/
Copy pathutils.js
File metadata and controls
296 lines (242 loc) · 7.49 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
/* Helper functions for Coding Contract implementations */
function removeBracketsFromArrayString(str) {
let strCpy = str;
if (strCpy.startsWith("[")) {
strCpy = strCpy.slice(1);
}
if (strCpy.endsWith("]")) {
strCpy = strCpy.slice(0, -1);
}
return strCpy;
}
function removeQuotesFromString(str) {
let strCpy = str;
if (strCpy.startsWith('"') || strCpy.startsWith("'")) {
strCpy = strCpy.slice(1);
}
if (strCpy.endsWith('"') || strCpy.endsWith("'")) {
strCpy = strCpy.slice(0, -1);
}
return strCpy;
}
function convert2DArrayToString(arr) {
const components = [];
arr.forEach((e) => {
let s = String(e);
s = ["[", s, "]"].join("");
components.push(s);
});
return components.join(",").replace(/\s/g, "");
}
// compress plaintest string
function comprLZEncode(plain) {
// for state[i][j]:
// if i is 0, we're adding a literal of length j
// else, we're adding a backreference of offset i and length j
let cur_state = Array.from(Array(10), () => Array(10).fill(null));
let new_state = Array.from(Array(10), () => Array(10));
function set(state, i, j, str) {
const current = state[i][j];
if (current == null || str.length < current.length) {
state[i][j] = str;
} else if (str.length === current.length && Math.random() < 0.5) {
// if two strings are the same length, pick randomly so that
// we generate more possible inputs to Compression II
state[i][j] = str;
}
}
// initial state is a literal of length 1
cur_state[0][1] = "";
for (let i = 1; i < plain.length; ++i) {
for (const row of new_state) {
row.fill(null);
}
const c = plain[i];
// handle literals
for (let length = 1; length <= 9; ++length) {
const string = cur_state[0][length];
if (string == null) {
continue;
}
if (length < 9) {
// extend current literal
set(new_state, 0, length + 1, string);
} else {
// start new literal
set(new_state, 0, 1, string + "9" + plain.substring(i - 9, i) + "0");
}
for (let offset = 1; offset <= Math.min(9, i); ++offset) {
if (plain[i - offset] === c) {
// start new backreference
set(new_state, offset, 1, string + String(length) + plain.substring(i - length, i));
}
}
}
// handle backreferences
for (let offset = 1; offset <= 9; ++offset) {
for (let length = 1; length <= 9; ++length) {
const string = cur_state[offset][length];
if (string == null) {
continue;
}
if (plain[i - offset] === c) {
if (length < 9) {
// extend current backreference
set(new_state, offset, length + 1, string);
} else {
// start new backreference
set(new_state, offset, 1, string + "9" + String(offset) + "0");
}
}
// start new literal
set(new_state, 0, 1, string + String(length) + String(offset));
// end current backreference and start new backreference
for (let new_offset = 1; new_offset <= Math.min(9, i); ++new_offset) {
if (plain[i - new_offset] === c) {
set(new_state, new_offset, 1, string + String(length) + String(offset) + "0");
}
}
}
}
const tmp_state = new_state;
new_state = cur_state;
cur_state = tmp_state;
}
let result = null;
for (let len = 1; len <= 9; ++len) {
let string = cur_state[0][len];
if (string == null) {
continue;
}
string += String(len) + plain.substring(plain.length - len, plain.length);
if (result == null || string.length < result.length) {
result = string;
} else if (string.length == result.length && Math.random() < 0.5) {
result = string;
}
}
for (let offset = 1; offset <= 9; ++offset) {
for (let len = 1; len <= 9; ++len) {
let string = cur_state[offset][len];
if (string == null) {
continue;
}
string += String(len) + "" + String(offset);
if (result == null || string.length < result.length) {
result = string;
} else if (string.length == result.length && Math.random() < 0.5) {
result = string;
}
}
}
return result ?? "";
}
// decompress LZ-compressed string, or return null if input is invalid
function comprLZDecode(compr) {
let plain = "";
for (let i = 0; i < compr.length; ) {
const literal_length = compr.charCodeAt(i) - 0x30;
if (literal_length < 0 || literal_length > 9 || i + 1 + literal_length > compr.length) {
return null;
}
plain += compr.substring(i + 1, i + 1 + literal_length);
i += 1 + literal_length;
if (i >= compr.length) {
break;
}
const backref_length = compr.charCodeAt(i) - 0x30;
if (backref_length < 0 || backref_length > 9) {
return null;
} else if (backref_length === 0) {
++i;
} else {
if (i + 1 >= compr.length) {
return null;
}
const backref_offset = compr.charCodeAt(i + 1) - 0x30;
if ((backref_length > 0 && (backref_offset < 1 || backref_offset > 9)) || backref_offset > plain.length) {
return null;
}
for (let j = 0; j < backref_length; ++j) {
plain += plain[plain.length - backref_offset];
}
i += 2;
}
}
return plain;
}
function HammingEncode(data) {
const enc = [0];
const data_bits = data.toString(2).split("").reverse();
data_bits.forEach((e, i, a) => {
a[i] = parseInt(e);
});
let k = data_bits.length;
/* NOTE: writing the data like this flips the endianness, this is what the
* original implementation by Hedrauta did so I'm keeping it like it was. */
for (let i = 1; k > 0; i++) {
if ((i & (i - 1)) != 0) {
enc[i] = data_bits[--k];
} else {
enc[i] = 0;
}
}
let parity = 0;
/* Figure out the subsection parities */
for (let i = 0; i < enc.length; i++) {
if (enc[i]) {
parity ^= i;
}
}
parity = parity.toString(2).split("").reverse();
parity.forEach((e, i, a) => {
a[i] = parseInt(e);
});
/* Set the parity bits accordingly */
for (let i = 0; i < parity.length; i++) {
enc[2 ** i] = parity[i] ? 1 : 0;
}
parity = 0;
/* Figure out the overall parity for the entire block */
for (let i = 0; i < enc.length; i++) {
if (enc[i]) {
parity++;
}
}
/* Finally set the overall parity bit */
enc[0] = parity % 2 == 0 ? 0 : 1;
return enc.join("");
}
function HammingDecode(data) {
let err = 0;
const bits = [];
/* TODO why not just work with an array of digits from the start? */
for (const i in data.split("")) {
const bit = parseInt(data[i]);
bits[i] = bit;
if (bit) {
err ^= +i;
}
}
/* If err != 0 then it spells out the index of the bit that was flipped */
if (err) {
/* Flip to correct */
bits[err] = bits[err] ? 0 : 1;
}
/* Now we have to read the message, bit 0 is unused (it's the overall parity bit
* which we don't care about). Each bit at an index that is a power of 2 is
* a parity bit and not part of the actual message. */
let ans = "";
for (let i = 1; i < bits.length; i++) {
/* i is not a power of two so it's not a parity bit */
if ((i & (i - 1)) != 0) {
ans += bits[i];
}
}
/* TODO to avoid ambiguity about endianness why not let the player return the extracted (and corrected)
* data bits, rather than guessing at how to convert it to a decimal string? */
return parseInt(ans, 2);
}
function filterTruthy(input) {
return input.filter(Boolean);
}