From 06128868af372d485fc8b883770876c5a9985b00 Mon Sep 17 00:00:00 2001 From: tomoron Date: Sun, 15 Dec 2024 19:22:16 +0100 Subject: [PATCH] add free on day 5 --- 2024/5/part1.c | 18 +++++++++++------- 2024/5/part2.c | 9 ++++++++- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/2024/5/part1.c b/2024/5/part1.c index 73fbfff..0776087 100644 --- a/2024/5/part1.c +++ b/2024/5/part1.c @@ -6,7 +6,7 @@ /* 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 j; -// int tmp; i = 0; while(i < len) @@ -85,12 +84,7 @@ static int respect_order(int *nbrs, t_orders *orders, int len) if(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); - } } j++; } @@ -119,7 +113,10 @@ static long int check_line(char *line, t_orders *orders) nbrs = get_numbers(line, &len); if(!is_ordered(nbrs, orders, len)) + { + free(nbrs); return(0); + } res = nbrs[(len / 2)]; free(nbrs); @@ -130,6 +127,7 @@ long int resolve_part1(char *input, char **split) { (void)input; t_orders *orders; + t_orders *tmp; long int res; orders = 0; @@ -144,5 +142,11 @@ long int resolve_part1(char *input, char **split) res += check_line(*split, orders); split++; } + while(orders) + { + tmp = orders->next; + free(orders); + orders = tmp; + } return(res); } diff --git a/2024/5/part2.c b/2024/5/part2.c index a1be9ce..278b13a 100644 --- a/2024/5/part2.c +++ b/2024/5/part2.c @@ -6,7 +6,7 @@ /* 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; t_orders *orders; + t_orders *tmp; long int res; orders = 0; @@ -149,5 +150,11 @@ long int resolve_part2(char *input, char **split) res += check_line(*split, orders); split++; } + while(orders) + { + tmp = orders->next; + free(orders); + orders = tmp; + } return(res); }