fix some problems and fix leaks on days 16 to 18
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/12/17 12:55:09 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/12/18 23:21:58 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -50,6 +50,7 @@ void *create_map(char **map, uint8_t fill, int bloc_size)
|
||||
while(map[0][width])
|
||||
width++;
|
||||
res = malloc((height + 1) * sizeof(void *));
|
||||
res[height] = 0;
|
||||
i = 0;
|
||||
while(i < height)
|
||||
{
|
||||
@ -186,6 +187,18 @@ int *rotate_dir(int dir[2], int inv, int *res)
|
||||
return(res);
|
||||
}
|
||||
|
||||
static void free_lst(t_lst *lst)
|
||||
{
|
||||
t_lst *tmp;
|
||||
|
||||
while(lst)
|
||||
{
|
||||
tmp = lst->next;
|
||||
free(lst);
|
||||
lst = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t dijkstra(char **map, t_pos *pos)
|
||||
{
|
||||
int dir[2];
|
||||
@ -203,7 +216,11 @@ uint32_t dijkstra(char **map, t_pos *pos)
|
||||
if(map[pos->y][pos->x] == '#')
|
||||
continue;
|
||||
if(map[pos->y][pos->x] == 'E')
|
||||
{
|
||||
free_lst(lst);
|
||||
ft_free_str_arr(visited);
|
||||
return(score);
|
||||
}
|
||||
if(map[pos->y + dir[0]][pos->x + dir[1]] != '#')
|
||||
add_lst(&lst, score + 1, (t_pos){pos->x + dir[1], pos->y + dir[0]}, dir);
|
||||
rotate_dir(dir, 0, tmp);
|
||||
@ -213,6 +230,7 @@ uint32_t dijkstra(char **map, t_pos *pos)
|
||||
if(map[pos->y + tmp[0]][pos->x + tmp[1]] != '#')
|
||||
add_lst(&lst, score + 1000, *pos, tmp);
|
||||
}
|
||||
ft_free_str_arr(visited);
|
||||
return(0);
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/12/17 15:34:55 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/12/18 23:27:17 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -224,6 +224,7 @@ static void *create_map(char **map, uint8_t fill, int bloc_size)
|
||||
while(map[0][width])
|
||||
width++;
|
||||
res = malloc((height + 1) * sizeof(void *));
|
||||
res[height] = 0;
|
||||
i = 0;
|
||||
while(i < height)
|
||||
{
|
||||
@ -276,10 +277,35 @@ long int get_result(t_pos_lst ***path, t_pos pos, char **map, uint32_t score)
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return(res + 1);
|
||||
}
|
||||
|
||||
static void free_path(char **map, t_pos_lst ***path)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
t_pos_lst *tmp;
|
||||
|
||||
i = 0;
|
||||
while(map[i])
|
||||
{
|
||||
j = 0;
|
||||
while(map[i][j])
|
||||
{
|
||||
while(path[i][j])
|
||||
{
|
||||
tmp = path[i][j]->next;
|
||||
free(path[i][j]);
|
||||
path[i][j] = tmp;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
ft_free_str_arr((char **)path);
|
||||
}
|
||||
|
||||
long int resolve_part2(char *input, char **split)
|
||||
{
|
||||
(void)input;
|
||||
@ -295,5 +321,7 @@ long int resolve_part2(char *input, char **split)
|
||||
score = dijkstra(split, &pos, visited, path);
|
||||
get_char(split, &pos, 'E');
|
||||
res = get_result(path, pos, split, score);
|
||||
ft_free_str_arr(visited);
|
||||
free_path(split, path);
|
||||
return(res);
|
||||
}
|
||||
|
15
2024/16/test
Normal file
15
2024/16/test
Normal file
@ -0,0 +1,15 @@
|
||||
###############
|
||||
#.......#....E#
|
||||
#.#.###.#.###.#
|
||||
#.....#.#...#.#
|
||||
#.###.#####.#.#
|
||||
#.#.#.......#.#
|
||||
#.#.#####.###.#
|
||||
#...........#.#
|
||||
###.#.#####.#.#
|
||||
#...#.....#.#.#
|
||||
#.#.#.###.#.#.#
|
||||
#.....#...#.#.#
|
||||
#.###.#.#.#.#.#
|
||||
#S..#.....#...#
|
||||
###############
|
Reference in New Issue
Block a user