Skip to content

Instantly share code, notes, and snippets.

@realStandal
Created December 1, 2019 12:20
Show Gist options
  • Save realStandal/0e0a48d87765c8a640d4f3186dd40df9 to your computer and use it in GitHub Desktop.
Save realStandal/0e0a48d87765c8a640d4f3186dd40df9 to your computer and use it in GitHub Desktop.
Advent of Code 2019
// Given:
// F = floor(M / 3) - 2
//
// Find the Fuel Required (F) for all of the given masses (M) day01.txt
day01();
/**
*
*/
function day01()
{
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('day01/day01.txt')
});
let totalItems = 100,
index = 0,
totalFuel = 0;
lineReader.on('line', (line) => {
index++;
let fuel = fuelFromMass(parseInt(line));
totalFuel += fuel;
while(fuel > 0)
{
fuel = fuelFromMass(fuel);
totalFuel += fuel;
}
if(index == totalItems)
console.log(totalFuel);
});
}
/**
* F = floor(M / 3) - 2
* @param mass Mass of an object (module)
*/
function fuelFromMass(mass)
{
let fuel = (Math.floor(mass / 3) - 2);
if(fuel > 0)
return fuel;
else
return 0;
}
// day01.txt
/*
108546
76196
144412
100530
143908
79763
109927
53656
82633
103781
97202
81600
115278
90095
85533
58230
142490
65176
132915
82319
148743
91444
145760
78002
127484
67225
74721
145620
146254
135544
74198
88015
53595
142036
113928
65217
56126
110117
57729
99052
89262
141540
70472
145271
81548
68065
93431
125210
66454
67709
149409
101787
130111
60569
131869
149702
135564
135094
71358
100169
127644
147741
102918
93503
74752
135883
120158
94570
129517
85602
55921
76746
107055
79320
81991
58982
63009
91360
147253
51139
61871
107140
146767
77441
125533
70317
125271
73189
141359
144549
104812
91315
145163
147202
95111
82628
116839
132358
99704
85305
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment