-
Notifications
You must be signed in to change notification settings - Fork 0
/
adventofcode-day1.3.js
73 lines (57 loc) · 1.84 KB
/
adventofcode-day1.3.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const readline = require('readline');
var expect = require('chai').expect;
const fs = require('fs');
var totalFuel = 0;
async function processLineByLine() {
const fileStream = fs.createReadStream('input.txt');
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
// Note: we use the crlfDelay option to recognize all instances of CR LF
// ('\r\n') in input.txt as a single line break.
for await (const line of rl) {
// Each line in input.txt will be successively available here as `line`.
var fuel = calculateFuelFuel(line);
totalFuel = totalFuel + fuel;
console.log(`Mass: ${line} - Fuel: ${fuel} - TotalFuel: ${totalFuel}`);
}
}
processLineByLine();
function calculateFuel(mass){
return Math.floor(mass / 3) - 2;
};
function calculateFuelFuel(mass){
var fuelFuel = 0;
while (mass > 0) {
mass = Math.max(0, Math.floor((mass / 3) - 2));
fuelFuel = fuelFuel + mass;
console.log(`Mass: ${mass} - fuelFuel: ${fuelFuel}`);
}
return fuelFuel
};
describe("Calculate Fuel", function() {
it("converts 12 mass to fuel units", function() {
expect(calculateFuel(12)).to.equal(2);
});
it("converts 14 mass to fuel units", function() {
expect(calculateFuel(14)).to.equal(2);
});
it("converts 1969 mass to fuel units", function() {
expect(calculateFuel(1969)).to.equal(654);
});
it("converts 100756 mass to fuel units", function() {
expect(calculateFuel(100756)).to.equal(33583);
});
});
describe("Calculate FuelFuel", function() {
it("converts 12 mass to fuel units", function() {
expect(calculateFuelFuel(12)).to.equal(2);
});
it("converts 1969 mass to fuel units", function() {
expect(calculateFuelFuel(1969)).to.equal(966);
});
it("converts 100756 mass to fuel units", function() {
expect(calculateFuelFuel(100756)).to.equal(50346);
});
});