diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/README.md b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/README.md new file mode 100644 index 0000000..5db65f3 --- /dev/null +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/README.md @@ -0,0 +1,3 @@ +# rotate-2-dimensional-array-90-degrees/c/function + +Created by [@Divlo](https://github.com/Divlo) on 5 December 2021. diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.c b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.c new file mode 100644 index 0000000..cbb2521 --- /dev/null +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.c @@ -0,0 +1,97 @@ +#include "array_2D_int.h" + +#include +#include +#include + +#include "character.h" + +void array_2D_int_print(int **array, size_t number_of_rows, size_t number_of_columns) { + for (size_t i = 0; i < number_of_rows; i++) { + for (size_t j = 0; j < number_of_columns; j++) { + printf("%d", array[i][j]); + if (j != number_of_columns - 1) { + printf(" "); + } + } + printf("\n"); + } +} + +int **array_2D_int_input(size_t *number_of_rows, size_t *number_of_columns) { + int **array = malloc(sizeof(int *)); + *number_of_rows = 1; + *number_of_columns = 1; + array[0] = malloc(*number_of_columns * sizeof(int)); + array[0][0] = 0; + char character; + size_t length = 1; + char *string = malloc(length * sizeof(char)); + *string = '\0'; + while ((character = getchar()) != EOF) { + if (character == '\n') { + int number = atoi(string); + array[*number_of_rows - 1][*number_of_columns - 1] = number; + length = 1; + memset(string, 0, length * sizeof(char)); + *string = '\0'; + *number_of_rows = *number_of_rows + 1; + *number_of_columns = 1; + array = realloc(array, *number_of_rows * sizeof(int *)); + array[*number_of_rows - 1] = malloc(*number_of_columns * sizeof(int)); + } else { + if (character == ' ') { + int number = atoi(string); + array[*number_of_rows - 1][*number_of_columns - 1] = number; + length = 1; + memset(string, 0, length * sizeof(char)); + *string = '\0'; + *number_of_columns = *number_of_columns + 1; + } else { + length++; + string = realloc(string, length * sizeof(char)); + character_append(string, character); + } + } + } + int number = atoi(string); + array[*number_of_rows - 1][*number_of_columns - 1] = number; + return array; +} + +int **array_2D_int_reverse_rows(int **array, size_t *number_of_rows, size_t *number_of_columns) { + int **rotated_array = malloc(*number_of_columns * sizeof(int *)); + for (size_t i = 0; i < *number_of_columns; i++) { + rotated_array[i] = malloc(*number_of_rows * sizeof(int)); + } + for (size_t i = 0; i < *number_of_columns; i++) { + for (size_t j = 0; j < *number_of_rows; j++) { + rotated_array[i][j] = array[*number_of_rows - i - 1][j]; + } + } + return rotated_array; +} + +int **array_2D_int_rotate_90_degrees_clockwise(int **array, size_t *number_of_rows, size_t *number_of_columns) { + int **rotated_array = malloc(*number_of_columns * sizeof(int *)); + for (size_t i = 0; i < *number_of_columns; i++) { + rotated_array[i] = malloc(*number_of_rows * sizeof(int)); + } + for (size_t i = 0; i < *number_of_columns; i++) { + for (size_t j = 0; j < *number_of_rows; j++) { + rotated_array[i][j] = array[*number_of_rows - j - 1][i]; + } + } + size_t number_of_rows_temp = *number_of_rows; + *number_of_rows = *number_of_columns; + *number_of_columns = number_of_rows_temp; + return rotated_array; +} + +int **array_2D_int_rotate_90_degrees_anticlockwise(int **array, size_t *number_of_rows, size_t *number_of_columns) { + int **result = array_2D_int_rotate_90_degrees_clockwise(array, number_of_rows, number_of_columns); + result = array_2D_int_rotate_90_degrees_clockwise(result, number_of_rows, number_of_columns); + result = array_2D_int_rotate_90_degrees_clockwise(result, number_of_rows, number_of_columns); + result = array_2D_int_reverse_rows(result, number_of_rows, number_of_columns); + return result; +} diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.h b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.h new file mode 100644 index 0000000..c42db5a --- /dev/null +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/array_2D_int.h @@ -0,0 +1,54 @@ +#ifndef __ARRAY_2D_INT__ +#define __ARRAY_2D_INT__ + +#include + +/** + * @brief Prints a 2D array of integers. + * + * @param array + * @param number_of_rows + * @param number_of_columns + */ +void array_2D_int_print(int **array, size_t number_of_rows, size_t number_of_columns); + +/** + * @brief Read from stdin a 2D array (rectangle or square) of integers. + * + * @param number_of_rows + * @param number_of_columns + * @return int** + */ +int **array_2D_int_input(size_t *number_of_rows, size_t *number_of_columns); + +/** + * @brief Reverse the order of the rows of a 2D array of integers. + * + * @param array + * @param number_of_rows + * @param number_of_columns + * @return int** + */ +int **array_2D_int_reverse_rows(int **array, size_t *number_of_rows, size_t *number_of_columns); + +/** + * @brief Rotate a 2D array of integers by 90 degrees clockwise. + * + * @param array + * @param number_of_rows + * @param number_of_columns + * @return int** + */ +int **array_2D_int_rotate_90_degrees_clockwise(int **array, size_t *number_of_rows, size_t *number_of_columns); + +/** + * @brief Rotate a 2D array of integers by 90 degrees anticlockwise. + * + * @param array + * @param number_of_rows + * @param number_of_columns + * @return int** + */ +int **array_2D_int_rotate_90_degrees_anticlockwise(int **array, size_t *number_of_rows, size_t *number_of_columns); + +#endif diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/character.c b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/character.c new file mode 100644 index 0000000..49d4f00 --- /dev/null +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/character.c @@ -0,0 +1,10 @@ +#include "character.h" + +#include +#include + +void character_append(char* string, char character) { + size_t length = strlen(string); + string[length] = character; + string[length + 1] = '\0'; +} diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/character.h b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/character.h new file mode 100644 index 0000000..0fab88f --- /dev/null +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/character.h @@ -0,0 +1,13 @@ +#ifndef __CHARACTER__ +#define __CHARACTER__ + +/** + * @brief Append a character to a string, assuming string points to an array + * with enough space. + * + * @param string + * @param character + */ +void character_append(char* string, char character); + +#endif diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/input.c b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/input.c new file mode 100644 index 0000000..c07bb6d --- /dev/null +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/input.c @@ -0,0 +1,19 @@ +#include "input.h" + +#include +#include + +#include "character.h" + +char* input() { + char character; + size_t length = 1; + char* string = malloc(length * sizeof(char)); + *string = '\0'; + while ((character = getchar()) != '\n' && character != EOF) { + length++; + string = realloc(string, length * sizeof(char)); + character_append(string, character); + } + return string; +} diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/input.h b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/input.h new file mode 100644 index 0000000..d432398 --- /dev/null +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/input.h @@ -0,0 +1,11 @@ +#ifndef __INPUT__ +#define __INPUT__ + +/** + * @brief Read a line from stdin. + * + * @return char* + */ +char* input(); + +#endif diff --git a/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/solution.c b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/solution.c new file mode 100644 index 0000000..214340f --- /dev/null +++ b/challenges/rotate-2-dimensional-array-90-degrees/solutions/c/function/solution.c @@ -0,0 +1,26 @@ +#include +#include + +#include "array_2D_int.h" +#include "input.h" + +int main() { + size_t number_of_rows = 0; + size_t number_of_columns = 0; + char *direction = input(); + int **array = array_2D_int_input(&number_of_rows, &number_of_columns); + + if (strcmp(direction, "clockwise") == 0) { + array = array_2D_int_rotate_90_degrees_clockwise(array, &number_of_rows, &number_of_columns); + } else { + array = array_2D_int_rotate_90_degrees_anticlockwise(array, &number_of_rows, &number_of_columns); + } + array_2D_int_print(array, number_of_rows, number_of_columns); + + for (size_t i = 0; i < number_of_rows; i++) { + free(array[i]); + } + free(array); + free(direction); + return EXIT_SUCCESS; +} diff --git a/templates/docker/javascript/Dockerfile b/templates/docker/javascript/Dockerfile index 66269ae..bb33567 100644 --- a/templates/docker/javascript/Dockerfile +++ b/templates/docker/javascript/Dockerfile @@ -1,3 +1,3 @@ -FROM node:16.13.0 +FROM node:16.13.1 COPY ./ ./ CMD ["node", "solution.js"] diff --git a/templates/docker/rust/Dockerfile b/templates/docker/rust/Dockerfile index f3416e3..adc2c61 100644 --- a/templates/docker/rust/Dockerfile +++ b/templates/docker/rust/Dockerfile @@ -1,4 +1,4 @@ -FROM rust:1.56.1 +FROM rust:1.57.0 COPY ./ ./ RUN rustc solution.rs CMD ["./solution"] diff --git a/templates/docker/typescript/Dockerfile b/templates/docker/typescript/Dockerfile index 8698d3d..7f76dbd 100644 --- a/templates/docker/typescript/Dockerfile +++ b/templates/docker/typescript/Dockerfile @@ -1,4 +1,4 @@ -FROM node:16.13.0 +FROM node:16.13.1 RUN npm install --global ts-node typescript @types/node COPY ./ ./ CMD ["ts-node", "solution.ts"]