Skip to content

Commit e7a0d0f

Browse files
feat(2019-day-02): fueling the fuel
1 parent fd6fb51 commit e7a0d0f

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

2019/day-01/fuel-calculator.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const calculateFuel = (mass) => {
2+
return Math.max(
3+
0,
4+
Math.floor(mass / 3) - 2
5+
)
6+
}
7+
8+
const sumMassWithFuel = (mass) => {
9+
const fuel = calculateFuel(mass)
10+
if (fuel <= 0) {
11+
return mass
12+
}
13+
14+
return mass + sumMassWithFuel(fuel)
15+
}
16+
17+
const calculateFuelRecursively = (mass) => {
18+
// For fuel calcs, exclude initial mass
19+
return sumMassWithFuel(mass) - mass
20+
}
21+
22+
module.exports = {
23+
calculateFuel,
24+
calculateFuelRecursively
25+
}
File renamed without changes.
File renamed without changes.

2019/day-01/part-1/fuel-calculator.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

2019/day-01/part-1/solution.js renamed to 2019/day-01/solution.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const fs = require('fs')
22
const path = require('path')
33
const filePath = path.join(__dirname, 'input.txt')
4-
const { calculateFuel } = require('./fuel-calculator')
4+
const { calculateFuel, calculateFuelRecursively } = require('./fuel-calculator')
55
const { parseData } = require('../../../2018/inputParser')
66

77
fs.readFile(filePath, { encoding: 'utf8' }, (err, data) => {
@@ -15,4 +15,11 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, data) => {
1515

1616
console.log('-- Part 1 --')
1717
console.log(`Answer: ${answer}`)
18+
19+
const answer2 = data.reduce((prev, cur) => {
20+
return prev + calculateFuelRecursively(parseInt(cur))
21+
}, 0)
22+
23+
console.log('-- Part 2 --')
24+
console.log(`Answer: ${answer2}`)
1825
})

0 commit comments

Comments
 (0)