function decompressedLength2(string) {
let i = 0;
let totalLength = 0;
const parseUntil = (test) => {
let buf = "";
while (true) {
const c = string[i];
if (c == undefined) {
return buf;
}
if (test(c)) {
i++;
return buf;
}
buf += c;
i++;
}
};
const takeCount = (count) => {
let buf = "";
let j = 0;
while (j < count) {
const c = string[i + j];
buf += c;
j++;
}
return buf;
};
while (i < string.length) {
const chars = parseUntil((c) => c == "(");
totalLength += chars.length;
const countToTake = Number(parseUntil((c) => c == "x"));
const repeats = Number(parseUntil((c) => c == ")"));
const substring = takeCount(countToTake);
totalLength += decompressedLength2(substring) * repeats;
i += countToTake;
}
return totalLength;
}