Skip to content

Instantly share code, notes, and snippets.

@croraf
Created October 22, 2020 19:54
Show Gist options
  • Save croraf/969346dd4c40323080b9973f7375b073 to your computer and use it in GitHub Desktop.
Save croraf/969346dd4c40323080b9973f7375b073 to your computer and use it in GitHub Desktop.
Mars Lander - episode 2
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
const points = [];
const surfaceN = parseInt(readline()); // the number of points used to draw the surface of Mars.
for (let i = 0; i < surfaceN; i++) {
var inputs = readline().split(' ');
const landX = parseInt(inputs[0]); // X coordinate of a surface point. (0 to 6999)
const landY = parseInt(inputs[1]); // Y coordinate of a surface point. By linking all the points together in a sequential fashion, you form the surface of Mars.
points.push({ x: landX, y: landY });
}
let landingSurface;
let previousPoint = points[0];
for (let i = 1; i < points.length; i++) {
if (previousPoint.y === points[i].y) {
landingSurface = [previousPoint, points[i]];
break;
}
previousPoint = points[i];
}
console.error(landingSurface);
const landingPoint = {
x: (landingSurface[0].x + landingSurface[1].x) / 2,
y: (landingSurface[0].y + landingSurface[1].y) / 2,
}
console.error(landingPoint);
let phase = 'StabilizationHorizontal';
const horizontalBreakingDistanceCalc = (hSpeed, maxHorizontalAcc) => {
const horizontalBreakingTime = hSpeed / maxHorizontalAcc;
const horizontalBreakingDistance = maxHorizontalAcc / 2 * horizontalBreakingTime * horizontalBreakingTime;
return horizontalBreakingDistance;
}
// game loop
let maxAngle
while (true) {
var inputs = readline().split(' ');
const x = parseInt(inputs[0]);
const y = parseInt(inputs[1]);
const hSpeed = parseInt(inputs[2]); // the horizontal speed (in m/s), can be negative.
const vSpeed = parseInt(inputs[3]); // the vertical speed (in m/s), can be negative.
const fuel = parseInt(inputs[4]); // the quantity of remaining fuel in liters.
const rotate = parseInt(inputs[5]); // the rotation angle in degrees (-90 to 90).
const power = parseInt(inputs[6]); // the thrust power (0 to 4).
let targetRotation;
let targetThrust;
const relativeHorizontalDistance = landingPoint.x - x;
const relativeVerticalDistance = y - landingPoint.y;
// approximate max angle allowed
// 25 allows falling
// 23 approximate 0 vertical gravity
// 16 rises the vessel a bit
if (!maxAngle) { maxAngle = relativeVerticalDistance > 2000 ? 25 : 16; }
const maxHorizontalAcc = 4 * Math.sin(maxAngle * Math.PI / 180);
console.error(maxHorizontalAcc);
let horizontalBreakingDistance = horizontalBreakingDistanceCalc(Math.abs(hSpeed), maxHorizontalAcc);
if (Math.abs(hSpeed) < 3 && Math.abs(relativeHorizontalDistance) <= 200) {
console.error('//horizontally adjusted, proceede with landing');
targetRotation = 0;
if (vSpeed < -39) {
targetThrust = 4;
} else {
targetThrust = 3;
targetRotation = 0;
}
} else if (hSpeed > 0 && relativeHorizontalDistance <= 0) {
console.error('//speed right, target left, turn left');
targetRotation = maxAngle;
targetThrust = 4;
} else if (hSpeed < 0 && relativeHorizontalDistance > 0) {
console.error('//speed left, target right, turn right');
targetRotation = -maxAngle;
targetThrust = 4;
} else if (relativeHorizontalDistance >= 0) {
console.error('//target right');
if (horizontalBreakingDistance < relativeHorizontalDistance) {
console.error('//target far right, we need to accelerate right');
console.error('//turn right');
targetRotation = -maxAngle;
targetThrust = 4;
} else {
console.error('//we need to decelerate right');
console.error('//turn left');
targetRotation = maxAngle;
targetThrust = 4;
}
} else if (relativeHorizontalDistance < -0) {
console.error('//target left');
if (horizontalBreakingDistance < Math.abs(relativeHorizontalDistance)) {
console.error('//target far left, we need to accelerate left');
console.error('//turn left');
targetRotation = maxAngle;
targetThrust = 4;
} else {
console.error('//we need to decelerate left');
console.error('//turn right');
targetRotation = -maxAngle;
targetThrust = 4;
}
}
console.log(`${targetRotation} ${targetThrust}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment