xxxxxxxxxx
85
let tx = [60, 94];
let ty = [-171, -136];
// tx = [20, 30];
// ty = [-10, -5];
function setup() {
createCanvas(1, 1);
solve();
noLoop();
}
// Part 1
// Since y vel is always -1 and vel is int
// and since y pos starts at 0, with any +ve initial
// y vel, the y pos MUST pass back to 0 with vel at -1 * initial vel - 1 (grav)
// Due to constraints on the target area, the lowest we can hit is at -171
// If we know we are passing through 0 again with (-1 * initial vel) - 1
// and hitting the max of -171, then working backwards, the vel is -170 before
// passing through 0 and there for 170 is initial vel;
function solve() {
// part 1
// maxYPos(170);
let xrange = [0, tx[1]];
let yrange = [ty[0], ty[0] * -1];
let num = 0;
for(let vx = xrange[0]; vx <= xrange[1]; vx ++) {
for(let vy = yrange[0]; vy <= yrange[1]; vy ++) {
// print(vx, vy);
if(hits(vx, vy)) {
// print("hit!");
num ++;
}
}
}
print(num);
}
function hits(vx, vy) {
let x = 0;
let y = 0;
while(!pastTarget(x, y)) {
x += vx;
y += vy;
if(vx > 0) {
vx -=1;
}
vy -= 1;
if(inTarget(x, y)) {
return true;
}
}
return false;
}
function inTarget(x, y) {
return x >= tx[0] && x <= tx[1] && y >= ty[0] && y <= ty[1];
}
function pastTarget(x, y) {
return x > tx[1] || y < ty[0];
}
function maxYPos(vy) {
let p = 0;
while(vy >= 0) {
p += vy;
vy -= 1;
}
print(p);
}