Skip to content

Commit

Permalink
Modify file path + added part b
Browse files Browse the repository at this point in the history
  • Loading branch information
nbhattac committed Dec 2, 2019
1 parent fedb874 commit f54545b
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions Day1/day1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require("path");

function readFile(fileName) {
const fs = require("fs");
const path = require("path");
return fs.readFileSync(fileName, 'utf8');
}

Expand All @@ -14,18 +15,39 @@ function getLine(textBuffer) {

function calculateFuelPerModule(massString) {
const mass = parseInt(massString)
console.log("Mass : ", mass)
const fuel = Math.floor(mass/3) - 2
console.log(fuel)
return fuel
}

function calculateReducedFuelPerModule(massString) {
let mass = parseInt(massString)
let fuel = 0

do
{
tempFuel = Math.floor(mass/3) - 2
fuel += tempFuel
mass = tempFuel
}while(mass/3 > 2)

return fuel;
}


console.log(path.join(__dirname, "input.txt"))

const textFileBuffer = readFile("./input.txt");
const textFileBuffer = readFile(path.join(__dirname, "input.txt"));
const lines = getLine(textFileBuffer);
const totalFuel = lines.reduce(function(sum, line) {
return sum + calculateFuelPerModule(line);
}, 0);

const totalAugmentedFuel = lines.reduce(function(sum, moduleFuel)
{
return sum + calculateReducedFuelPerModule(moduleFuel);
}, 0);

console.log("Total Fuel: ", totalFuel)
console.log("Augmented Fuel : ", totalAugmentedFuel)


0 comments on commit f54545b

Please sign in to comment.