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)
|
||||
{
|
||||
@ -280,6 +281,31 @@ long int get_result(t_pos_lst ***path, t_pos pos, char **map, uint32_t score)
|
||||
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..#.....#...#
|
||||
###############
|
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/12/17 18:40:12 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/12/18 23:29:22 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -126,7 +126,7 @@ long int resolve_part1(char *input, char **split)
|
||||
|
||||
parse_input(split, reg, &ops, &len);
|
||||
exec_instructions(ops, reg, len);
|
||||
printf("\nreg : %d, %d, %d", reg[0], reg[1], reg[2]);
|
||||
free(ops);
|
||||
printf("\n");
|
||||
return(0);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/12/18 01:15:31 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/12/18 23:32:19 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -133,14 +133,21 @@ int is_good(long int tmp, uint8_t *ops, size_t op_len, size_t cur_len)
|
||||
out = malloc(128);
|
||||
out_len = exec_instructions(ops, reg, op_len, out);
|
||||
if(out_len != cur_len)
|
||||
{
|
||||
free(out);
|
||||
return(0);
|
||||
}
|
||||
i = 0;
|
||||
while(i < out_len)
|
||||
while(i < out_len && i < op_len)
|
||||
{
|
||||
if(out[out_len - i - 1] != ops[op_len - i - 1])
|
||||
{
|
||||
free(out);
|
||||
return(0);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
free(out);
|
||||
return(1);
|
||||
}
|
||||
|
||||
@ -179,7 +186,10 @@ long int resolve_part2(char *input, char **split)
|
||||
long int reg[3];
|
||||
uint8_t *ops;
|
||||
size_t len;
|
||||
long int res;
|
||||
|
||||
parse_input(split, reg, &ops, &len);
|
||||
return(get_res(0, ops, len, 0));
|
||||
res = get_res(0, ops, len, 0);
|
||||
free(ops);
|
||||
return(res);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/12/18 16:05:18 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/12/19 00:49:19 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -43,6 +43,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)
|
||||
{
|
||||
@ -131,6 +132,18 @@ static int is_valid_move(char **map, int y, int x)
|
||||
return(x >= 0 && y >= 0 && map[y] && map[y][x] == '.');
|
||||
}
|
||||
|
||||
static void free_lst(t_lst *lst)
|
||||
{
|
||||
t_lst *tmp;
|
||||
|
||||
while(lst)
|
||||
{
|
||||
tmp = lst->next;
|
||||
free(lst);
|
||||
lst = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t dijkstra(char **map, t_pos *pos)
|
||||
{
|
||||
int dir[2];
|
||||
@ -148,7 +161,11 @@ static uint32_t dijkstra(char **map, t_pos *pos)
|
||||
if(map[pos->y][pos->x] == '#')
|
||||
continue;
|
||||
if(pos->y == 70 && pos->x == 70)
|
||||
{
|
||||
free_lst(lst);
|
||||
ft_free_str_arr(visited);
|
||||
return(score);
|
||||
}
|
||||
if(is_valid_move(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);
|
||||
@ -158,6 +175,7 @@ static uint32_t dijkstra(char **map, t_pos *pos)
|
||||
if(is_valid_move(map, pos->y + tmp[0], pos->x + tmp[1]))
|
||||
add_lst(&lst, score + 1, (t_pos){pos->x + tmp[1], pos->y + tmp[0]}, tmp);
|
||||
}
|
||||
ft_free_str_arr(visited);
|
||||
return(0);
|
||||
}
|
||||
|
||||
@ -212,8 +230,11 @@ long int resolve_part1(char *input, char **split)
|
||||
{
|
||||
char **map;
|
||||
(void)input;
|
||||
long int res;
|
||||
|
||||
map = create_map_size(71, '.');
|
||||
fill_map(map, split, 1024);
|
||||
return(dijkstra(map,&((t_pos){0, 0})));
|
||||
res = dijkstra(map,&((t_pos){0, 0}));
|
||||
ft_free_str_arr(map);
|
||||
return(res);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/12/18 16:29:43 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/12/19 00:48:24 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -43,6 +43,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)
|
||||
{
|
||||
@ -131,28 +132,40 @@ static int is_valid_move(char **map, char **visited, int y, int x)
|
||||
return(x >= 0 && y >= 0 && map[y] && map[y][x] == '.' && !visited[y][x]);
|
||||
}
|
||||
|
||||
static void free_lst(t_lst *lst)
|
||||
{
|
||||
t_lst *tmp;
|
||||
|
||||
while(lst)
|
||||
{
|
||||
tmp = lst->next;
|
||||
free(lst);
|
||||
lst = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t dijkstra(char **map, t_pos *pos, int corner)
|
||||
{
|
||||
int dir[2];
|
||||
int tmp[2];
|
||||
char **visited;
|
||||
uint32_t **dists;
|
||||
t_lst *lst;
|
||||
uint32_t score;
|
||||
|
||||
visited = create_map(map, 0, 1);
|
||||
dists = create_map(map, 255, sizeof(uint32_t));
|
||||
lst = 0;
|
||||
add_lst(&lst, 0, *pos, (int [2]){0, 1});
|
||||
while(get_new_pos(&lst, visited, pos, &score, dir))
|
||||
{
|
||||
if(dists[pos->y][pos->x] > score)
|
||||
dists[pos->y][pos->x] = score;
|
||||
visited[pos->y][pos->x] = 1;
|
||||
if(map[pos->y][pos->x] == '#')
|
||||
continue;
|
||||
if(pos->y == corner && pos->x == corner)
|
||||
{
|
||||
free_lst(lst);
|
||||
ft_free_str_arr(visited);
|
||||
return(score);
|
||||
}
|
||||
if(is_valid_move(map, visited, 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);
|
||||
@ -162,8 +175,7 @@ static uint32_t dijkstra(char **map, t_pos *pos, int corner)
|
||||
if(is_valid_move(map, visited, pos->y + tmp[0], pos->x + tmp[1]))
|
||||
add_lst(&lst, score + 1, (t_pos){pos->x + tmp[1], pos->y + tmp[0]}, tmp);
|
||||
}
|
||||
if(dists[corner][corner] != (uint32_t) -1)
|
||||
return(dists[corner][corner]);
|
||||
ft_free_str_arr(visited);
|
||||
return(0);
|
||||
}
|
||||
|
||||
@ -280,6 +292,7 @@ long int resolve_part2(char *input, char **split)
|
||||
size = 70;
|
||||
map = create_map_size(size + 1, '.');
|
||||
i = get_res_index(map, split, size);
|
||||
ft_free_str_arr(map);
|
||||
printf("real res p2 : %s\n", split[i]);
|
||||
return(i);
|
||||
}
|
||||
|
Reference in New Issue
Block a user