Public
Edited
Dec 5, 2022
1 fork
1 star
Insert cell
Insert cell
Insert cell
parseCrates = (input) => {
const lines = input.split("\n");
const stackLine = lines[lines.length - 1];
const stackNames = stackLine.trim().split(/\s+/);
const stacks = stackNames.reduce((obj, name) => {
obj[name] = [];
return obj;
}, {});

// The position of the stack names lines up with the crate name
// so we can use the string index to find crates
const stackPositions = stackNames.reduce((obj, name) => {
obj[name] = stackLine.indexOf(name);
return obj;
}, {});

// Walk from the bottom up and push onto our stacks
for (let i = lines.length - 1; i--; i >= 0) {
stackNames.forEach((n) => {
if (lines[i][stackPositions[n]] !== " ")
stacks[n].push(lines[i][stackPositions[n]]);
});
}
return stacks;
}
Insert cell
parseMoves = (input) => {
return input.split("\n").map((l) => {
const tokens = l.split(" ");
return { count: tokens[1], from: tokens[3], to: tokens[5] };
});
}
Insert cell
parse = (input) => {
const [crates, moves] = input.split("\n\n");
return [ parseCrates(crates), parseMoves( moves) ];
}
Insert cell
parse(testInput)
Insert cell
mover = (crates, move) => {
for (let i = 0; i < move.count; i++) {
crates[move.to].push(crates[move.from].pop());
}
return crates;
}
Insert cell
{
const [crates, moves] = parse(testInput);
return mover(crates, moves[0]);
}
Insert cell
part1 = (input) => {
const [crates, moves] = parse(input);
const movedCrates = moves.reduce(mover, crates);
return Object.keys(movedCrates)
.map((k) => movedCrates[k].slice(-1)[0])
.join("");
}
Insert cell
part1(testInput)
Insert cell
Insert cell
part1(input)
Insert cell
mover2 = (crates, move) => {
const craneLoad = crates[move.from].splice(
crates[move.from].length - move.count,
move.count
);
crates[move.to] = crates[move.to].concat(craneLoad);
return crates;
}
Insert cell
{
let [crates, moves] = parse(testInput);
crates = mover2(crates, moves[0]);
crates = mover2(crates, moves[1]);
crates = mover2(crates, moves[2]);
crates = mover2(crates, moves[3]);
return crates;
}
Insert cell
part2 = (input) => {
const [crates, moves] = parse(input);
const movedCrates = moves.reduce(mover2, crates);
// return movedCrates;
return Object.keys(movedCrates)
.sort()
.map((k) => movedCrates[k].slice(-1)[0])
.join("");
}
Insert cell
part2(testInput)
Insert cell
part2(input)
Insert cell
function findElfWithMostCalories(caloriesPerElf) {
// Keep track of the Elf with the most calories and their total calories
let elfWithMostCalories = null;
let maxCalories = 0;

// Loop through each Elf's inventory of food
for (const inventory of caloriesPerElf) {
// Calculate the total calories for this Elf's inventory
const totalCalories = inventory.reduce((sum, calories) => sum + calories, 0);

// If this Elf has more calories than the current Elf with the most calories,
// update the Elf with the most calories and their total calories
if (totalCalories > maxCalories) {
elfWithMostCalories = inventory;
maxCalories = totalCalories;
}
}

// Return the Elf with the most calories and their total calories
return [elfWithMostCalories, maxCalories];
}
Insert cell
{
// Example usage
const elfCalories = [
[1000, 2000, 3000],
[4000],
[5000, 6000],
[7000, 8000, 9000],
[10000]
];

return findElfWithMostCalories(elfCalories);
}
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