-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle01.js
26 lines (20 loc) · 901 Bytes
/
puzzle01.js
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
const fs = require("fs");
const files = fs.readFileSync("./puzzle01-input.txt", "utf8"); //Read the input numbers of Calories as a string
const arr = files.replace(/\r/g, "").trim().split("\n\n"); //remove all carriage returns and split by double line return
const totalCalories = arr.map((x) => {
return x
.split("\n") //split by single line return for each item
.map(Number) //turn string into a number
.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
}, 0); //get the sum using the reduce method.
});
//Part1 Solution:
const maxTotalCalories = Math.max(...totalCalories);
console.log(maxTotalCalories);
//Part2:
//First sort the totalCalories array in descending order:
totalCalories.sort((a, b) => b - a);
//Part2 Solution:
topThreeTotalCalories = totalCalories[0] + totalCalories[1] + totalCalories[2];
console.log(topThreeTotalCalories);