Public
Edited
May 8, 2023
1 fork
1 star
Insert cell
Insert cell
Insert cell
function addValue(id, val, botnet) {
if (botnet[id] == undefined) {
botnet[id] = { vals: [val], low: "", hi: "" };
} else {
botnet[id].vals.push(val);
AOC.sort(botnet[id].vals);
}
return botnet;
}
Insert cell
Insert cell
function parseInput(input) {
const botnet = {};
input.split("\n").forEach((line) => {
const match = line.match(
/(\d+)(?:.*low| goes) to (bot|output) (\d+)(?:.*(bot|output) (\d+))?/
);
if (match[4] == undefined) {
// We have a 'value' instruction
addValue(match[2] + match[3], Number(match[1]), botnet);
} else {
// We have a bot transfer instruction
botnet["bot" + match[1]] = {
vals: botnet["bot" + match[1]] ? botnet["bot" + match[1]].vals : [],
low: match[2] + match[3],
hi: match[4] + match[5]
};
}
});
return botnet;
}
Insert cell
Insert cell
function propagate(botnet) {
const completed = {};
while (Object.keys(botnet).length > 0) {
for (const [k, v] of Object.entries(botnet)) {
if (v.vals.length == 2 || k.startsWith("output")) {
if (v.vals.length == 2) {
addValue(v.low, v.vals[0], botnet);
addValue(v.hi, v.vals[1], botnet);
}
completed[k] = v;
delete botnet[k];
}
}
}
return completed;
}
Insert cell
Insert cell
function part1(input) {
const botnet = propagate(parseInput(input));
for (const [k, v] of Object.entries(botnet)) {
if (v.vals[0] === 17 && v.vals[1] === 61) {
return k;
}
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
const botnet = propagate(parseInput(puzzleInput));
return botnet.output0.vals * botnet.output1.vals[0] * botnet.output2.vals[0];
}
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