93 lines
1.9 KiB
C
93 lines
1.9 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* part1.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
|
/* Updated: 2025/12/06 15:54:24 by tomoron ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "libft/libft.h"
|
|
|
|
long int get_nb(char *line, int index)
|
|
{
|
|
int i = 0;
|
|
|
|
while(i < index && *line)
|
|
{
|
|
while(*line == ' ')
|
|
line++;
|
|
while(ft_isdigit(*line) || *line == '+' || *line == '*')
|
|
line++;
|
|
i++;
|
|
}
|
|
if(!*line)
|
|
return(0);
|
|
while(*line == ' ')
|
|
line++;
|
|
if(*line == '*')
|
|
return(-1);
|
|
if(*line == '+')
|
|
return(-2);
|
|
return(ft_atoi(line));
|
|
|
|
}
|
|
|
|
static char *get_ll(char **split)
|
|
{
|
|
char *tmp;
|
|
|
|
tmp = 0;
|
|
while(*(split + 1))
|
|
split++;
|
|
|
|
tmp = *split;
|
|
*split = 0;
|
|
return(tmp);
|
|
}
|
|
|
|
long int resolve_part1(char *input, char **split)
|
|
{
|
|
(void)input;
|
|
char **tmp;
|
|
char *last_line;
|
|
long int ret;
|
|
long int res;
|
|
long int cur;
|
|
long int op;
|
|
int i = 0;
|
|
|
|
res = 0;
|
|
last_line = get_ll(split);
|
|
while(1)
|
|
{
|
|
tmp = split;
|
|
op = get_nb(last_line, i);
|
|
cur = 0;
|
|
while(*tmp)
|
|
{
|
|
ret = get_nb(*tmp, i);
|
|
if(ret == 0)
|
|
return(res);
|
|
if(cur)
|
|
{
|
|
if(op == -1)
|
|
cur *= ret;
|
|
else
|
|
cur += ret;
|
|
}
|
|
else
|
|
cur = ret;
|
|
tmp++;
|
|
}
|
|
res += cur;
|
|
i++;
|
|
}
|
|
return(0);
|
|
}
|