From 87b6b6d06b646898ee9dd2d8fe2c64aa2fdca1f8 Mon Sep 17 00:00:00 2001 From: tomoron Date: Wed, 11 Dec 2024 17:26:14 +0100 Subject: [PATCH] part 1 backup --- 2024/11/part1.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 2024/11/part1.c diff --git a/2024/11/part1.c b/2024/11/part1.c new file mode 100644 index 0000000..0564e91 --- /dev/null +++ b/2024/11/part1.c @@ -0,0 +1,128 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2024/12/11 17:17:34 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft/libft.h" + +typedef struct s_pebble +{ + long int value; + struct s_pebble *next; +} t_pebble; + +static t_pebble *get_input(char *input) +{ + t_pebble *res; + t_pebble *prev; + t_pebble *cur; + + prev = 0; + while(*input && (ft_isdigit(*input) || *input == ' ')) + { + cur = malloc(sizeof(t_pebble)); + cur->value = atol(input); + if(prev) + prev->next = cur; + else + res = cur; + prev = cur; + while(ft_isdigit(*input)) + input++; + while(*input == ' ') + input++; + } + return(res); +} + +int count_digits(long int nb) +{ + int res; + + res = 1; + while(nb > 9) + { + nb /= 10; + res++; + } + return(res); +} + +void split_pebble(t_pebble *pebble) +{ + t_pebble *new; + t_pebble *tmp; + int len; + long exp; + int i; + + new = malloc(sizeof(t_pebble)); + len = count_digits(pebble->value) / 2; + new->value = 0; + i = 0; + exp = 1; + while(i < len) + { + new->value += (pebble->value % 10) * exp; + pebble->value /= 10; + exp *= 10; + i++; + } + tmp = pebble->next; + pebble->next = new; + new->next = tmp; +} + +long int update(t_pebble *pebble) +{ + long int res; + + res = 0; + while(pebble) + { + if(!pebble->value) + pebble->value = 1; + else if(count_digits(pebble->value) % 2 == 0) + { + split_pebble(pebble); + pebble = pebble->next; + } + else + pebble->value *= 2024; + pebble = pebble->next; + res++; + } + return(res); +} + +long int resolve_part1(char *input, char **split) +{ + (void)split; + t_pebble *pebble; + pebble = get_input(input); + int i; + long int res; + + i = 0; + res = 0; + while(i < 25) + { + printf("step : %d, len :%ld\n",i, update(pebble)); + i++; + } + while(pebble) + { + res++; + pebble = pebble->next; + } + return(res); +}