Mars Lander - Episode 1 – CodinGame solution in C
Keep the lander vertical (rotation 0) and decide each turn between thrust 0 and thrust 4. The robust way is to simulate: if you wait one more turn and then ramp the thrust up to 4 (it can only rise by 1 per turn), will the vertical speed still be above -40 m/s when you reach the ground? If yes, keep free-falling; if not, output thrust 4. A simpler rule, full thrust whenever the vertical speed drops below about -37 m/s, also passes all tests.
The problem
A lander starts above a flat landing zone on Mars and must touch down safely. Each turn you receive its position, horizontal and vertical speed, remaining fuel, rotation and thrust power, and you answer with a rotation angle and a thrust power between 0 and 4.
In this first episode only the vertical motion matters: you land directly below, rotation must be 0 at touchdown and the vertical speed must not exceed 40 m/s. Mars gravity is 3.711 m/s², each unit of thrust gives 1 m/s² upwards and costs 1 litre of fuel per turn, and the thrust can change by at most 1 per turn, so your command always takes a few turns to take full effect.
Approach, step by step
- Find the ground altitudeRead the surface points once and find the segment that contains the lander's X coordinate; its Y is the altitude you will land on. Since the lander falls straight down, this never changes.
- Model one turnEach turn the actual power moves by at most 1 towards the requested value, then speed += power - 3.711 and altitude += speed. With power 4 the net effect is only +0.289 m/s per turn, so braking is slow.
- Simulate 'wait one more turn'From the current state, simulate one turn with power 0, then the ramp 1, 2, 3, 4, then constant 4 until either the ground is reached or the speed stops increasing in magnitude.
- DecideIf the simulated speed at the ground is still within -40 m/s, it is safe to wait: output '0 0'. Otherwise start braking now with '0 4'. Waiting as long as possible is what saves fuel.
- Keep rotation at 0Never output a rotation other than 0: the landing check requires a vertical lander, and any tilt also introduces horizontal speed you would have to cancel.
Complexity
O(H) per turn for the simulation, O(1) with the closed formO(N) for the surfaceThe C version simulates turn by turn until the ground (a few dozen iterations), while the Java and Python versions replace that loop with the arithmetic series of the constant-thrust phase.
Solution code
Mars Lander - Episode 1 – C
Literal simulation: apply 5 turns of ramping thrust, then add +0.289 m/s per turn until the lander reaches the ground or stops falling, and check the final speed against -40.
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv)
{
int N;
scanf("%d\n", &N);
int surfaceX[N];
int surfaceY[N];
for (int i = 0; i < N; i++)
scanf("%d %d\n", &surfaceX[i], &surfaceY[i]);
while (1) {
int X, Y, dX, dY, fuel, rotate, power;
scanf("%d %d %d %d %d %d %d", &X, &Y, &dX, &dY, &fuel, &rotate, &power);
// Finds landing altitude by Looking for 2 consecutive points w/ same Y
int groundY = -1;
for (int i = 0; (i < N && groundY == -1); i++)
if (surfaceX[i] <= X && X <= surfaceX[i+1])
groundY = surfaceY[i];
/* Finds out if it is safe to start braking only next turn, by
simulating it and analyzing vertical speed at landing
If it is safe, waits, else starts braking*/
double vdY = (double)dY;
double vY = (double)Y;
for (double i = 0.0; i <= 4; i++) {
vdY += i - 3.711;
vY += vdY;
}
while (vY > groundY && vdY < 0) {
vdY += 0.289; // 4-3.711;
vY += vdY;
}
printf((vdY >= 0 || vdY > -40.0) ? "0 0\n" : "0 4\n");
}
return EXIT_SUCCESS;
}
Mars Lander - Episode 1 – Java
Same model in closed form: after the 5 ramp turns the speed is dY - 8.555 and the altitude Y - 36.665 + 5*dY, then the arithmetic series gives where the speed reaches -40; Math.round is used for the turn count.
import java.util.*;
import java.math.*;
class Player {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int[] surfaceX = new int[N];
int[] surfaceY = new int[N];
for (int i = 0; i < N; i++) {
surfaceX[i] = in.nextInt();
surfaceY[i] = in.nextInt();
}
while (true) {
int X = in.nextInt();
int Y = in.nextInt();
int dX = in.nextInt();
int dY = in.nextInt();
in.nextInt(); in.nextInt(); in.nextInt();
// Finds landing altitude by Looking for 2 consecutive points w/ same Y
int groundY = -1;
for (int i = 0; (i < N && groundY == -1); i++)
if (surfaceX[i] <= X && X <= surfaceX[i+1])
groundY = surfaceY[i];
/* Finds out if it is safe to start braking only next turn, by
simulating it and analyzing vertical speed at landing
If it is safe, waits, else starts braking*/
double vdY = dY - 8.555;
double vY = Y - 36.665 + 5*dY;
long t = Math.round((-40-vdY)/0.289);
if (vY + t*(vdY+0.289*(1+t)/2) > groundY)
System.out.println("0 0");
else
System.out.println("0 4");
}
}
}
Mars Lander - Episode 1 – Python 3
Identical closed form to Java but rounds the number of turns up with math.ceil, which is slightly more conservative.
import math
N = int(input())
surfaceX, surfaceY = [], []
for i in range(N):
landX, landY = [int(j) for j in input().split()]
surfaceX.append(landX)
surfaceY.append(landY)
while 1:
X, Y, dX, dY, fuel, rotate, power = [int(i) for i in input().split()]
# Finds landing altitude by Looking for 2 consecutive points w/ same Y
groundY, i = -1, 0
while (i < N) and (groundY == -1):
if (surfaceX[i] <= X) and (X <= surfaceX[i+1]):
groundY = surfaceY[i]
i+=1
# Finds out if it is safe to start braking only next turn, by
# simulating it and analyzing vertical speed at landing
# If it is safe, waits, else starts braking
vdY , vY = dY-8.555, Y-36.665+5*dY;
t = math.ceil((-40-vdY)/0.289);
if (vY + t*(vdY+0.289*(1+t)/2) > groundY):
print(0, 0)
else:
print(0, 4)
Common mistakes
- Assuming thrust is instantRequesting power 4 when the speed hits -40 is too late: the power goes 1, 2, 3, 4 over the next turns while gravity keeps accelerating you, so the speed overshoots by several m/s. Leave a margin or simulate the ramp.
- Braking from the first turnFull thrust from the start lands safely but burns fuel for many useless turns; on tests with little fuel the tank empties mid-air and you crash. Brake late, not early.
- Trusting the rounded speedThe speeds you receive are rounded integers while the game checks the real value. A controller tuned to land at exactly -40 can fail at -40.4; aim for a couple of m/s of margin.
- Not reading every input valueEach turn sends 7 integers. Skipping the unused ones (fuel, rotation, power) desynchronises the input and the lander reacts to wrong numbers on the next turn.
Going further
There is a neat argument for why braking as late as possible is optimal. Over T turns the change of vertical speed is the sum of the powers minus 3.711*T, so the fuel spent equals (v_final - v_start) + 3.711*T. To minimise fuel you therefore want to land as fast as allowed (v_final close to -40) and spend as few turns as possible in the air, which means free-falling first and then braking at full power at the last safe moment. That is exactly the 'bang-bang' policy implemented here, and the same prediction routine is reused in episodes 2 and 3 once the horizontal motion is under control. In practice the one-line threshold rule ends up close in fuel, because once the power is at 4 it oscillates around the limit, but it gives you no guarantee when the starting speed is already high.
FAQ
What is the simplest Mars Lander Episode 1 solution?
Output '0 4' when the vertical speed is below about -37 m/s and '0 0' otherwise. The margin compensates for the thrust ramping up by only 1 per turn.
Why can't I just set the power to the value I need?
The game lets the power change by at most 1 per turn, so the value you send is a target. You have to anticipate the few turns needed to reach it.
Do I need the surface points in episode 1?
Not strictly, since the lander is already above the flat zone, but using the ground altitude lets you predict the landing speed precisely instead of relying on a fixed threshold.