Onboarding – CodinGame solution in C
Each turn, read the two enemy names and their distances, then print the name of the enemy with the smaller distance. Repeat forever inside the game loop; the referee ends the program when the game is over. Converting the distances to integers before comparing them is the only real trap.
The problem
Onboarding is CodinGame's introductory puzzle. Your cannon faces a stream of enemy ships, and on every turn two of them are announced: the name of the first, its distance, the name of the second and its distance, each on its own line.
You answer with the name of the ship to destroy this turn. If any ship reaches you, you lose, so the rule is simply to shoot the nearest one every time.
Approach, step by step
- Write the infinite game loopAll bot-style puzzles work the same way: an endless loop where each iteration reads one turn of input and prints exactly one command. Learning this pattern here pays off in every later game puzzle.
- Read four values per turnRead a name, a distance, a name, a distance. Convert the distances to numbers so the comparison is numeric and not alphabetical.
- Compare and printPrint the name whose distance is smaller. A ternary expression (or Python's conditional expression) does this in one line. When both distances are equal, either name is accepted, so there is no tie rule to worry about.
- Print exactly one line per turnEnd the answer with a newline and print nothing else on standard output. The referee reads one line per turn; a missing line causes a timeout and an extra one is taken as next turn's answer.
Complexity
O(1) per turnO(1)Each turn is one comparison between two numbers. Nothing is stored between turns.
Solution code
Onboarding – C
Uses scanf("%s %d %s %d") to read the four values at once; scanf skips newlines, so the line-per-value format is no problem. The 256-byte buffers are far larger than any enemy name.
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv)
{
while (1) {
char enemy1[256], enemy2[256];
int dist1, dist2;
scanf("%s %d %s %d", enemy1, &dist1, enemy2, &dist2);
if (dist1 < dist2)
printf("%s\n",enemy1);
else
printf("%s\n",enemy2);
}
return EXIT_SUCCESS;
}
Onboarding – Java
Scanner.next() and nextInt() read tokens regardless of line breaks, and the ternary operator picks the name inline.
import java.util.*;
class Player {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
while (true) {
String enemy1 = in.next();
int dist1 = in.nextInt();
String enemy2 = in.next();
int dist2 = in.nextInt();
System.out.println(dist1 < dist2 ? enemy1 : enemy2);
}
}
}
Onboarding – Python 3
input() returns one line at a time, so the four values are read with four calls, converting the distances with int().
# Bot puzzles run forever: one iteration = one turn, the referee stops the program.
while 1:
# Four separate lines per turn: name, distance, name, distance.
# The distances must be converted to int, otherwise "10" < "9" would be compared as text.
enemy1, dist1, enemy2, dist2 = input(), int(input()), input(), int(input())
# Shoot the closer enemy (on a tie either one is fine).
print(enemy1 if dist1 < dist2 else enemy2)
Common mistakes
- Comparing distances as stringsIn Python, input() returns text, and "10" < "9" is True alphabetically. Without int() the cannon targets the farther ship and loses on the first test where distances have different numbers of digits.
- Printing the distance or an indexThe referee expects the enemy's name exactly as received. Printing 1 or 2, or the distance, is rejected as an invalid target.
- Reading everything on one lineIn Python, input().split() on the first line only returns the first name. Each value has its own line, so read them one by one.
- Adding extra outputDebug prints on standard output are read as commands. Send debug messages to standard error (stderr) instead.
Going further
Onboarding looks trivial, but it teaches the contract every CodinGame bot puzzle follows: the referee writes a turn, waits for exactly one line from you, then writes the next turn. If you print two lines, the second one is consumed as your answer to the next turn and everything drifts by one; if you print nothing, you time out. Keeping debug output on stderr (System.err in Java, sys.stderr in Python, fprintf(stderr, ...) in C) is the habit that saves the most time on The Descent, Power of Thor and every multiplayer bot afterwards.
FAQ
What is the Onboarding puzzle on CodinGame?
It is the tutorial puzzle that introduces the game loop: each turn you get two enemies with their distances and must print the name of the closer one.
Why does my Onboarding code shoot the wrong ship?
Most often the distances are compared as text. Convert them to integers before comparing, otherwise "10" is treated as smaller than "9".
How do I debug without breaking the output?
Print debug messages to standard error. The referee only reads standard output, so stderr lines show up in the console without being treated as commands.