Skip to content

Instantly share code, notes, and snippets.

@nickforce
Created July 10, 2023 05:15
Show Gist options
  • Select an option

  • Save nickforce/885aad8ef0d335b0d365b5b94d09996f to your computer and use it in GitHub Desktop.

Select an option

Save nickforce/885aad8ef0d335b0d365b5b94d09996f to your computer and use it in GitHub Desktop.
day01_adventofcode_2022_part2
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