Skip to content

Instantly share code, notes, and snippets.

@pengx17
Created December 21, 2021 14:30
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 pengx17/36884998bf286bba85ce7feb94bd93c2 to your computer and use it in GitHub Desktop.
Save pengx17/36884998bf286bba85ce7feb94bd93c2 to your computer and use it in GitHub Desktop.
const assert = require("assert");
let xrange = [241, 273];
let yrange = [-97, -63];
function check([x, y], [vx, vy], maxHeight = 0, inShoot = false) {
[x, y] = [x + vx, y + vy];
// console.log([x, y], [vx, vy], maxHeight, inShoot);
vx = vx > 0 ? vx - 1 : vx < 0 ? vx + 1 : 0;
vy--;
if (y > maxHeight) {
maxHeight = y;
}
if (x >= xrange[0] && x <= xrange[1] && y >= yrange[0] && y <= yrange[1]) {
inShoot = true;
}
// if x is beyond range and not finish
if ((vx <= 0 && x < xrange[0]) || (vx >= 0 && x > xrange[1])) {
return inShoot ? maxHeight : null;
}
if (vy < 0 && y < yrange[0]) {
return inShoot ? maxHeight : null;
}
return check([x, y], [vx, vy], maxHeight, inShoot);
}
function run([vx, vy], expected) {
const res = check([0, 0], [vx, vy]);
console.log(res);
assert(res === expected);
}
// run([17, -4], null);
// run([7, 2], 3);
// run([6, 3], 6);
// run([6, 9], 45);
let max = yrange[1];
let velocities = [];
for (let vx = 0; vx <= xrange[1]; vx++) {
for (let vy = -1000; vy < 1000; vy++) {
const h = check([0, 0], [vx, vy]);
if (h != null) {
max = Math.max(h, max);
console.log([vx, vy], h);
velocities.push([vx, vy]);
}
}
}
console.log(max, velocities.length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment