advent-of-code-2022/day01/index.js

33 lines
761 B
JavaScript
Raw Normal View History

2022-12-01 10:27:28 +00:00
import { readFileSync } from "fs";
const input = readFileSync("calories.txt", "utf-8");
const elves = [];
let elfNum = 0;
let isFirst = true;
input.split("\r\n").forEach(line => {
if(line === "") {
elfNum++;
isFirst = true;
} else {
if(isFirst) {
isFirst = false;
elves[elfNum] = parseInt(line);
} else elves[elfNum] += parseInt(line);
}
});
2022-12-01 10:37:59 +00:00
let maxCalories = [0, 0, 0];
2022-12-01 10:27:28 +00:00
elves.forEach(elf => {
2022-12-01 10:37:59 +00:00
if(elf > maxCalories[0]) {
maxCalories[2] = maxCalories[1];
maxCalories[1] = maxCalories[0];
maxCalories[0] = elf;
}
2022-12-01 10:27:28 +00:00
});
2022-12-01 10:37:59 +00:00
console.log(`Elf with most calories: ${maxCalories[0]}`);
console.log(`Top 3 elves calories: ${maxCalories.reduce((e, acc) => acc += e)}`);