add day 11
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/12/11 17:17:34 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/12/11 22:19:45 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -30,6 +30,7 @@ static t_pebble *get_input(char *input)
|
||||
while(*input && (ft_isdigit(*input) || *input == ' '))
|
||||
{
|
||||
cur = malloc(sizeof(t_pebble));
|
||||
cur->next = 0;
|
||||
cur->value = atol(input);
|
||||
if(prev)
|
||||
prev->next = cur;
|
||||
@ -44,7 +45,7 @@ static t_pebble *get_input(char *input)
|
||||
return(res);
|
||||
}
|
||||
|
||||
int count_digits(long int nb)
|
||||
static int count_digits(long int nb)
|
||||
{
|
||||
int res;
|
||||
|
||||
@ -57,71 +58,57 @@ int count_digits(long int nb)
|
||||
return(res);
|
||||
}
|
||||
|
||||
void split_pebble(t_pebble *pebble)
|
||||
static void split_pebble(long int *l, long int *r)
|
||||
{
|
||||
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;
|
||||
len = count_digits(*l) / 2;
|
||||
*r = 0;
|
||||
i = 0;
|
||||
exp = 1;
|
||||
while(i < len)
|
||||
{
|
||||
new->value += (pebble->value % 10) * exp;
|
||||
pebble->value /= 10;
|
||||
*r += (*l % 10) * exp;
|
||||
*l /= 10;
|
||||
exp *= 10;
|
||||
i++;
|
||||
}
|
||||
tmp = pebble->next;
|
||||
pebble->next = new;
|
||||
new->next = tmp;
|
||||
}
|
||||
|
||||
long int update(t_pebble *pebble)
|
||||
static long int get_nb(long int nb, int depth)
|
||||
{
|
||||
long int res;
|
||||
long int tmp;
|
||||
|
||||
res = 0;
|
||||
while(pebble)
|
||||
if(depth == 25)
|
||||
return(1);
|
||||
if(nb == 0)
|
||||
return(get_nb(1, depth + 1));
|
||||
else if(count_digits(nb) % 2 == 0)
|
||||
{
|
||||
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++;
|
||||
split_pebble(&nb, &tmp);
|
||||
res = get_nb(nb, depth + 1);
|
||||
return(res + get_nb(tmp, depth + 1));
|
||||
}
|
||||
return(res);
|
||||
else
|
||||
return(get_nb(nb * 2024, depth + 1));
|
||||
}
|
||||
|
||||
long int resolve_part1(char *input, char **split)
|
||||
{
|
||||
(void)split;
|
||||
(void)input;
|
||||
t_pebble *pebble;
|
||||
pebble = get_input(input);
|
||||
int i;
|
||||
long int res;
|
||||
(void)get_input;
|
||||
|
||||
i = 0;
|
||||
res = 0;
|
||||
while(i < 25)
|
||||
while(pebble)
|
||||
{
|
||||
printf("step : %d, len :%ld\n",i, update(pebble));
|
||||
i++;
|
||||
}
|
||||
while(pebble)
|
||||
{
|
||||
res++;
|
||||
res += get_nb(pebble->value, 0);
|
||||
pebble = pebble->next;
|
||||
}
|
||||
return(res);
|
||||
|
Reference in New Issue
Block a user