Skynet: The Chasm – CodinGame solution in C
The minimum speed that clears a gap of G cells is G + 1, and keeping exactly that speed makes stopping on the landing platform easy. Each turn: SLOW if you are past the gap or faster than G + 1, SPEED if you are slower than G + 1, JUMP when a jump from your current cell lands beyond the gap (x + speed >= road + gap), and WAIT otherwise.
The problem
A motorbike rides on a straight road of R cells, followed by a gap of G cells and a landing platform of L cells. At the start you get these three lengths; then every turn you receive the current speed and position of the bike and answer SPEED, SLOW, JUMP or WAIT.
Speed changes by one per command and the bike moves by its speed each turn. The bike must clear the gap with a jump and then come to a full stop (speed 0) before the end of the landing platform.
Approach, step by step
- Pick the target speedA jump from the last road cell (x = R - 1) lands at x + speed, so it reaches the platform when speed >= G + 1. Aim for exactly G + 1: any extra speed only makes braking on a short platform harder.
- Brake after the gapOnce x >= R + G the bike is on the platform: answer SLOW every turn until it stops. The same SLOW command also handles the case where the bike starts faster than G + 1.
- Accelerate before the gapWhile the speed is G or less, answer SPEED. The road is long enough in every test to build up the needed speed.
- Jump at the right momentWith the speed equal to G + 1, jump as soon as x + speed >= R + G, which at that speed means x = R - 1, the last cell before the gap. Otherwise WAIT and keep the speed.
Complexity
O(1) per turnO(1)Each turn is a handful of comparisons on the current speed and position; nothing is stored between turns.
Solution code
Skynet: The Chasm – C
The C, Java and Python versions share the same ordered chain of conditions. Order matters: the "past the gap or too fast" test comes first so the bike always brakes on the platform, before the speed check could push it to accelerate.
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv)
{
int before, gap, after;
scanf("%d%d%d\n", &before, &gap, &after);
for (;;) {
// x = motorbike abscissa ; dx = motorbike speed
int dx, x;
scanf("%d%d", &dx, &x);
// if the bike has passed the gap, or going faster than needed to jump over it
/* past the gap: brake to 0 on the platform; too fast: lose speed before the jump */
if (x >= before+gap || dx > gap+1)
printf("SLOW\n");
// if the bike isn't going fast enough to jump over the gap
else if (dx <= gap)
printf("SPEED\n");
// if the bike can land after the gap by jumping
/* here dx == gap+1, so the jump lands on the first platform cell or later */
else if (x+dx >= before+gap)
printf("JUMP\n");
else
printf("WAIT\n");
}
return EXIT_SUCCESS;
}
Skynet: The Chasm – Java
Same decision chain as the C code, reading with a Scanner and printing one command per turn. The variable names before, gap and after map to road, gap and platform.
import java.util.*;
class Player {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int before = in.nextInt();
int gap = in.nextInt();
int after = in.nextInt();
while (true) {
// x = motorbike abscissa ; dx = motorbike speed
int dx = in.nextInt();
int x = in.nextInt();
// if the bike isn't going fast enough to jump over the gap
if ((x >= before + gap) || (dx > gap + 1))
System.out.println("SLOW");
// if the bike is going slower than needed : speed
else if (dx <= gap)
System.out.println("SPEED");
// if the bike can land after the gap : jump
else if (x + dx >= before + gap)
System.out.println("JUMP");
else
System.out.println("WAIT");
}
}
}
Skynet: The Chasm – Python 3
Same logic in Python; inputs come one value per line, so each turn reads two int(input()) calls, speed first and then position.
# road length, gap length, landing platform length (one value per line)
before, gap, after = int(input()), int(input()), int(input())
while 1:
# x = motorbike abscissa ; dx = motorbike speed
dx, x = int(input()), int(input())
# if the bike is after the gap, or going faster than needed : slow
# minimum speed to clear the gap is gap + 1: never go faster than that
if (x >= before + gap) or (dx > gap+1):
print("SLOW")
# if the bike isn't going fast enough to jump over the gap
elif (dx <= gap):
print("SPEED")
# if the bike can land after the gap by jumping
# at speed gap + 1 this is true only on the last road cell, x = before - 1
elif (x+dx >= before+gap):
print("JUMP")
else:
print("WAIT")
Common mistakes
- Jumping too earlyAnswering JUMP as soon as the speed is right, wherever the bike is, lands it inside the gap. The jump condition must involve the position: x + speed has to reach R + G.
- Going faster than neededAccelerating to the maximum seems safe for the jump, but the bike then needs many turns to brake and overshoots short landing platforms. Cap the speed at G + 1.
- Checking the speed before the positionIf "speed too low, accelerate" is tested before "already past the gap", the bike keeps accelerating on the platform. Put the after-the-gap test first.
- Reading the inputs on one lineRoad, gap and platform come on three separate lines, and so do speed and position each turn. A Python input().split() on one line raises an error on the first turn.
Going further
The only subtle condition is the jump, and writing it as x + speed >= R + G rather than x == R - 1 makes the rule explain itself: a jump is worth it exactly when its landing point is past the gap. At the target speed G + 1 both formulations are identical, but the general one also tells you why the tests are designed the way they are: at that speed the bike only has one cell from which a jump succeeds, so the official cases are built so that the bike, accelerating one unit per turn, passes through R - 1. If you want a more robust bot, compute ahead how many turns of acceleration and waiting bring you exactly to R - 1, or accept a slightly higher speed so that more cells qualify, provided the platform is long enough to brake: stopping from speed s takes s turns and covers roughly s(s - 1) / 2 more cells. This is a nice warm-up for Skynet: The Bridge, where several bikes and holes turn the same reasoning into a search.
FAQ
What speed do I need to jump the chasm?
At least gap + 1, because the jump starts from the last road cell and must land on the first platform cell or beyond. Keeping exactly gap + 1 is safest for braking.
When should I send JUMP?
When the current position plus the current speed reaches road + gap. At speed gap + 1 that happens on the last road cell, x = road - 1.
Why does my bike fall off the end of the platform?
It is going too fast. Each SLOW reduces the speed by one only, so limit the speed to gap + 1 before the jump and brake every turn once you have landed.