Public
Edited
Dec 1, 2023
Insert cell
Insert cell
Insert cell
function parse(input) {
return input.split("\n").map((room) => {
const matches = room.match(/(.+)-(\d+)\[(.+)\]/);
return {
name: matches[1],
id: Number(matches[2]),
checksum: matches[3]
};
});
}
Insert cell
Insert cell
function top5(room) {
const fTable = new Map();
[...room.name.replace(/-/g, "")].forEach((c) =>
AOC.addToFreqTable(fTable, c)
);
return [...fTable]
.sort(([c1, f1], [c2, f2]) =>
f2 === f1 ? c1.charCodeAt(0) - c2.charCodeAt(0) : f2 - f1
)
.slice(0, 5)
.map(([c, f]) => c)
.join("");
}
Insert cell
Insert cell
function validRooms(rooms) {
return rooms.filter((room) => room.checksum === top5(room));
}
Insert cell
Insert cell
function part1(input) {
return AOC.sum(validRooms(parse(input)).map((room) => room.id));
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function decrypt(room) {
const aCode = "a".charCodeAt(0);
return [...room.name]
.map((c) =>
c === "-"
? " "
: String.fromCharCode(
((c.charCodeAt(0) - aCode + room.id) % 26) + aCode
)
)
.join("");
}
Insert cell
Insert cell
function part2(input) {
return validRooms(parse(input)).filter((room) =>
decrypt(room).includes("north")
)[0].id;
}
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