Solution for day one
This commit is contained in:
parent
9362062718
commit
8764826a99
32
01/code.js
Normal file
32
01/code.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
const fs = require('fs');
|
||||
|
||||
const inputArray = fs.readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
// Part One
|
||||
let caloriesArray = [];
|
||||
let calories = 0;
|
||||
|
||||
for (i in inputArray) {
|
||||
const value = parseInt(inputArray[i]);
|
||||
if (value > 0) {
|
||||
calories += value;
|
||||
} else {
|
||||
caloriesArray.push(calories);
|
||||
calories = 0;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(Math.max(...caloriesArray));
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
const first = Math.max(...caloriesArray);
|
||||
|
||||
caloriesArray.splice(caloriesArray.indexOf(first), 1);
|
||||
const second = Math.max(...caloriesArray);
|
||||
|
||||
caloriesArray.splice(caloriesArray.indexOf(second), 1);
|
||||
const third = Math.max(...caloriesArray);
|
||||
|
||||
console.log(first + second + third);
|
2255
01/input.txt
Normal file
2255
01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user