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> +#+ +:+ +#+ */ /* 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/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; *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; static t_cache *cache;
int cur_objective[2]; 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; uint64_t res;
int i; int i;
if(clear)
{
clear_cache(cache);
return(0);
}
res = get_cache(cache, objectives, depth, pos[depth]); res = get_cache(cache, objectives, depth, pos[depth]);
i = 0; i = 0;
if(res) 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); get_char_pos(keypad, objectives[i], cur_objective);
path = plan_route(keypad, pos[depth], cur_objective); path = plan_route(keypad, pos[depth], cur_objective);
if(depth < 2) 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 else
res += ft_strlen(path); res += ft_strlen(path);
free(path); free(path);
@ -186,11 +204,12 @@ uint64_t resolve_part1(char *input, char **split)
res = 0; res = 0;
while(*split) 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++; split++;
} }
ft_free_str_arr((void *)pos); ft_free_str_arr((void *)pos);
ft_free_str_arr(keypad); ft_free_str_arr(keypad);
ft_free_str_arr(robot_pad); ft_free_str_arr(robot_pad);
get_path_len(0, 0, 0, 0, 0 ,1);
return(res); return(res);
} }

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* part1.c :+: :+: :+: */ /* part2.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* 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/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; *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; static t_cache *cache;
int cur_objective[2]; 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; uint64_t res;
int i; int i;
if(clear)
{
clear_cache(cache);
return(0);
}
res = get_cache(cache, objectives, depth, pos[depth]); res = get_cache(cache, objectives, depth, pos[depth]);
i = 0; i = 0;
if(res) 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); get_char_pos(keypad, objectives[i], cur_objective);
path = plan_route(keypad, pos[depth], cur_objective); path = plan_route(keypad, pos[depth], cur_objective);
if(depth < 25) 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 else
res += ft_strlen(path); res += ft_strlen(path);
free(path); free(path);
@ -186,11 +204,12 @@ uint64_t resolve_part2(char *input, char **split)
res = 0; res = 0;
while(*split) 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++; split++;
} }
ft_free_str_arr((void *)pos); ft_free_str_arr((void *)pos);
ft_free_str_arr(keypad); ft_free_str_arr(keypad);
ft_free_str_arr(robot_pad); ft_free_str_arr(robot_pad);
get_path_len(0, 0, 0, 0, 0 ,1);
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/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; *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) static int get_res(t_computer *input)
{ {
t_connected *tmp1; t_connected *tmp1;
@ -165,9 +177,35 @@ static int get_res(t_computer *input)
} }
input = input->next; input = input->next;
} }
free_done(done);
return(res); 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) long int resolve_part1(char *line, char **split)
{ {
(void)line; (void)line;
@ -182,5 +220,6 @@ long int resolve_part1(char *line, char **split)
split++; split++;
} }
res = get_res(input); res = get_res(input);
free_computer(input);
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/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) static int get_res(t_computer *input)
{ {
t_found *found; t_found *found;
@ -211,6 +223,7 @@ static int get_res(t_computer *input)
found = 0; found = 0;
tmp = 0; tmp = 0;
find_connected(&tmp,&found, input); find_connected(&tmp,&found, input);
free_found(tmp);
i = 0; i = 0;
tmp = found; tmp = found;
while(tmp) while(tmp)
@ -221,10 +234,15 @@ static int get_res(t_computer *input)
if(i > max_len) if(i > max_len)
{ {
max_len = i; max_len = i;
if(res)
free_found(res);
res = found; res = found;
} }
else
free_found(found);
input = input->next; input = input->next;
} }
tmp = res;
i = 0; i = 0;
while(res) while(res)
{ {
@ -236,9 +254,35 @@ static int get_res(t_computer *input)
i++; i++;
} }
printf("\n"); printf("\n");
free_found(tmp);
return(0); 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) long int resolve_part2(char *line, char **split)
{ {
(void)line; (void)line;
@ -253,5 +297,6 @@ long int resolve_part2(char *line, char **split)
split++; split++;
} }
res = get_res(input); res = get_res(input);
free_computer(input);
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/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; tmp = tmp->next;
} }
while(vars)
{
tmp = vars->next;
free(vars->op);
free(vars);
vars = 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/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; (void)input;
t_vars *vars; t_vars *vars;
t_vars *tmp;
vars = 0; vars = 0;
while((*split)[3] == ':') while((*split)[3] == ':')
@ -269,5 +270,12 @@ long int resolve_part2(char *input, char **split)
// show_wrong(vars); // show_wrong(vars);
get_wrong(vars); get_wrong(vars);
printf("order it yourself idgaf\n"); printf("order it yourself idgaf\n");
while(vars)
{
tmp = vars->next;
free(vars->op);
free(vars);
vars = tmp;
}
return(0); return(0);
} }

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/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; int i;
t_elem *lst; t_elem *lst;
t_elem *tmp;
int res; int res;
i = 1; i = 1;
@ -128,5 +129,11 @@ long int resolve_part1(char *input, char **split)
lst = get_all(split); lst = get_all(split);
ft_free_str_arr(split); ft_free_str_arr(split);
res = get_res(lst); res = get_res(lst);
while(lst)
{
tmp = lst->next;
free(lst);
lst = tmp;
}
return(res); return(res);
} }