From: Lukáš Jiřiště Date: Sat, 9 Dec 2023 17:35:27 +0000 (+0100) Subject: Add solution to first part of day 8 X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=eee5d053efc81a0cba8eadedc408105edd1e8509;p=AoC_2023.git Add solution to first part of day 8 --- diff --git a/8/8.c b/8/8.c new file mode 100644 index 0000000..c77da29 --- /dev/null +++ b/8/8.c @@ -0,0 +1,169 @@ + +#include +#include +#include +#include "libft.h" + +#define ID_LEN 3 +#define INIT_CAP 4 + +struct s_snode +{ + char id[ID_LEN]; + char id_l[ID_LEN]; + char id_r[ID_LEN]; +}; +typedef struct s_snode t_snode; + +struct s_inode +{ + char id[ID_LEN]; + size_t index_l; + size_t index_r; +}; +typedef struct s_inode t_inode; + +union u_node +{ + t_snode snode; + t_inode inode; +}; +typedef union u_node t_node; + +struct s_node_vec +{ + size_t capacity; + size_t size; + t_node *array; +}; +typedef struct s_node_vec t_node_vec; + +int enlarge_vec(t_node_vec *vec) +{ + t_node *tmp; + + if (vec->capacity == 0) + { + vec->array = NULL; + vec->size = 0; + vec->capacity = INIT_CAP; + } + tmp = vec->array; + vec->array = ft_calloc(vec->capacity * 2, sizeof(*vec->array)); + if (!vec->array) + { + vec->array = tmp; + return (1); + } + vec->capacity *= 2; + ft_memcpy(vec->array, tmp, vec->size * sizeof(*vec->array)); + free(tmp); + return (0); +} + +void translate_nodes(t_node_vec *str_map) +{ + char left[ID_LEN]; + char right[ID_LEN]; + size_t i; + size_t j; + + i = 0; + while (i < str_map->size) + { + ft_memcpy(left, str_map->array[i].snode.id_l, ID_LEN); + ft_memcpy(right, str_map->array[i].snode.id_r, ID_LEN); + j = 0; + while (j < str_map->size) + { + if (!ft_strncmp(str_map->array[j].snode.id, left, ID_LEN)) + str_map->array[i].inode.index_l = j; + if (!ft_strncmp(str_map->array[j].snode.id, right, ID_LEN)) + str_map->array[i].inode.index_r = j; + ++j; + } + ++i; + } + return ; +} + +void add_node(t_node_vec *str_map, const char *line) +{ + if (str_map->size >= str_map->capacity) + enlarge_vec(str_map); + ft_memcpy(&(str_map->array[str_map->size].snode.id), line, ID_LEN); + ft_memcpy(&(str_map->array[str_map->size].snode.id_l), line + ID_LEN + 4, ID_LEN); + ft_memcpy(&(str_map->array[str_map->size].snode.id_r), line + 2 * ID_LEN + 6, ID_LEN); + ++str_map->size; + return ; +} + +int parse(const char *filename, t_node_vec *ind_map, char **inst) +{ + int fd; + char *line; + + fd = open(filename, O_RDONLY); + if (fd < 0) + return (2); + *inst = get_next_line(fd); + (*inst)[ft_strlen(*inst) - 1] = '\0'; + free(get_next_line(fd)); + line = get_next_line(fd); + while (line) + { + add_node(ind_map, line); + free(line); + line = get_next_line(fd); + } + translate_nodes(ind_map); + close(fd); + return (0); +} + +size_t find_ind(const t_node_vec ind_map, const char wanted[ID_LEN]) +{ + size_t i; + + i = 0; + while (i < ind_map.size) + { + if (!ft_strncmp(ind_map.array[i].inode.id, wanted, ID_LEN)) + return (i); + ++i; + } + return (-1); +} +int steps_to_traverse(const t_node_vec ind_map, const char *inst, const char end_id[ID_LEN]) +{ + size_t i; + int result; + + i = find_ind(ind_map, "AAA"); + result = 0; + while (ft_strncmp(ind_map.array[i].inode.id, end_id, ID_LEN)) + { + if (inst[result % ft_strlen(inst)] == 'R') + i = ind_map.array[i].inode.index_r; + else + i = ind_map.array[i].inode.index_l; + ++result; + } + return (result); +} + +int main(int argc, char **argv) +{ + t_node_vec ind_map; + char *inst; + + if (argc != 2) + return (1); + ind_map.capacity = 0; + parse(argv[1], &ind_map, &inst); + ft_printf("%i steps are needed to reach ZZZ.\n", steps_to_traverse(ind_map, inst, "ZZZ")); + return (0); + free(ind_map.array); + free(inst); +} +