add free on days 21 to 25, nothing leaks now (maybe)

This commit is contained in:
2024-12-29 23:54:34 +01:00
parent 27a62a50ad
commit fa519ac929
7 changed files with 158 additions and 14 deletions

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/23 23:20:04 by tomoron ### ########.fr */
/* Updated: 2024/12/28 12:23:06 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -134,6 +134,18 @@ static void add_done(t_done **done, t_computer *c1, t_computer *c2, t_computer *
*done = new;
}
static void free_done(t_done *d)
{
t_done *tmp;
while(d)
{
tmp = d->next;
free(d);
d = tmp;
}
}
static int get_res(t_computer *input)
{
t_connected *tmp1;
@ -165,9 +177,35 @@ static int get_res(t_computer *input)
}
input = input->next;
}
free_done(done);
return(res);
}
static void free_connected(t_connected *co)
{
t_connected *tmp;
while(co)
{
tmp = co->next;
free(co);
co = tmp;
}
}
static void free_computer(t_computer *in)
{
t_computer *tmp;
while(in)
{
tmp = in->next;
free_connected(in->connected);
free(in);
in = tmp;
}
}
long int resolve_part1(char *line, char **split)
{
(void)line;
@ -182,5 +220,6 @@ long int resolve_part1(char *line, char **split)
split++;
}
res = get_res(input);
free_computer(input);
return(res);
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/24 00:26:43 by tomoron ### ########.fr */
/* Updated: 2024/12/29 23:49:22 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -196,6 +196,18 @@ static void sort_list(t_found *found)
}
}
static void free_found(t_found *f)
{
t_found *tmp;
while(f)
{
tmp = f->next;
free(f);
f = tmp;
}
}
static int get_res(t_computer *input)
{
t_found *found;
@ -211,6 +223,7 @@ static int get_res(t_computer *input)
found = 0;
tmp = 0;
find_connected(&tmp,&found, input);
free_found(tmp);
i = 0;
tmp = found;
while(tmp)
@ -221,10 +234,15 @@ static int get_res(t_computer *input)
if(i > max_len)
{
max_len = i;
if(res)
free_found(res);
res = found;
}
else
free_found(found);
input = input->next;
}
tmp = res;
i = 0;
while(res)
{
@ -236,9 +254,35 @@ static int get_res(t_computer *input)
i++;
}
printf("\n");
free_found(tmp);
return(0);
}
static void free_connected(t_connected *co)
{
t_connected *tmp;
while(co)
{
tmp = co->next;
free(co);
co = tmp;
}
}
static void free_computer(t_computer *in)
{
t_computer *tmp;
while(in)
{
tmp = in->next;
free_connected(in->connected);
free(in);
in = tmp;
}
}
long int resolve_part2(char *line, char **split)
{
(void)line;
@ -253,5 +297,6 @@ long int resolve_part2(char *line, char **split)
split++;
}
res = get_res(input);
free_computer(input);
return(res);
}