Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Escape by Sim0000
import assert from "assert";
function escape(jar: number[], fly: number[]): boolean {
const [W, H, d] = jar;
let [x, y, vx, vy] = fly;
if(vx == 0 && vy && Math.abs(2 * x - W) < d) return true;
if(vx == 0 || vy == 0) return false;
for(let i = 0; i < 20; i++){
let x2 = vx >= 0 ? W : 0;
let y2 = y + vy * (x2 - x) / vx;
if(y2 < 0 || H < y2){
y2 = vy >= 0 ? H : 0;
x2 = x + vx * (y2 - y) / vy;
}
if(vy > 0 && Math.abs(2 * x2 - W) < d) return true;
[x, y] = [x2, y2];
if(x == 0 || x == W) vx = -vx;
if(y == 0 || y == H) vy = -vy;
}
return false;
}
console.log('Example:');
console.log(escape([1000, 500, 200], [0, 0, 100, 0]));
// These "asserts" are used for self-checking
assert.equal(escape([1000, 500, 200], [0, 0, 100, 0]), false);
assert.equal(escape([1000, 500, 200], [450, 50, 0, -100]), true);
assert.equal(escape([1000, 1000, 200], [450, 1000, 100, 0]), false);
assert.equal(escape([1000, 1000, 200], [250, 250, -10, -50]), false);
assert.equal(escape([1000, 2000, 200], [20, 35, 100, 175]), true);
console.log("Coding complete? Click 'Check' to earn cool rewards!");
Dec. 20, 2020
Comments: