Public
Edited
Oct 1, 2023
1 fork
Importers
1 star
Insert cell
Insert cell
Insert cell
function parse(input) {
return input.split(/,\s*/).map(Number);
}
Insert cell
Insert cell
function knotHash(lengths, repeats = 64) {
let [pos, skipSize] = [0, 0];
const list = d3.range(0, 256);
for (let rep = 0; rep < repeats; rep++) {
for (const length of lengths) {
const subArray = [];
for (let i = 0; i < length; i++) {
subArray.push(list[(pos + i) % 256]);
}
// Put the reversed subarray back into main list
subArray.reverse();
for (let i = 0; i < length; i++) {
list[(pos + i) % 256] = subArray[i];
}
pos = (pos + length + skipSize++) % 256; // Update position and skip size
}
}
return list;
}
Insert cell
Insert cell
function part1(input) {
return AOC.product(knotHash(parse(input), 1).slice(0, 2));
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function parse2(input) {
const asciiInput = [...input.trim()].map((c) => c.charCodeAt(0));
return [...asciiInput, 17, 31, 73, 47, 23];
}
Insert cell
Insert cell
function denseHash(list) {
const hash = [];
for (let i = 0; i < 256; i += 16) {
hash.push(list.slice(i, i + 16).reduce((acc, x) => acc ^ x));
}
return hash.map((d) => AOC.decToHex(d)).join("");
}
Insert cell
Insert cell
function part2(input) {
return denseHash(knotHash(parse2(input)));
}
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