Public
Edited
Dec 11, 2022
Insert cell
Insert cell
Insert cell
elves = (await FileAttachment("input1.txt").text())
.split("\n\n")
.map((elf) => elf.split("\n"))
Insert cell
sums = elves.map((elf) => {
return d3.sum(elf);
})
Insert cell
maxCalories = Math.max(...sums)
Insert cell
top3Calories = d3.sum(sums.sort((a, b) => b - a).slice(0, 3))
Insert cell
Insert cell
rounds = (await FileAttachment("input2.txt").text())
.split("\n")
.map((round) => round.split(" "))
Insert cell
points = calculatePoints(rounds)
Insert cell
calculatePoints = (rounds) => {
let pts = 0;
rounds.forEach(([opponentChoice, desiredOutcome]) => {
if (desiredOutcome === "X") {
// lose
pts += 0;
if (opponentChoice === "A") {
// opponent chooses rock, so I choose scissors (3 points)
pts += 3;
} else if (opponentChoice === "B") {
// opponent chooses paper, so I choose rock
pts += 1;
} else if (opponentChoice === "C") {
// opponent chooses scissor, so I choose paper
pts += 2;
}
} else if (desiredOutcome === "Y") {
// draw
pts += 3;
if (opponentChoice === "A") {
// opponent chooses rock, choose same as opponent
pts += 1;
} else if (opponentChoice === "B") {
// opponent chooses paper, choose same as opponent
pts += 2;
} else if (opponentChoice === "C") {
// opponent chooses scissor, choose same as opponent
pts += 3;
}
} else if (desiredOutcome === "Z") {
// win
pts += 6;
if (opponentChoice === "A") {
// opponent chooses rock, so I choose paper
pts += 2;
} else if (opponentChoice === "B") {
// opponent chooses paper, so I choose scissor
pts += 3;
} else if (opponentChoice === "C") {
// opponent chooses scissor, so I choose rock
pts += 1;
}
}
});
return pts;
}
Insert cell
Insert cell
lines = (await FileAttachment("input3.txt").text())
.split("\n");
Insert cell
rucksacks = lines.map((rucksack) => {
const half = rucksack.length / 2;
const firstCompartment = rucksack.substring(0, half); // CjhshBJCSr
const secondCompartment = rucksack.substring(half); // TTsLwqwqwb
return [firstCompartment, secondCompartment];
});
Insert cell
priorityOfFirstChar = (char) => {
const firstLetter = char.substring(0, 1);
const upperCase = firstLetter === firstLetter.toUpperCase();
// a/A is char code 65
return firstLetter.charCodeAt(0) - 64;
}
Insert cell
priorityOfFirstChar("A")
Insert cell
"A".charCodeAt(0)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more