Skip to content

Commit

Permalink
Day 1 Part A of advent of code 2019
Browse files Browse the repository at this point in the history
File Parsing, Getting Line, Floor, and Reduce
  • Loading branch information
nbhattac committed Dec 1, 2019
1 parent d1be365 commit fedb874
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Day1/day1.js
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)


100 changes: 100 additions & 0 deletions Day1/input.txt
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

0 comments on commit fedb874

Please sign in to comment.