From e8bac6e221215f9450d332754d8e3c61ebed8939 Mon Sep 17 00:00:00 2001 From: Arkagedon <38785328+ARKAGEDON@users.noreply.github.com> Date: Thu, 27 Jan 2022 11:31:40 +0100 Subject: [PATCH] Adding the first coding game Challenges --- Easy_Challenges/ascii_art.c | 66 +++++++++++++++++++++++ Easy_Challenges/chuck_norris.c | 50 +++++++++++++++++ Easy_Challenges/mars_landers_1.c | 49 +++++++++++++++++ Easy_Challenges/power_of_thor_1.c | 49 +++++++++++++++++ Easy_Challenges/temperatures.c | 40 ++++++++++++++ Easy_Challenges/the_descent.c | 27 ++++++++++ Medium_Challenges/shadow_of_knight_1.c | 42 +++++++++++++++ Medium_Challenges/stock_exchange_losses.c | 20 +++++++ Medium_Challenges/there_is_no_spoon_1.c | 64 ++++++++++++++++++++++ README.md | 15 +++++- 10 files changed, 421 insertions(+), 1 deletion(-) create mode 100644 Easy_Challenges/ascii_art.c create mode 100644 Easy_Challenges/chuck_norris.c create mode 100644 Easy_Challenges/mars_landers_1.c create mode 100644 Easy_Challenges/power_of_thor_1.c create mode 100644 Easy_Challenges/temperatures.c create mode 100644 Easy_Challenges/the_descent.c create mode 100644 Medium_Challenges/shadow_of_knight_1.c create mode 100644 Medium_Challenges/stock_exchange_losses.c create mode 100644 Medium_Challenges/there_is_no_spoon_1.c diff --git a/Easy_Challenges/ascii_art.c b/Easy_Challenges/ascii_art.c new file mode 100644 index 0000000..f174f4c --- /dev/null +++ b/Easy_Challenges/ascii_art.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include + + +int main() +{ + int L; + scanf("%d", &L); + int H; + scanf("%d", &H); fgetc(stdin); + char tab[L][1025]; + char T[257]; + scanf("%[^\n]", T); fgetc(stdin); + for (int i = 0; i < H; i++) { + char ROW[1025]; + scanf("%[^\n]", ROW); fgetc(stdin); + for (int j=0; j<1025; j++) + tab[i][j] = ROW[j]; + } + + int size=0; + //On compte la taille du mot + for (int i =0; i < 257; i++) + { + if (T[i] != '\0') + { + size++; + //On convertit notre mot + if (T[i] > 96 && T[i] < 123) + T[i] -= 32; + } + else + break; + } + + //Affichage du mot + for (int i = 0; i < H; i++) //On répète ça le nombre de lignes + { + for (int j = 0; j< size; j++) //Répétition du nombre de lettre pour afficher la ligne i de chaque lettre + { + int id = T[j] - 65; + //fprintf(stderr,"T = %d Id= %d\n",T[j],id); + if ((id >= 0 && id < 26)) + { + for (int k =(id*L); k<((id+1)*(L)); k++) + { + printf("%c",tab[i][k]); + } + } + else + { + for (int k =(26*L); k<(27*(L)); k++) + { + printf("%c",tab[i][k]); + } + } + } + printf("\n"); + } + // Write an answer using printf(). DON'T FORGET THE TRAILING \n + // To debug: fprintf(stderr, "Debug messages...\n"); + + return 0; +} \ No newline at end of file diff --git a/Easy_Challenges/chuck_norris.c b/Easy_Challenges/chuck_norris.c new file mode 100644 index 0000000..5f2f4ec --- /dev/null +++ b/Easy_Challenges/chuck_norris.c @@ -0,0 +1,50 @@ +#include +#include +#include + +//Convert char into binary ascii +void charToBin(char c, char *buffer) +{ + for (int i = 6; i >=0; i--) + { + buffer[i] = '0' + c%2; + c/=2; + } + buffer[7] = '\n'; +} + +int main() +{ + char MESSAGE[101], binary[8], binMessage[707], prev; + int i = 0, j = 0, count = 0; + scanf("%[^\n]", MESSAGE); + binMessage[0] = '\0'; //Reset the string + + for (int i = 0; i < strlen(MESSAGE); i++) + { + charToBin(MESSAGE[i], binary); + strncat(binMessage,binary,7); //Adding the binary of each char into the binMessage string + } + + for (i =0; binMessage[i] != '\0'; i++) + { + if (binMessage[i] != prev) + { + for (j=0; j +#include +#include + + +int main() +{ + // the number of points used to draw the surface of Mars. + int surface_n; + scanf("%d", &surface_n); + for (int i = 0; i < surface_n; i++) { + // X coordinate of a surface point. (0 to 6999) + int land_x; + // Y coordinate of a surface point. By linking all the points together in a sequential fashion, you form the surface of Mars. + int land_y; + scanf("%d%d", &land_x, &land_y); + } + + // game loop + while (1) { + int X; + int Y; + // the horizontal speed (in m/s), can be negative. + int h_speed; + // the vertical speed (in m/s), can be negative. + int v_speed; + // the quantity of remaining fuel in liters. + int fuel; + // the rotation angle in degrees (-90 to 90). + int rotate; + // the thrust power (0 to 4). + int power; + scanf("%d%d%d%d%d%d%d", &X, &Y, &h_speed, &v_speed, &fuel, &rotate, &power); + + // Write an action using printf(). DON'T FORGET THE TRAILING \n + // To debug: fprintf(stderr, "Debug messages...\n"); + + int final_power = 0; + // 2 integers: rotate power. rotate is the desired rotation angle (should be 0 for level 1), power is the desired thrust power (0 to 4). + if (Y > 1500) + final_power = 3; + if (Y < 1500 && v_speed < -35) + final_power = 4; + + printf("0 %d\n",final_power); + } + + return 0; +} \ No newline at end of file diff --git a/Easy_Challenges/power_of_thor_1.c b/Easy_Challenges/power_of_thor_1.c new file mode 100644 index 0000000..a5e0960 --- /dev/null +++ b/Easy_Challenges/power_of_thor_1.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +int main() +{ + // the X position of the light of power + int light_x; + // the Y position of the light of power + int light_y; + // Thor's starting X position + int initial_tx; + // Thor's starting Y position + int initial_ty; + + scanf("%d%d%d%d", &light_x, &light_y, &initial_tx, &initial_ty); + + // game loop + while (1) + { + // The remaining amount of turns Thor can move. Do not remove this line. + int remaining_turns; + scanf("%d", &remaining_turns); + + char *dirX = ""; + char *dirY = ""; + + if ((initial_tx >= 0 && initial_tx < 40) && (initial_ty >= 0 && initial_ty < 18)) + { + if (initial_tx > light_x) + dirX = "W", initial_tx--; + else if (initial_tx < light_x) + dirX = "E", initial_tx++; + else + dirX = ""; + + if (initial_ty > light_y) + dirY = "N", initial_ty--; + else if (initial_ty < light_y) + dirY = "S", initial_ty++; + else + dirY = ""; + } + printf("%s%s\n", dirY, dirX); + } + + return 0; +} \ No newline at end of file diff --git a/Easy_Challenges/temperatures.c b/Easy_Challenges/temperatures.c new file mode 100644 index 0000000..dccc1b9 --- /dev/null +++ b/Easy_Challenges/temperatures.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +#define TAILLE_MAX 10000 + +int main() +{ + // the number of temperatures to analyse + int n,min=TAILLE_MAX; + scanf("%d", &n); + for (int i = 0; i < n; i++) { + // a temperature expressed as an integer ranging from -273 to 5526 + int t; + scanf("%d", &t); + + if (abs((0-t)) < abs(0-min)) + { + min = t; + } + else if (min == t) + { + min = t; + } + else if (abs(t) == abs(min)) + { + min = abs(min); + } + } + + // Write an answer using printf(). DON'T FORGET THE TRAILING \n + // To debug: fprintf(stderr, "Debug messages...\n"); + + if (min == TAILLE_MAX) + printf("0"); + else + printf("%d",min); + return 0; +} \ No newline at end of file diff --git a/Easy_Challenges/the_descent.c b/Easy_Challenges/the_descent.c new file mode 100644 index 0000000..baaaec8 --- /dev/null +++ b/Easy_Challenges/the_descent.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include + + +int main() +{ + + // game loop + while (1) { + int mountain_max = 0, mountain_Id = 0; + for (int i = 0; i < 8; i++) { + // represents the height of one mountain. + int mountain_h; + scanf("%d", &mountain_h); + if (mountain_h > mountain_max) + { + mountain_max = mountain_h; + mountain_Id = i; + } + } + printf("%d\n",mountain_Id); + } + + return 0; +} \ No newline at end of file diff --git a/Medium_Challenges/shadow_of_knight_1.c b/Medium_Challenges/shadow_of_knight_1.c new file mode 100644 index 0000000..a7abf20 --- /dev/null +++ b/Medium_Challenges/shadow_of_knight_1.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +int main() +{ + int W,H; + scanf("%d%d", &W, &H); + int N; + scanf("%d", &N); + int X0,Y0; + scanf("%d%d", &X0, &Y0); + + int LX = 0, LY = 0, HX = W - 1, HY = H - 1; + // game loop + while (1) { + // the direction of the bombs from batman's current location (U, UR, R, DR, D, DL, L or UL) + char bomb_dir[4]; + scanf("%s", bomb_dir); + fprintf(stderr,"La bombe est dans la direction: %s",bomb_dir); + + if (bomb_dir[0] == 'L' || bomb_dir[1] == 'L') + HX = X0 - 1; + else if (bomb_dir[0] == 'R' || bomb_dir[1] == 'R') + LX = X0 + 1; + + if (bomb_dir[0] == 'U') + HY = Y0 - 1; + else + LY = Y0 + 1; + + X0 = (HX + LX) /2; + Y0 = (HY + LY) /2; + + + // the location of the next window Batman should jump to. + printf("%d %d\n",X0,Y0); + } + + return 0; +} \ No newline at end of file diff --git a/Medium_Challenges/stock_exchange_losses.c b/Medium_Challenges/stock_exchange_losses.c new file mode 100644 index 0000000..9a805f4 --- /dev/null +++ b/Medium_Challenges/stock_exchange_losses.c @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +int main() +{ + int n, v, max = 0, max_lost =0, curr_lost = 0; + scanf("%d", &n); + for (int i = 0; i < n; i++) { + scanf("%d", &v); + + curr_lost = v-max; + if (curr_lost < max_lost) max_lost = curr_lost; + if (v>max) max = v; + } + printf("%d\n",max_lost); + + return 0; +} \ No newline at end of file diff --git a/Medium_Challenges/there_is_no_spoon_1.c b/Medium_Challenges/there_is_no_spoon_1.c new file mode 100644 index 0000000..b2f099c --- /dev/null +++ b/Medium_Challenges/there_is_no_spoon_1.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include + +int main() +{ + // the number of cells on the X axis + int width; + scanf("%d", &width); + + // the number of cells on the Y axis + int height; + scanf("%d", &height); fgetc(stdin); + + // New grid which will contains all the stuff + char grid[30][30]; + + //Getting items from input and adding them into the grid + for (int i = 0; i < height; i++) { + char line[32]; + scanf("%[^\n]", line); fgetc(stdin); + + for(int j =0;j < 32; j++){ + grid[i][j] = line[j]; + } + } + + for (int y = 0; y < height; y++) + { + for (int x = 0; x < width; x++) + { + //If we found a node, we print it's coordonnate and after check his neighbours + if (grid[y][x] == '0') + { + printf("%d %d ",x,y); + short x2, y2, ansX = -1, ansY = -1; + for (x2 = x+1; x2 < width; ++x2) //Checking if we found another node on the same line + { + if (grid[y][x2] == '0') //If node found we add it as neightbours and stop searching + { + ansX = x2; + ansY = y; + break; + } + } + printf("%d %d ",ansX, ansY); + ansX = -1; ansY = -1; + for (y2 = y+1; y2 < height; ++y2) //Checking if we found another node on the same column + { + if (grid[y2][x] == '0') //If we found we add it as neightbours and stop searching + { + ansX = x; + ansY = y2; + break; + } + } + printf("%d %d\n",ansX, ansY); + } + } + } + return 0; +} + \ No newline at end of file diff --git a/README.md b/README.md index b49b940..29d80ec 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,15 @@ # CodingGame - My solutions in C for coding game Challenges +------------ +In this github project you will find all my solutions for the coding games challenge I've made. For the moment all the challenges are done in C + +# Easy challenges +--------- + +# Medium challenges +--------- + +# Hard challenges +-------- + + +