diff --git a/2024/16/Makefile b/2024/16/Makefile new file mode 100644 index 0000000..808a501 --- /dev/null +++ b/2024/16/Makefile @@ -0,0 +1,69 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: tomoron +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/07/17 15:48:36 by tomoron #+# #+# # +# Updated: 2024/10/29 20:43:12 by tomoron ### ########.fr # +# # +# **************************************************************************** # + +SRCS = main.c\ + part1.c\ + part2.c + +OBJS = $(SRCS:.c=.o) + +FLAGS = -Wextra -Werror -Wall -g + +LIB = libft/libft.a + +all: $(LIB) a.out test + cp test cur_input + ./a.out + +f: $(LIB) a.out input + cp input cur_input + ./a.out + +test: + touch test + +input: + touch input + +cp: + cp part1.c part2.c + sed -i 's/resolve_part1/resolve_part2/g' part2.c + +$(LIB): libft + make -C libft + +libft: + cp -r ~/Desktop/aoc/libft . + +main.c: + cp ~/Desktop/aoc/main.c . + +part1.c: + cp ~/Desktop/aoc/part1.c . + +part2.c: + cp ~/Desktop/aoc/part2.c . + +a.out: main.c $(OBJS) $(LIB) + clang $(FLAGS) $(OBJS) $(LIB) + +clean: + rm -rf $(OBJS) + rm -rf libft + +fclean: clean + rm -rf a.out + +.c.o: + clang $(FLAGS) -c $< -o $@ + +.PHONY: t all clean fclean cp diff --git a/2024/16/main.c b/2024/16/main.c new file mode 100644 index 0000000..4f9b609 --- /dev/null +++ b/2024/16/main.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include "libft/libft.h" + +long int resolve_part1(char *input, char **split); +long int resolve_part2(char *input, char **split); + +char *get_input(char *file) +{ + int fd; + int rd_len; + char buffer[1000]; + int len; + char *res; + + fd = open(file, O_RDONLY); + len = 0; + if(fd < 0) + return(0); + rd_len = -1; + while(rd_len) + { + rd_len = read(fd, buffer, 1000); + len+= rd_len; + } + close(fd); + res = malloc(len + 1); + if(!res) + return(0); + fd = open(file, O_RDONLY); + if(fd < 0) + return(0); + rd_len = read(fd,res, len); + res[len] = 0; + close(fd); + return(res); +} + +int main(void) +{ + char *input; + char *input_cpy; + char **split; + + input = get_input("cur_input"); + input_cpy = ft_strdup(input); + if(!input || !input_cpy) + { + fprintf(stderr, "file read error\n"); + return(1); + } + split = ft_split(input, '\n'); + if(!split) + return(1); + printf("result part 1: %ld\n", resolve_part1(input, split)); + ft_free_str_arr(split); + split = ft_split(input_cpy, '\n'); + if(!split) + return(1); + printf("result part 2: %ld\n", resolve_part2(input, split)); + ft_free_str_arr(split); + free(input); + free(input_cpy); + return(0); +} diff --git a/2024/16/part1.c b/2024/16/part1.c index 0ac09a4..b193cc2 100644 --- a/2024/16/part1.c +++ b/2024/16/part1.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ -/* Updated: 2024/12/17 03:44:38 by tomoron ### ########.fr */ +/* Updated: 2024/12/17 12:55:09 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -88,7 +88,20 @@ int is_visited(t_vis *vis, t_pos pos, int dir[2]) return(0); } -int get_new_pos(t_lst **lst, t_vis *vis, t_pos *pos, uint32_t *score, int *dir) +static char dir_to_bit(int *dir) +{ + if(dir[1] == 1) + return(0b1); + if(dir[1] == -1) + return(0b10); + if(dir[0] == 1) + return(0b100); + if(dir[0] == -1) + return(0b1000); + return(0); +} + +int get_new_pos(t_lst **lst, char **vis, t_pos *pos, uint32_t *score, int *dir) { t_lst *cur; t_lst *sel; @@ -125,8 +138,8 @@ int get_new_pos(t_lst **lst, t_vis *vis, t_pos *pos, uint32_t *score, int *dir) dir[0] = sel->dir[0]; dir[1] = sel->dir[1]; free(sel); - if(is_visited(vis, *pos, dir)) - get_new_pos(lst, vis, pos, score, dir); + if(vis[pos->y][pos->x] & (dir_to_bit(dir))) + return(get_new_pos(lst, vis, pos, score, dir)); return(1); } @@ -177,25 +190,28 @@ uint32_t dijkstra(char **map, t_pos *pos) { int dir[2]; int tmp[2]; - t_vis *visited; + char **visited; t_lst *lst; uint32_t score; - visited = 0; + visited = create_map(map, 0, 1); lst = 0; add_lst(&lst, 0, *pos, (int [2]){0, 1}); while(get_new_pos(&lst, visited, pos, &score, dir)) { - visited_add(&visited, pos, dir); + visited[pos->y][pos->x] |= dir_to_bit(dir); if(map[pos->y][pos->x] == '#') continue; if(map[pos->y][pos->x] == 'E') return(score); - add_lst(&lst, score + 1, (t_pos){pos->x + dir[1], pos->y + dir[0]}, dir); + 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); - add_lst(&lst, score + 1000, *pos, tmp); + if(map[pos->y + tmp[0]][pos->x + tmp[1]] != '#') + add_lst(&lst, score + 1000, *pos, tmp); rotate_dir(dir, 1, tmp); - add_lst(&lst, score + 1000, *pos, tmp); + if(map[pos->y + tmp[0]][pos->x + tmp[1]] != '#') + add_lst(&lst, score + 1000, *pos, tmp); } return(0); } diff --git a/2024/16/part2.c b/2024/16/part2.c new file mode 100644 index 0000000..ba6d0c6 --- /dev/null +++ b/2024/16/part2.c @@ -0,0 +1,299 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2024/12/17 15:34:55 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include "libft/libft.h" + +typedef struct s_pos +{ + int x; + int y; +} t_pos; + +typedef struct s_lst +{ + t_pos pos; + t_pos from; + uint32_t score; + int dir[2]; + struct s_lst *next; + struct s_lst *prev; +} t_lst; + +typedef struct s_pos_lst +{ + int x; + int y; + uint32_t score; + struct s_pos_lst *next; +} t_pos_lst; + + +static void get_char(char **map, t_pos *pos, char c) +{ + pos->y = 0; + while(map[pos->y]) + { + pos->x = 0; + while(map[pos->y][pos->x]) + { + if(map[pos->y][pos->x] == c) + return; + pos->x++; + } + pos->y++; + } +} + +static char dir_to_bit(int *dir) +{ + if(dir[1] == 1) + return(0b1); + if(dir[1] == -1) + return(0b10); + if(dir[0] == 1) + return(0b100); + if(dir[0] == -1) + return(0b1000); + return(0); +} + +static int get_new_pos(t_lst **lst, char **vis, t_pos *pos, uint32_t *score, int *dir, t_pos *from) +{ + t_lst *cur; + t_lst *sel; + t_lst *p_sel; + t_lst *prev; + uint32_t min; + + if(!*lst) + return(0); + cur = *lst; + sel = 0; + p_sel = 0; + prev = 0; + min = -1; + while(cur) + { + if(cur->score < min) + { + sel = cur; + p_sel = prev; + min = cur->score; + } + prev = cur; + cur = cur->next; + } + if(*lst && sel == *lst) + *lst = (*lst)->next; + if(p_sel) + p_sel->next = sel->next; + sel->next = 0; + *score = sel->score; + pos->x = sel->pos.x; + pos->y = sel->pos.y; + dir[0] = sel->dir[0]; + dir[1] = sel->dir[1]; + from->x = sel->from.x; + from->y = sel->from.y; + free(sel); + if(vis[pos->y][pos->x] & (dir_to_bit(dir))) + return(get_new_pos(lst, vis, pos, score, dir, from)); + return(1); +} + +static void add_lst(t_lst **lst, uint32_t score, t_pos pos, int dir[2], t_pos *from) +{ + t_lst *new; + + new = malloc(sizeof(t_lst)); + new->dir[0] = dir[0]; + new->dir[1] = dir[1]; + new->pos.x = pos.x; + new->pos.y = pos.y; + new->score = score; + new->next = *lst; + if(from) + { + new->from.x = from->x; + new->from.y = from->y; + } + else + { + new->from.x = -1; + new->from.y = -1; + } + *lst = new; +} + +static int *rotate_dir(int dir[2], int inv, int *res) +{ + res[0] = 0; + res[1] = 0; + if(dir[0]) + res[1] = dir[0]; + if(dir[1]) + res[0] = dir[1]; + if(inv) + { + res[0] = -res[0]; + res[1] = -res[1]; + } + return(res); +} + +static void add_path(t_pos_lst **path, t_pos pos, uint32_t score) +{ + t_pos_lst *new; + t_pos_lst *tmp; + + if(*path && (*path)->score > score) + *path = 0; + tmp = *path; + while(tmp) + { + if(tmp->x == pos.x && tmp->y == pos.y) + return; + tmp = tmp->next; + } +// if(*path && (*path)->score < score) +// return ; + new = malloc(sizeof(t_pos_lst)); + new->score = score; + new->x = pos.x; + new->y = pos.y; + new->next = *path; + *path = new; +} + +static uint32_t dijkstra(char **map, t_pos *pos, char **visited, t_pos_lst ***path) +{ + int dir[2]; + int tmp[2]; + t_lst *lst; + t_pos from; + uint32_t score; + uint32_t min_score; + + lst = 0; + min_score = -1; + add_lst(&lst, 0, *pos, (int [2]){0, 1}, 0); + while(get_new_pos(&lst, visited, pos, &score, dir, &from)) + { + visited[pos->y][pos->x] |= dir_to_bit(dir); + if(map[pos->y][pos->x] == '#') + continue; + if(map[pos->y][pos->x] == 'E' && min_score > score) + min_score = score; + if(from.x != -1 && from.y != -1) + add_path(path[pos->y] + pos->x, from, 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, pos); + rotate_dir(dir, 0, tmp); + if(map[pos->y + tmp[0]][pos->x + tmp[1]] != '#') + add_lst(&lst, score + 1000, *pos, tmp, &from); + rotate_dir(dir, 1, tmp); + if(map[pos->y + tmp[0]][pos->x + tmp[1]] != '#') + add_lst(&lst, score + 1000, *pos, tmp, &from); + } + return (min_score); +} + +static void *create_map(char **map, uint8_t fill, int bloc_size) +{ + int width; + int height; + int i; + void **res; + + height = 0; + while(map[height]) + height++; + width = 0; + while(map[0][width]) + width++; + res = malloc((height + 1) * sizeof(void *)); + i = 0; + while(i < height) + { + res[i] = malloc((width + 1) * bloc_size); + ft_memset(res[i], fill, width * bloc_size); + ((char **)res)[i][width * bloc_size] = 0; + i++; + } + return(res); +} + +static int fill_path_map(t_pos_lst ***path, int x, int y, char **map, uint32_t score) +{ + t_pos_lst *cur; + int res; + + res = 0; + + cur = path[y][x]; + if(map[y][x] == 'S') + return(1); + while(cur) + { + if(cur->score <= score) + res += fill_path_map(path, cur->x, cur->y, map, cur->score); + cur = cur->next; + } + if(res) + map[y][x] = 'O'; + return(res); +} + +long int get_result(t_pos_lst ***path, t_pos pos, char **map, uint32_t score) +{ + long int res; + int i; + int j; + + res = 0; + fill_path_map(path, pos.x, pos.y, map, score); + i = 0; + while(map[i]) + { + printf("%s\n", map[i]); + j = 0; + while(map[i][j]) + { + if(map[i][j] == 'O') + res++; + j++; + } + i++; + } + return(res + 1); +} + +long int resolve_part2(char *input, char **split) +{ + (void)input; + t_pos pos; + t_pos_lst ***path; + char **visited; + uint32_t score; + long int res; + + get_char(split, &pos, 'S'); + visited = create_map(split, 0, 1); + path = create_map(split, 0, sizeof(t_pos_lst *)); + score = dijkstra(split, &pos, visited, path); + get_char(split, &pos, 'E'); + res = get_result(path, pos, split, score); + return(res); +}