Files
aoc/2025/6/part2.c
2025-12-06 16:22:04 +01:00

80 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* part2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2025/12/06 16:16:55 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft/libft.h"
static char *get_ll(char **split)
{
char *tmp;
tmp = 0;
while(*(split + 1))
split++;
tmp = *split;
*split = 0;
return(tmp);
}
long int resolve_part2(char *input, char **split)
{
(void)input;
char **tmp;
char *last_line;
long int nb;
long int res;
long int cur;
long int op;
int i = 0;
res = 0;
last_line = get_ll(split);
cur = 0;
while(split[0][i])
{
if(last_line[i] == '+')
op = 1;
if(last_line[i] == '*')
op = 0;
tmp = split;
nb = 0;
while(*tmp)
{
if(ft_isdigit((*tmp)[i]))
{
nb *= 10;
nb += (*tmp)[i] - '0';
}
tmp++;
}
if(nb == 0)
{
res += cur;
cur = 0;
}
if(!cur)
cur = nb;
else
{
if(op)
cur += nb;
else
cur *= nb;
}
i++;
}
res+=cur;
return(res);
}