Public
Edited
Aug 26, 2023
1 fork
1 star
Insert cell
Insert cell
Insert cell
function dragon(str, length) {
let bits = [...str].map((c) => (c == "1" ? true : false)); // Convert from string to boolean array
while (bits.length < length) {
bits = [...bits, false, ...bits.reverse().map((bit) => !bit)];
}
return bits.slice(0, length);
}
Insert cell
Insert cell
function checksum(bits) {
while (bits.length % 2 === 0) {
const newBits = [];
for (let i = 0; i < bits.length; i += 2) {
newBits.push(bits[i] === bits[i + 1]);
}
bits = newBits;
}
return bits.map((bit) => (bit ? "1" : "0")).join(""); // Convert from boolean array back to string
}
Insert cell
function part1(input) {
return checksum(dragon(input, 272));
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
return checksum(dragon(input, 35651584));
}
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