function minGeppoJumps(x1, y1, x2, y2) {
//movimientos
const directions = [
[1, 2], [2, 1], [-1, 2], [-2, 1],
[1, -2], [2, -1], [-1, -2], [-2, -1]
];
const maxDistance = 300;
const xMin = Math.min(x1, x2) - maxDistance;
const xMax = Math.max(x1, x2) + maxDistance;
const yMin = Math.min(y1, y2) - maxDistance;
const yMax = Math.max(y1, y2) + maxDistance;
const queue = [[x1, y1, 0]]; // [currentX, currentY, steps]
const visited = new Set();
visited.add(`${x1},${y1}`);
while (queue.length > 0) {
const [currentX, currentY, steps] = queue.shift();
if (currentX === x2 && currentY === y2) {
return steps;
}
for (const [dx, dy] of directions) {
const nextX = currentX + dx;
const nextY = currentY + dy;
if (
nextX >= xMin &&
nextX <= xMax &&
nextY >= yMin &&
nextY <= yMax &&
!visited.has(`${nextX},${nextY}`)
) {
visited.add(`${nextX},${nextY}`);
queue.push([nextX, nextY, steps + 1]);
}
}
}
return "IMPOSSIBLE";
}
/*const input = require('fs').readFileSync(0, 'utf8').trim().split('\n');
const [x1, y1, x2, y2] = input[0].split(' ').map(Number);
console.log(minGeppoJumps(x1, y1, x2, y2));*/
console.log(minGeppoJumps(0, 0, 2, 1)); // Output: 1
//console.log(minGeppoJumps(-2, -1, 198, 99)); // Output: 100
console.log(minGeppoJumps(0, 0, 1, 1)); // Output: 2
ZnVuY3Rpb24gbWluR2VwcG9KdW1wcyh4MSwgeTEsIHgyLCB5MikgewogIC8vbW92aW1pZW50b3MKICAgIGNvbnN0IGRpcmVjdGlvbnMgPSBbCiAgICAgICAgWzEsIDJdLCBbMiwgMV0sIFstMSwgMl0sIFstMiwgMV0sCiAgICAgICAgWzEsIC0yXSwgWzIsIC0xXSwgWy0xLCAtMl0sIFstMiwgLTFdCiAgICBdOwoKICAKICAgIGNvbnN0IG1heERpc3RhbmNlID0gMzAwOyAKICAgIGNvbnN0IHhNaW4gPSBNYXRoLm1pbih4MSwgeDIpIC0gbWF4RGlzdGFuY2U7CiAgICBjb25zdCB4TWF4ID0gTWF0aC5tYXgoeDEsIHgyKSArIG1heERpc3RhbmNlOwogICAgY29uc3QgeU1pbiA9IE1hdGgubWluKHkxLCB5MikgLSBtYXhEaXN0YW5jZTsKICAgIGNvbnN0IHlNYXggPSBNYXRoLm1heCh5MSwgeTIpICsgbWF4RGlzdGFuY2U7CgogICAgCiAgICBjb25zdCBxdWV1ZSA9IFtbeDEsIHkxLCAwXV07IC8vIFtjdXJyZW50WCwgY3VycmVudFksIHN0ZXBzXQogICAgY29uc3QgdmlzaXRlZCA9IG5ldyBTZXQoKTsgCiAgICB2aXNpdGVkLmFkZChgJHt4MX0sJHt5MX1gKTsKCiAgICB3aGlsZSAocXVldWUubGVuZ3RoID4gMCkgewogICAgICAgIGNvbnN0IFtjdXJyZW50WCwgY3VycmVudFksIHN0ZXBzXSA9IHF1ZXVlLnNoaWZ0KCk7CgogICAgICAgCiAgICAgICAgaWYgKGN1cnJlbnRYID09PSB4MiAmJiBjdXJyZW50WSA9PT0geTIpIHsKICAgICAgICAgICAgcmV0dXJuIHN0ZXBzOwogICAgICAgIH0KCiAgICAgIAogICAgICAgIGZvciAoY29uc3QgW2R4LCBkeV0gb2YgZGlyZWN0aW9ucykgewogICAgICAgICAgICBjb25zdCBuZXh0WCA9IGN1cnJlbnRYICsgZHg7CiAgICAgICAgICAgIGNvbnN0IG5leHRZID0gY3VycmVudFkgKyBkeTsKCiAgICAgICAgICAKICAgICAgICAgICAgaWYgKAogICAgICAgICAgICAgICAgbmV4dFggPj0geE1pbiAmJgogICAgICAgICAgICAgICAgbmV4dFggPD0geE1heCAmJgogICAgICAgICAgICAgICAgbmV4dFkgPj0geU1pbiAmJgogICAgICAgICAgICAgICAgbmV4dFkgPD0geU1heCAmJgogICAgICAgICAgICAgICAgIXZpc2l0ZWQuaGFzKGAke25leHRYfSwke25leHRZfWApCiAgICAgICAgICAgICkgewogICAgICAgICAgICAgICAgdmlzaXRlZC5hZGQoYCR7bmV4dFh9LCR7bmV4dFl9YCk7CiAgICAgICAgICAgICAgICBxdWV1ZS5wdXNoKFtuZXh0WCwgbmV4dFksIHN0ZXBzICsgMV0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiAiSU1QT1NTSUJMRSI7Cn0KCi8qY29uc3QgaW5wdXQgPSByZXF1aXJlKCdmcycpLnJlYWRGaWxlU3luYygwLCAndXRmOCcpLnRyaW0oKS5zcGxpdCgnXG4nKTsKY29uc3QgW3gxLCB5MSwgeDIsIHkyXSA9IGlucHV0WzBdLnNwbGl0KCcgJykubWFwKE51bWJlcik7Cgpjb25zb2xlLmxvZyhtaW5HZXBwb0p1bXBzKHgxLCB5MSwgeDIsIHkyKSk7Ki8KY29uc29sZS5sb2cobWluR2VwcG9KdW1wcygwLCAwLCAyLCAxKSk7IC8vIE91dHB1dDogMQovL2NvbnNvbGUubG9nKG1pbkdlcHBvSnVtcHMoLTIsIC0xLCAxOTgsIDk5KSk7IC8vIE91dHB1dDogMTAwCmNvbnNvbGUubG9nKG1pbkdlcHBvSnVtcHMoMCwgMCwgMSwgMSkpOyAvLyBPdXRwdXQ6IDIKCgo=