Public
Edited
May 7, 2023
1 fork
1 star
Insert cell
Insert cell
Insert cell
function decompressedLen(input) {
let [len, i] = [0, 0];
while (i < input.length) {
if (input[i] === "(") {
const markerEnd = input.indexOf(")", i);
const [n, r] = input
.slice(i + 1, markerEnd)
.split("x")
.map(Number);
len += n * r;
i = markerEnd + 1 + n;
} else {
len++;
i++;
}
}
return len;
}
Insert cell
function part1(input) {
return decompressedLen(input);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function decompressedLen2(input) {
const weights = new Array(input.length).fill(1);
let [len, i] = [0, 0];

while (i < input.length) {
if (input[i] === "(") {
const markerEnd = input.indexOf(")", i);
const [n, r] = input
.substring(i + 1, markerEnd)
.split("x")
.map(Number);

for (let j = 0; j < n; j++) {
weights[j + markerEnd + 1] *= r;
}
i = markerEnd + 1;
} else {
len += weights[i];
i++;
}
}
return len;
}
Insert cell
function part2(input) {
return decompressedLen2(input);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more