-
Notifications
You must be signed in to change notification settings - Fork 17
/
launcher.js
52 lines (41 loc) · 865 Bytes
/
launcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function launch(initial_xv, initial_yv, input) {
let xv = initial_xv;
let yv = initial_yv;
let x = 0;
let y = 0;
const lowest_y = Math.min(...input.y);
let max_y = y;
while (y > lowest_y) {
x += xv;
y += yv;
if (y > max_y) {
max_y = y;
}
xv += xv === 0 ? 0 : xv > 0 ? -1 : 1;
yv--;
if (x >= input.x[0] && x <= input.x[1] && y >= input.y[0] && y <= input.y[1]) {
return {
max_y,
xv: initial_xv,
yv: initial_yv,
};
}
}
}
function getValidTrajectories(input) {
const lowest_y = Math.min(...input.y);
const farthest_x = Math.max(...input.x);
let solutions = [];
for (let x = 0; x <= farthest_x + 1; x++) {
for (let y = lowest_y; y <= 1000; y++) {
let landed = launch(x, y, input);
if (landed) {
solutions.push(landed);
}
}
}
return solutions;
}
module.exports = {
getValidTrajectories,
};