From fa519ac929bc9563b40ad5368bec8d41c49f4d7d Mon Sep 17 00:00:00 2001 From: tomoron Date: Sun, 29 Dec 2024 23:54:34 +0100 Subject: [PATCH] add free on days 21 to 25, nothing leaks now (maybe) --- 2024/21/part1.c | 27 +++++++++++++++++++++++---- 2024/21/part2.c | 29 ++++++++++++++++++++++++----- 2024/23/part1.c | 41 ++++++++++++++++++++++++++++++++++++++++- 2024/23/part2.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 2024/24/part1.c | 9 ++++++++- 2024/24/part2.c | 10 +++++++++- 2024/25/part1.c | 9 ++++++++- 7 files changed, 158 insertions(+), 14 deletions(-) diff --git a/2024/21/part1.c b/2024/21/part1.c index 3d035e0..951af8a 100644 --- a/2024/21/part1.c +++ b/2024/21/part1.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ -/* Updated: 2024/12/22 20:25:29 by tomoron ### ########.fr */ +/* Updated: 2024/12/28 11:39:36 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -120,7 +120,20 @@ static void add_cache(t_cache **cache, char *str, int depth, int *pos, uint64_t *cache = new; } -static uint64_t get_path_len(char **keypad, char **robot_pad, int **pos, char *objectives, int depth) +static void clear_cache(t_cache *cache) +{ + t_cache *tmp; + + while(cache) + { + tmp = cache->next; + free(cache->str); + free(cache); + cache = tmp; + } +} + +static uint64_t get_path_len(char **keypad, char **robot_pad, int **pos, char *objectives, int depth, int clear) { static t_cache *cache; int cur_objective[2]; @@ -128,6 +141,11 @@ static uint64_t get_path_len(char **keypad, char **robot_pad, int **pos, char *o uint64_t res; int i; + if(clear) + { + clear_cache(cache); + return(0); + } res = get_cache(cache, objectives, depth, pos[depth]); i = 0; if(res) @@ -139,7 +157,7 @@ static uint64_t get_path_len(char **keypad, char **robot_pad, int **pos, char *o get_char_pos(keypad, objectives[i], cur_objective); path = plan_route(keypad, pos[depth], cur_objective); if(depth < 2) - res += get_path_len(keypad, robot_pad, pos, path, depth + 1); + res += get_path_len(keypad, robot_pad, pos, path, depth + 1, 0); else res += ft_strlen(path); free(path); @@ -186,11 +204,12 @@ uint64_t resolve_part1(char *input, char **split) res = 0; while(*split) { - res += get_path_len(keypad, robot_pad, pos, *split, 0) * ft_atoi(*split); + res += get_path_len(keypad, robot_pad, pos, *split, 0, 0) * ft_atoi(*split); split++; } ft_free_str_arr((void *)pos); ft_free_str_arr(keypad); ft_free_str_arr(robot_pad); + get_path_len(0, 0, 0, 0, 0 ,1); return(res); } diff --git a/2024/21/part2.c b/2024/21/part2.c index 8917d75..ccc264b 100644 --- a/2024/21/part2.c +++ b/2024/21/part2.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* part1.c :+: :+: :+: */ +/* part2.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ -/* Updated: 2024/12/22 20:25:16 by tomoron ### ########.fr */ +/* Updated: 2024/12/28 11:39:29 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -120,7 +120,20 @@ static void add_cache(t_cache **cache, char *str, int depth, int *pos, uint64_t *cache = new; } -static uint64_t get_path_len(char **keypad, char **robot_pad, int **pos, char *objectives, int depth) +static void clear_cache(t_cache *cache) +{ + t_cache *tmp; + + while(cache) + { + tmp = cache->next; + free(cache->str); + free(cache); + cache = tmp; + } +} + +static uint64_t get_path_len(char **keypad, char **robot_pad, int **pos, char *objectives, int depth, int clear) { static t_cache *cache; int cur_objective[2]; @@ -128,6 +141,11 @@ static uint64_t get_path_len(char **keypad, char **robot_pad, int **pos, char *o uint64_t res; int i; + if(clear) + { + clear_cache(cache); + return(0); + } res = get_cache(cache, objectives, depth, pos[depth]); i = 0; if(res) @@ -139,7 +157,7 @@ static uint64_t get_path_len(char **keypad, char **robot_pad, int **pos, char *o get_char_pos(keypad, objectives[i], cur_objective); path = plan_route(keypad, pos[depth], cur_objective); if(depth < 25) - res += get_path_len(keypad, robot_pad, pos, path, depth + 1); + res += get_path_len(keypad, robot_pad, pos, path, depth + 1, 0); else res += ft_strlen(path); free(path); @@ -186,11 +204,12 @@ uint64_t resolve_part2(char *input, char **split) res = 0; while(*split) { - res += get_path_len(keypad, robot_pad, pos, *split, 0) * ft_atoi(*split); + res += get_path_len(keypad, robot_pad, pos, *split, 0, 0) * ft_atoi(*split); split++; } ft_free_str_arr((void *)pos); ft_free_str_arr(keypad); ft_free_str_arr(robot_pad); + get_path_len(0, 0, 0, 0, 0 ,1); return(res); } diff --git a/2024/23/part1.c b/2024/23/part1.c index 60c359a..f7a095b 100644 --- a/2024/23/part1.c +++ b/2024/23/part1.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/2024/23/part2.c b/2024/23/part2.c index 72361d1..6503a52 100644 --- a/2024/23/part2.c +++ b/2024/23/part2.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/2024/24/part1.c b/2024/24/part1.c index b7d16c5..3dcd9a3 100644 --- a/2024/24/part1.c +++ b/2024/24/part1.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ -/* Updated: 2024/12/24 16:05:51 by tomoron ### ########.fr */ +/* Updated: 2024/12/29 23:51:34 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -144,5 +144,12 @@ long int resolve_part1(char *input, char **split) } tmp = tmp->next; } + while(vars) + { + tmp = vars->next; + free(vars->op); + free(vars); + vars = tmp; + } return(res); } diff --git a/2024/24/part2.c b/2024/24/part2.c index cdb7680..ebc8854 100644 --- a/2024/24/part2.c +++ b/2024/24/part2.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ -/* Updated: 2024/12/24 18:44:00 by tomoron ### ########.fr */ +/* Updated: 2024/12/29 23:51:59 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -254,6 +254,7 @@ long int resolve_part2(char *input, char **split) { (void)input; t_vars *vars; + t_vars *tmp; vars = 0; while((*split)[3] == ':') @@ -269,5 +270,12 @@ long int resolve_part2(char *input, char **split) // show_wrong(vars); get_wrong(vars); printf("order it yourself idgaf\n"); + while(vars) + { + tmp = vars->next; + free(vars->op); + free(vars); + vars = tmp; + } return(0); } diff --git a/2024/25/part1.c b/2024/25/part1.c index 35161e9..97c9195 100644 --- a/2024/25/part1.c +++ b/2024/25/part1.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ -/* Updated: 2024/12/25 12:08:33 by tomoron ### ########.fr */ +/* Updated: 2024/12/29 23:53:18 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -112,6 +112,7 @@ long int resolve_part1(char *input, char **split) { int i; t_elem *lst; + t_elem *tmp; int res; i = 1; @@ -128,5 +129,11 @@ long int resolve_part1(char *input, char **split) lst = get_all(split); ft_free_str_arr(split); res = get_res(lst); + while(lst) + { + tmp = lst->next; + free(lst); + lst = tmp; + } return(res); }