Public
Edited
Dec 24, 2022
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
caloriesPerElf(part1ExampleText)
Insert cell
caloriesPerElf = (list) =>
R.pipe(
R.split("\n"),
R.map(Number), // note: turns empty lines into 0; no problem since does not affect sum per Elf
R.groupWith(R.identity), // group by falsy borders: the 0s
R.map(R.sum), // sum per Elf
)(list)
Insert cell
Insert cell
// just pick Elf with maximum calories: 4th Elf in this case
part1Answer = R.pipe(
caloriesPerElf,
R.apply(Math.max)
)
(part1ExampleText)
Insert cell
Insert cell
totalCaloriesTop3 = (list) => R.pipe(
caloriesPerElf, // get list of total calories per Elf
R.sort(descending), // sort descending
R.slice(0, 3), // grab top three
R.sum, // and sum
)(list)
Insert cell
descending = (a, b) => b - a
Insert cell
totalCaloriesTop3(part1ExampleText)
Insert cell
totalCaloriesTop3(input)
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