-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution-1.js
44 lines (35 loc) · 1.03 KB
/
solution-1.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';
const fs = require('fs');
const input = fs
.readFileSync('./input1')
.toString('utf-8')
.trim()
.split('\n')
console.time('run');
// Fuel pr. module = Math.floor(mass / 3) - 2
const masses = input.map(value => parseInt(value));
const getFuelRequired = (mass) => {
return Math.floor(mass / 3) - 2;
}
const answer = masses.reduce((acc, curr) => {
const currentFuel = getFuelRequired(curr);
return acc += currentFuel;
}, 0);
console.log(`Answer part 1: ${answer}`);
// PART TWO ====================================
let fuelArray = [];
const calculateFuelConsumption = (mass) => {
let totalFuel = getFuelRequired(mass);
fuelArray.push(totalFuel);
if (getFuelRequired(totalFuel) >= 0) {
return calculateFuelConsumption(totalFuel);
}
return fuelArray;
};
const answerPartTwo = masses.reduce((acc, curr) => {
const totalFuel = calculateFuelConsumption(curr).reduce((a, b) => a + b, 0);
fuelArray = [];
return acc += totalFuel;
}, 0);
console.log(`Answer part 2: ${answerPartTwo}`);
console.timeEnd('run');