Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Last active December 3, 2019 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazy4groovy/092b88dfdf520dad502ba158c7c0da79 to your computer and use it in GitHub Desktop.
Save crazy4groovy/092b88dfdf520dad502ba158c7c0da79 to your computer and use it in GitHub Desktop.
function main(input) {
const lines = input.split('\n');
const results = lines.map(n => Math.floor(Number(n) / 3) - 2)
const total = results.reduce((sum, n) => sum + n, 0);
console.log(total); // <<<<<<<<<<<<<<<<<
}
main(`120150
105328
70481
86674
112434
94883
147500
146007
103982
65758
132357
60885
97516
96977
129085
80330
124081
102501
102505
70029
54155
69253
60120
53192
89470
137125
136296
104546
92859
74937
135044
66238
126678
86364
138559
82393
96947
107749
115332
117563
95431
99640
107667
120427
108389
51567
57493
68518
114565
107248
50627
122517
129687
118989
52459
83726
106765
75872
147111
78822
65058
142460
122496
148942
72753
141599
105711
141860
134066
116716
107455
56673
148238
92318
86652
69312
91352
94528
73441
137814
80247
101115
61773
100951
77189
119083
93841
109090
83370
70230
144812
67647
105358
135780
85225
100697
100998
131151
52826
114084`);
var calcFuel = mass => Math.floor(Number(mass) / 3) - 2;
function main(input) {
const lines = input.split('\n').map(n => Number(n));
const results = lines.map(mass => {
let fuel = calcFuel(mass)
mass = fuel
while (fuel > 0) {
fuel = Math.max(0, calcFuel(fuel))
mass += fuel
}
return mass
})
const total = results.reduce((sum, n) => sum + n, 0);
console.log(total); // <<<<<<<<<<<<<<<<<
}
main(`120150
105328
70481
86674
112434
94883
147500
146007
103982
65758
132357
60885
97516
96977
129085
80330
124081
102501
102505
70029
54155
69253
60120
53192
89470
137125
136296
104546
92859
74937
135044
66238
126678
86364
138559
82393
96947
107749
115332
117563
95431
99640
107667
120427
108389
51567
57493
68518
114565
107248
50627
122517
129687
118989
52459
83726
106765
75872
147111
78822
65058
142460
122496
148942
72753
141599
105711
141860
134066
116716
107455
56673
148238
92318
86652
69312
91352
94528
73441
137814
80247
101115
61773
100951
77189
119083
93841
109090
83370
70230
144812
67647
105358
135780
85225
100697
100998
131151
52826
114084`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment