diff --git a/2022/9/part1.c b/2022/9/part1.c index b63f903..9e63587 100644 --- a/2022/9/part1.c +++ b/2022/9/part1.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ -/* Updated: 2024/11/04 13:32:41 by tomoron ### ########.fr */ +/* Updated: 2024/12/19 18:17:10 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,32 +27,70 @@ t_instruction get_instruction(char *line) return(res); if(line[0] == 'U') - res.dir[1] = -1; - if(line[0] == 'D') - res.dir[1] = 1; - if(line[0] == 'L') res.dir[0] = -1; - if(line[0] == 'R') + if(line[0] == 'D') res.dir[0] = 1; + if(line[0] == 'L') + res.dir[1] = -1; + if(line[0] == 'R') + res.dir[1] = 1; res.rep = ft_atoi(line + 2); return(res); } static void move_tail(int tail[2], int head[2]) { - int dist_up; - int dist_side; + float dist; - dist_up = tail[0] - head[0]; - dist_side = tail[1] - head[1]; - if(dist_up == -2) - tail[0] = head[0] - 1; - if(dist_up == 2) - tail[0] = head[0] + 1; - if(dist_side == -2) - tail[1] = head[1] - 1; - if(dist_side == 2) - tail[1] = head[1] + 1; + dist = sqrt(pow(tail[0] - head[0], 2) + pow(tail[1] - head[1], 2)); + if(dist < 1.5) + return; + if(tail[0] < head[0]) + tail[0]++; + else if(tail[0] > head[0]) + tail[0]--; + if(tail[1] < head[1]) + tail[1]++; + else if(tail[1] > head[1]) + tail[1]--; + dist = sqrt(pow(tail[0] - head[0], 2) + pow(tail[1] - head[1], 2)); +} + +static void add_pos(t_pos **lst, int *pos) +{ + t_pos *tmp; + t_pos *last; + + tmp = *lst; + last = 0; + while(tmp) + { + if(tmp->pos[0] == pos[0] && tmp->pos[1] == pos[1]) + return; + last = tmp; + tmp = tmp->next; + } + tmp = malloc(sizeof(t_pos)); + tmp->pos[0] = pos[0]; + tmp->pos[1] = pos[1]; + tmp->next = 0; + if(!last) + *lst = tmp; + else + last->next = tmp; +} + +static long int len_pos(t_pos *lst) +{ + long int res; + + res = 0; + while(lst) + { + res++; + lst = lst->next; + } + return(res); } long int resolve_part1(char *input, char **split) @@ -62,22 +100,34 @@ long int resolve_part1(char *input, char **split) int head[2]; int tail[2]; t_instruction instr; + t_pos *lst; + t_pos *tmp; + long int res; i = 0; ft_bzero(head, sizeof(int) * 2); ft_bzero(tail, sizeof(int) * 2); + lst = 0; while(split[i]) { instr = get_instruction(split[i]); while(instr.rep > 0) { + add_pos(&lst, tail); head[0] += instr.dir[0]; head[1] += instr.dir[1]; - printf("%d, %d\n", head[0], head[1]); move_tail(tail, head); instr.rep--; } i++; } - return(0); + add_pos(&lst, tail); + res = len_pos(lst); + while(lst) + { + tmp = lst->next; + free(lst); + lst = tmp; + } + return(res); } diff --git a/2022/9/part2.c b/2022/9/part2.c index 35dff46..6ab1a4b 100644 --- a/2022/9/part2.c +++ b/2022/9/part2.c @@ -6,17 +6,135 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ -/* Updated: 2024/07/17 23:18:02 by tomoron ### ########.fr */ +/* Updated: 2024/12/19 18:23:03 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include #include +#include #include "libft/libft.h" +#include "structs.h" + +static t_instruction get_instruction(char *line) +{ + t_instruction res; + + res.dir[0] = 0; + res.dir[1] = 0; + res.rep = 0; + if(!line[0]) + return(res); + + if(line[0] == 'U') + res.dir[0] = -1; + if(line[0] == 'D') + res.dir[0] = 1; + if(line[0] == 'L') + res.dir[1] = -1; + if(line[0] == 'R') + res.dir[1] = 1; + res.rep = ft_atoi(line + 2); + return(res); +} + +static void move_tail(int tail[2], int head[2]) +{ + float dist; + + dist = sqrt(pow(tail[0] - head[0], 2) + pow(tail[1] - head[1], 2)); + if(dist < 1.5) + return; + if(tail[0] < head[0]) + tail[0]++; + else if(tail[0] > head[0]) + tail[0]--; + if(tail[1] < head[1]) + tail[1]++; + else if(tail[1] > head[1]) + tail[1]--; + dist = sqrt(pow(tail[0] - head[0], 2) + pow(tail[1] - head[1], 2)); +} + +static void add_pos(t_pos **lst, int *pos) +{ + t_pos *tmp; + t_pos *last; + + tmp = *lst; + last = 0; + while(tmp) + { + if(tmp->pos[0] == pos[0] && tmp->pos[1] == pos[1]) + return; + last = tmp; + tmp = tmp->next; + } + tmp = malloc(sizeof(t_pos)); + tmp->pos[0] = pos[0]; + tmp->pos[1] = pos[1]; + tmp->next = 0; + if(!last) + *lst = tmp; + else + last->next = tmp; +} + +static long int len_pos(t_pos *lst) +{ + long int res; + + res = 0; + while(lst) + { + res++; + lst = lst->next; + } + return(res); +} long int resolve_part2(char *input, char **split) { (void)input; - (void)split; - return(0); + int i; + int head[2]; + int tails[9][2]; + t_instruction instr; + t_pos *lst; + t_pos *tmp; + long int res; + + i = 0; + ft_bzero(head, sizeof(int) * 2); + while(i < 9) + ft_bzero(tails[i++], sizeof(int) * 2); + lst = 0; + while(*split) + { + instr = get_instruction(*split); + while(instr.rep > 0) + { + add_pos(&lst, tails[8]); + head[0] += instr.dir[0]; + head[1] += instr.dir[1]; + move_tail(tails[0], head); + i = 0; + while(i < 8) + { + move_tail(tails[i + 1], tails[i]); + i++; + } + instr.rep--; + } + split++; + } + add_pos(&lst, tails[8]); + res = len_pos(lst); + while(lst) + { + tmp = lst->next; + free(lst); + lst = tmp; + } + return(res); } diff --git a/2022/9/structs.h b/2022/9/structs.h index a5414d8..c78d8a6 100644 --- a/2022/9/structs.h +++ b/2022/9/structs.h @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/01 12:50:02 by tomoron #+# #+# */ -/* Updated: 2024/11/01 13:01:52 by tomoron ### ########.fr */ +/* Updated: 2024/12/19 18:08:53 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,4 +20,10 @@ typedef struct s_instruction int dir[2]; int rep; } t_instruction; + +typedef struct s_pos +{ + int pos[2]; + struct s_pos *next; +} t_pos; #endif