From f54545b33bf2e8eff5059d16e1b467479b5faeee Mon Sep 17 00:00:00 2001 From: nbhattac Date: Sun, 1 Dec 2019 16:03:00 -0800 Subject: [PATCH] Modify file path + added part b --- Day1/day1.js | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/Day1/day1.js b/Day1/day1.js index c8f2c85..61bbc5e 100644 --- a/Day1/day1.js +++ b/Day1/day1.js @@ -1,6 +1,7 @@ +const path = require("path"); + function readFile(fileName) { const fs = require("fs"); - const path = require("path"); return fs.readFileSync(fileName, 'utf8'); } @@ -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)