Skip to content

Commit dc1eb3a

Browse files
Merge pull request #41 from amclin/feature/2019-day-01
Feature/2019 day 01
2 parents 8709351 + e7a0d0f commit dc1eb3a

File tree

5 files changed

+187
-1
lines changed

5 files changed

+187
-1
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+
}

2019/day-01/fuel-calculator.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-env mocha */
2+
const expect = require('chai').expect
3+
const { calculateFuel, calculateFuelRecursively } = require('./fuel-calculator')
4+
5+
describe('--- 2019 Day 1: The Tyranny of the Rocket Equation ---', () => {
6+
describe('Part 1', () => {
7+
describe('calculateFuel()', () => {
8+
it('calculates the fuel amount for a specified mass', () => {
9+
const inputs = [12, 14, 1969, 100756]
10+
const outputs = [2, 2, 654, 33583]
11+
12+
inputs.forEach((input, idx) => {
13+
expect(calculateFuel(input)).equals(outputs[idx])
14+
})
15+
})
16+
it('will not output negative quantities for fuel', () => {
17+
const inputs = [0, 1, -2345]
18+
const outputs = [0, 0, 0]
19+
20+
inputs.forEach((input, idx) => {
21+
expect(calculateFuel(input)).equals(outputs[idx])
22+
})
23+
})
24+
})
25+
describe('calculateFuelRecursively()', () => {
26+
it('calculates fuel amount for a specified mass, along with the fuel for the fuel', () => {
27+
const inputs = [14, 1969, 100756]
28+
const outputs = [2, 966, 50346]
29+
30+
inputs.forEach((input, idx) => {
31+
expect(calculateFuelRecursively(input)).equals(outputs[idx])
32+
})
33+
})
34+
})
35+
})
36+
})

2019/day-01/input.txt

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
114739
2+
132732
3+
123925
4+
146385
5+
72590
6+
51932
7+
64164
8+
72919
9+
97336
10+
101791
11+
101477
12+
95677
13+
89907
14+
76998
15+
60782
16+
109021
17+
138070
18+
113374
19+
68672
20+
133830
21+
134079
22+
89079
23+
72321
24+
142643
25+
102734
26+
103743
27+
77864
28+
111363
29+
72140
30+
64483
31+
139567
32+
141318
33+
132736
34+
87824
35+
81937
36+
123404
37+
56730
38+
54447
39+
63602
40+
84561
41+
145159
42+
93598
43+
137770
44+
52943
45+
146307
46+
91674
47+
143183
48+
52543
49+
143052
50+
107171
51+
80699
52+
130825
53+
141406
54+
75771
55+
123497
56+
87982
57+
144545
58+
125153
59+
147925
60+
106898
61+
60193
62+
111251
63+
102454
64+
135886
65+
141408
66+
84556
67+
94143
68+
97958
69+
86474
70+
96848
71+
126314
72+
51037
73+
140379
74+
142065
75+
73629
76+
102432
77+
123801
78+
68146
79+
107720
80+
51512
81+
123873
82+
104308
83+
59601
84+
149175
85+
80400
86+
76822
87+
116115
88+
87990
89+
90592
90+
68688
91+
133868
92+
78357
93+
91425
94+
149328
95+
123010
96+
93278
97+
101150
98+
120352
99+
107642
100+
136050

2019/day-01/solution.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const filePath = path.join(__dirname, 'input.txt')
4+
const { calculateFuel, calculateFuelRecursively } = require('./fuel-calculator')
5+
const { parseData } = require('../../../2018/inputParser')
6+
7+
fs.readFile(filePath, { encoding: 'utf8' }, (err, data) => {
8+
if (err) throw err
9+
10+
data = parseData(data.trim())
11+
12+
const answer = data.reduce((prev, cur) => {
13+
return prev + calculateFuel(parseInt(cur))
14+
}, 0)
15+
16+
console.log('-- Part 1 --')
17+
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}`)
25+
})

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
require('./2018/day-14/solution')
1+
require('./2019/day-01/part-1/solution')

0 commit comments

Comments
 (0)