Skip to content

Instantly share code, notes, and snippets.

@kYroL01
Created August 9, 2016 13:58
Show Gist options
  • Save kYroL01/eeb7752ae71f6ae58a095ad74e989986 to your computer and use it in GitHub Desktop.
Save kYroL01/eeb7752ae71f6ae58a095ad74e989986 to your computer and use it in GitHub Desktop.
Solution for game "The Descent" on CodingGame
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/**
* The while loop represents the game.
* Each iteration represents a turn of the game
* where you are given inputs (the heights of the mountains)
* and where you have to print an output (the index of the moutain to fire on)
* The inputs you are given are automatically updated according to your last actions.
**/
int main()
{
int max_height, idx_height;
// game loop
while (1) {
max_height = 0;
idx_height = 0;
for (int i = 0; i < 8; i++) {
int mountainH; // represents the height of one mountain.
scanf("%d", &mountainH);
if(mountainH > max_height) {
max_height = mountainH;
idx_height = i;
}
}
// Write an action using printf(). DON'T FORGET THE TRAILING \n
// To debug: fprintf(stderr, "Debug messages...\n");
printf("%d\n", idx_height); // The index of the mountain to fire on.
}
return 0;
}
@kYroL01
Copy link
Author

kYroL01 commented Oct 11, 2023

Hi @engrhafizmaqsood
I think your version is a bit inefficient because you're using 2 for inside the while(1) .
Basically is replicated code. You can do the same in one loop :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment