Skip to content

Instantly share code, notes, and snippets.

@nandastone
Last active December 1, 2022 08:02
Show Gist options
  • Save nandastone/60ce672d988c5df2b4dea57fb29caec4 to your computer and use it in GitHub Desktop.
Save nandastone/60ce672d988c5df2b4dea57fb29caec4 to your computer and use it in GitHub Desktop.
1-1
import fs from "fs";
const input = fs.readFileSync('./input.txt').toString()
const elves = input.split(/\n\s*\n/);
const calories = elves
// Split each elf's list of foods (new lines) and sum into the total calories for all their foods.
.map(
(elf) =>
elf
.split(/\n/)
.map((calories) => parseInt(calories, 10))
.reduce((acc, curr) => acc + curr),
0
)
// Sort DESC.
.sort((a, b) => b - a);
// Because the list of calories is sorted DESC, the first item will be the largest.
const highestCalories = calories[0]
console.log(highestCalories);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment