forked from tpatel/advent-of-code-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.mjs
31 lines (24 loc) · 868 Bytes
/
day01.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { readFileSync } from "node:fs";
const elves = readFileSync("day01.txt", { encoding: "utf-8" }) // read day??.txt content
.replace(/\r/g, "") // remove all \r characters to avoid issues on Windows
.trim() // Remove starting/ending whitespace
.split("\n\n"); // Split on newline
function part1() {
const calories = elves.map((elf) => {
const calories = elf.split("\n").map(Number);
return calories.reduce((previous, current) => previous + current, 0);
});
console.log(Math.max(...calories));
}
function part2() {
const calories = elves.map((elf) => {
const calories = elf.split("\n").map(Number);
return calories.reduce((previous, current) => previous + current, 0);
});
calories.sort((a, b) => b - a);
console.log(
calories.slice(0, 3).reduce((previous, current) => previous + current, 0)
);
}
part1();
part2();