add free on day 5

This commit is contained in:
2024-12-15 19:22:16 +01:00
parent 5c411be678
commit 06128868af
2 changed files with 19 additions and 8 deletions

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ /* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/05 08:50:09 by tomoron ### ########.fr */ /* Updated: 2024/12/15 19:20:04 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -74,7 +74,6 @@ static int respect_order(int *nbrs, t_orders *orders, int len)
{ {
int i; int i;
int j; int j;
// int tmp;
i = 0; i = 0;
while(i < len) while(i < len)
@ -85,13 +84,8 @@ static int respect_order(int *nbrs, t_orders *orders, int len)
if(i != j) if(i != j)
{ {
if(nbrs[i] == orders->left && nbrs[j] == orders->right && i > j) if(nbrs[i] == orders->left && nbrs[j] == orders->right && i > j)
{
// tmp = nbrs[i];
// nbrs[i] = nbrs[j];
// nbrs[j] = tmp;
return(0); return(0);
} }
}
j++; j++;
} }
i++; i++;
@ -119,7 +113,10 @@ static long int check_line(char *line, t_orders *orders)
nbrs = get_numbers(line, &len); nbrs = get_numbers(line, &len);
if(!is_ordered(nbrs, orders, len)) if(!is_ordered(nbrs, orders, len))
{
free(nbrs);
return(0); return(0);
}
res = nbrs[(len / 2)]; res = nbrs[(len / 2)];
free(nbrs); free(nbrs);
@ -130,6 +127,7 @@ long int resolve_part1(char *input, char **split)
{ {
(void)input; (void)input;
t_orders *orders; t_orders *orders;
t_orders *tmp;
long int res; long int res;
orders = 0; orders = 0;
@ -144,5 +142,11 @@ long int resolve_part1(char *input, char **split)
res += check_line(*split, orders); res += check_line(*split, orders);
split++; split++;
} }
while(orders)
{
tmp = orders->next;
free(orders);
orders = tmp;
}
return(res); return(res);
} }

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ /* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
/* Updated: 2024/12/05 08:49:16 by tomoron ### ########.fr */ /* Updated: 2024/12/15 19:20:52 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -135,6 +135,7 @@ long int resolve_part2(char *input, char **split)
{ {
(void)input; (void)input;
t_orders *orders; t_orders *orders;
t_orders *tmp;
long int res; long int res;
orders = 0; orders = 0;
@ -149,5 +150,11 @@ long int resolve_part2(char *input, char **split)
res += check_line(*split, orders); res += check_line(*split, orders);
split++; split++;
} }
while(orders)
{
tmp = orders->next;
free(orders);
orders = tmp;
}
return(res); return(res);
} }