-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
File Parsing, Getting Line, Floor, and Reduce
- Loading branch information
Showing
2 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
function readFile(fileName) { | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
return fs.readFileSync(fileName, 'utf8'); | ||
} | ||
|
||
function getLine(textBuffer) { | ||
const arr = textBuffer.split('\n').filter(line => { | ||
return line.length > 0; | ||
}); | ||
|
||
return arr; | ||
} | ||
|
||
function calculateFuelPerModule(massString) { | ||
const mass = parseInt(massString) | ||
console.log("Mass : ", mass) | ||
const fuel = Math.floor(mass/3) - 2 | ||
console.log(fuel) | ||
return fuel; | ||
} | ||
|
||
|
||
const textFileBuffer = readFile("./input.txt"); | ||
const lines = getLine(textFileBuffer); | ||
const totalFuel = lines.reduce(function(sum, line) { | ||
return sum + calculateFuelPerModule(line); | ||
}, 0); | ||
console.log("Total Fuel: ", totalFuel) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
50350 | ||
104487 | ||
101866 | ||
143582 | ||
58497 | ||
69981 | ||
98300 | ||
119291 | ||
148489 | ||
83005 | ||
107291 | ||
124738 | ||
142256 | ||
108102 | ||
121054 | ||
119697 | ||
75546 | ||
109022 | ||
136754 | ||
52073 | ||
115235 | ||
87668 | ||
64523 | ||
71179 | ||
69071 | ||
142380 | ||
68233 | ||
115226 | ||
132656 | ||
137007 | ||
82838 | ||
79339 | ||
131726 | ||
52295 | ||
102941 | ||
98297 | ||
144374 | ||
118998 | ||
63910 | ||
146772 | ||
82916 | ||
72068 | ||
82855 | ||
55915 | ||
91663 | ||
82917 | ||
105876 | ||
119551 | ||
70639 | ||
114459 | ||
129235 | ||
56041 | ||
70031 | ||
145187 | ||
54913 | ||
56928 | ||
52159 | ||
144384 | ||
80104 | ||
83932 | ||
81334 | ||
72693 | ||
50595 | ||
128895 | ||
54138 | ||
79126 | ||
69930 | ||
72896 | ||
108357 | ||
67415 | ||
110581 | ||
131477 | ||
65517 | ||
87912 | ||
125782 | ||
51785 | ||
145472 | ||
54358 | ||
87715 | ||
98067 | ||
99791 | ||
92502 | ||
50750 | ||
76614 | ||
110137 | ||
56118 | ||
149501 | ||
76542 | ||
87183 | ||
128333 | ||
127657 | ||
144246 | ||
141704 | ||
96873 | ||
62434 | ||
136609 | ||
121829 | ||
111796 | ||
103936 | ||
69807 |