add day 5
This commit is contained in:
78
2025/5/part1.c
Normal file
78
2025/5/part1.c
Normal file
@ -0,0 +1,78 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* part1.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2025/12/05 11:36:52 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "libft/libft.h"
|
||||
|
||||
typedef struct s_range_list
|
||||
{
|
||||
long int min;
|
||||
long int max;
|
||||
struct s_range_list *next;
|
||||
} t_range_list;
|
||||
|
||||
static t_range_list *parse_ranges(char **input)
|
||||
{
|
||||
t_range_list *res;
|
||||
t_range_list *new;
|
||||
|
||||
res = 0;
|
||||
while(**input != '\n')
|
||||
{
|
||||
new = malloc(sizeof(t_range_list));
|
||||
if(!new)
|
||||
return(0);
|
||||
new->min = atol(*input);
|
||||
while(ft_isdigit(**input))
|
||||
(*input)++;
|
||||
(*input)++;
|
||||
new->max = atol(*input);
|
||||
while(ft_isdigit(**input))
|
||||
(*input)++;
|
||||
(*input)++;
|
||||
new->next = res;
|
||||
res = new;
|
||||
}
|
||||
return(res);
|
||||
}
|
||||
|
||||
static int test_range(long int nb, t_range_list *lst)
|
||||
{
|
||||
while(lst)
|
||||
{
|
||||
if(nb >= lst->min && nb <= lst->max)
|
||||
return(1);
|
||||
lst = lst->next;
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
long int resolve_part1(char *input, char **split)
|
||||
{
|
||||
(void)split;
|
||||
t_range_list *lst;
|
||||
long int res;
|
||||
|
||||
res = 0;
|
||||
lst = parse_ranges(&input);
|
||||
input++;
|
||||
while(*input)
|
||||
{
|
||||
if(test_range(atol(input), lst))
|
||||
res++;
|
||||
while(ft_isdigit(*input))
|
||||
input++;
|
||||
input++;
|
||||
}
|
||||
return(res);
|
||||
}
|
||||
Reference in New Issue
Block a user