From 71054502c344baa167bb44e23461af055dca6160 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Mon, 4 Dec 2023 18:50:25 +0100 Subject: [PATCH] Add solution for first part of day 3 --- 3/3.c | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 3/3.c diff --git a/3/3.c b/3/3.c new file mode 100644 index 0000000..14baf24 --- /dev/null +++ b/3/3.c @@ -0,0 +1,170 @@ + +#include +#include +#include +#include "libft.h" + +struct s_matrix +{ + size_t rows; + size_t cols; + char *array; +}; +typedef struct s_matrix t_matrix; + +char *get_element(t_matrix matrix, size_t i, size_t j) +{ + if (i >= matrix.rows || j >= matrix.cols) + return (NULL); + return (matrix.array + i * matrix.cols + j); +} + +int fill_matrix_size(t_matrix *matrix, const char *file_name) +{ + int fd; + char c; + char *line; + + matrix->rows = 1; + matrix->cols = 0; + fd = open(file_name, O_RDONLY); + if (fd < 0) + return (-1); + c = 'a'; + while (c != '\n' && c != '\0') + { + read(fd, &c, 1); + ++matrix->cols; + } + --matrix->cols; + line = malloc(matrix->cols + 1); + while (read(fd, line, matrix->cols + 1)) + ++matrix->rows; + close(fd); + free(line); + return (0); +} + +t_matrix read_as_matrix(const char *file_name) +{ + int fd; + t_matrix matrix; + size_t i; + size_t j; + char *line; + + fill_matrix_size(&matrix, file_name); + fd = open(file_name, O_RDONLY); + if (fd < 0) + return (matrix); + matrix.array = malloc(matrix.rows * matrix.cols * sizeof(*matrix.array)); + if (!matrix.array) + return (matrix); + line = malloc(matrix.cols + 1); + i = 0; + while (i < matrix.rows) + { + read(fd, line, matrix.cols + 1); + j = 0; + while (j < matrix.cols) + { + *get_element(matrix, i, j) = line[j]; + ++j; + } + ++i; + } + free(line); + return (matrix); +} + +size_t get_number_length(const char *number) +{ + size_t result; + + result = 0; + while (ft_isdigit(number[result])) + ++result; + return (result); +} + +int is_symbol(char *el) +{ + if (el) + if (*el != '.' && !ft_isdigit(*el)) + return (1); + return (0); +} + +int serial_to_int(const char *str, size_t length) +{ + char *serial; + int result; + + serial = ft_strndup(str, length); + result = ft_atoi(serial); + free(serial); + return (result); +} + +int serial_number(t_matrix schematics, size_t i, size_t j) +{ + const size_t i_0 = i; + const size_t j_0 = j; + int result; + size_t length; + + if (get_element(schematics, i, j - 1)) + if (ft_isdigit(*get_element(schematics, i, j - 1))) + return (0); + length = get_number_length(get_element(schematics, i, j)); + if (i > 0) + --i; + while (i <= i_0 + 1) + { + j = j_0; + if (j > 0) + j = j_0 - 1; + while (j <= j_0 + length) + { + if (is_symbol(get_element(schematics, i, j))) + return (serial_to_int(get_element(schematics, i_0, j_0), length)); + ++j; + } + ++i; + } + return (0); +} + +int process(t_matrix schematics) +{ + int result; + size_t i; + size_t j; + + result = 0; + i = 0; + while (i < schematics.rows) + { + j = 0; + while (j < schematics.cols) + { + if (ft_isdigit(*get_element(schematics, i, j))) + result += serial_number(schematics, i, j); + ++j; + } + ++i; + } + return (result); +} + +int main(int argc, char **argv) +{ + t_matrix schematics; + + if (argc != 2) + return (1); + schematics = read_as_matrix(argv[1]); + ft_printf("The resulting sum is %i.\n", process(schematics)); + return (0); +} + -- 2.30.2