add day 5
This commit is contained in:
142
2025/5/part2.c
Normal file
142
2025/5/part2.c
Normal file
@ -0,0 +1,142 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* part2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2025/12/05 14:49:11 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 *test_range(long int nb, t_range_list *lst)
|
||||
{
|
||||
while(lst)
|
||||
{
|
||||
if(nb >= lst->min && nb <= lst->max)
|
||||
return(lst);
|
||||
lst = lst->next;
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
static int check_dup(t_range_list *node, t_range_list *lst)
|
||||
{
|
||||
t_range_list *found_min;
|
||||
t_range_list *found_max;
|
||||
|
||||
found_min = test_range(node->min, lst);
|
||||
found_max = test_range(node->max, lst);
|
||||
if(!found_min && !found_max)
|
||||
return(0);
|
||||
if(found_min && found_min == found_max)
|
||||
return(1);
|
||||
|
||||
if(found_min)
|
||||
node->min = found_min->max + 1;
|
||||
if(node->min > node->max)
|
||||
return(1);
|
||||
|
||||
if(found_max)
|
||||
node->max = found_max->min - 1;
|
||||
if(node->min > node->max)
|
||||
return(1);
|
||||
|
||||
if(node->min == node->max && test_range(node->min, lst))
|
||||
return(1);
|
||||
|
||||
return(check_dup(node, lst));
|
||||
}
|
||||
|
||||
static int rm_duplicates(t_range_list **orig)
|
||||
{
|
||||
t_range_list *tmp;
|
||||
t_range_list *lst;
|
||||
t_range_list *prev;
|
||||
|
||||
prev = 0;
|
||||
lst = (*orig);
|
||||
while(lst)
|
||||
{
|
||||
tmp = *orig;
|
||||
while(tmp)
|
||||
{
|
||||
if(tmp != lst && lst->min >= tmp->min && lst->max <= tmp->max)
|
||||
{
|
||||
if(prev)
|
||||
prev->next = lst->next;
|
||||
else
|
||||
*orig = lst->next;
|
||||
tmp = lst;
|
||||
lst = lst->next;
|
||||
free(tmp);
|
||||
return(1);
|
||||
}
|
||||
tmp = tmp->next;
|
||||
}
|
||||
prev = lst;
|
||||
lst = lst->next;
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
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)++;
|
||||
if(check_dup(new, res))
|
||||
free(new);
|
||||
else
|
||||
{
|
||||
new->next = res;
|
||||
res = new;
|
||||
}
|
||||
}
|
||||
|
||||
while(rm_duplicates(&res))
|
||||
;;
|
||||
return(res);
|
||||
}
|
||||
|
||||
long int resolve_part2(char *input, char **split)
|
||||
{
|
||||
(void)split;
|
||||
t_range_list *lst;
|
||||
long int res;
|
||||
|
||||
res = 0;
|
||||
lst = parse_ranges(&input);
|
||||
while(lst)
|
||||
{
|
||||
res += (lst->max - lst->min) + 1;
|
||||
lst = lst->next;
|
||||
}
|
||||
return(res);
|
||||
}
|
||||
Reference in New Issue
Block a user