Onboarding


Most simple puzzle : you have to shoot down all enemies coming at you before they reach you. Each turn you have the choice between two enemies, so you just have to compare the distance that separates each one from you, and shoot the closest one.

C

#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;
}
            

Java

In C and Java, the if then else construct can be replaced by a conditional assignment thanks to the ternary operator ? :, as shown in the following code snippet.

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);
        }
    }
}
            

Python 3

Since version 2.5, Python also has a ternary operator, only with a different order.

while 1:
    enemy1, dist1, enemy2, dist2 = input(), int(input()), input(), int(input())
    print(enemy1 if dist1 < dist2 else enemy2)