Public
Edited
Dec 1, 2022
1 fork
2 stars
Insert cell
Insert cell
rawData = FileAttachment("aocdata-1").text()
Insert cell
lines = rawData.split("\n")
Insert cell
calorieSums = {
let sums = [0];

for (let i = 0; i < lines.length; i++) {
if (lines[i] != "") {
sums[sums.length - 1] += +lines[i]; // add the snack to the current elf's total
} else {
sums.push(0); // start counting for a new elf
}
}

return sums;
}
Insert cell
d3.max(calorieSums) // First answer: the highest calorie sum
Insert cell
sortedSums = calorieSums.sort((a, b) => a - b).reverse()
Insert cell
top3Total = sortedSums
.slice(0, 3)
.reduce((accumulator, currentValue) => accumulator + currentValue) // Second answer: top 3 calorie sums
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