Last active
December 1, 2022 08:02
-
-
Save nandastone/60ce672d988c5df2b4dea57fb29caec4 to your computer and use it in GitHub Desktop.
1-1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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