Created
July 10, 2023 05:15
-
-
Save nickforce/885aad8ef0d335b0d365b5b94d09996f to your computer and use it in GitHub Desktop.
day01_adventofcode_2022_part2
This file contains hidden or 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 * as fs from 'fs'; | |
| const elves = fs.readFileSync("day01.txt", { encoding: "utf-8" }) | |
| .replace(/\r/g, "") | |
| .trim() | |
| .split("\n\n"); // split on newline | |
| function findTop3CalorieElves() { | |
| const calories = elves.map((elf) => { | |
| const calories = elf.split("\n").map(Number); | |
| return calories.reduce((prev, cur) => prev + cur, 0) | |
| }); | |
| calories.sort((a, b) => b - a); | |
| console.log( | |
| calories.slice(0, 3).reduce((prev, cur) => prev + cur, 0) | |
| ); | |
| } | |
| findTop3CalorieElves(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment