add 2022 day 9
This commit is contained in:
@ -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/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);
|
return(res);
|
||||||
|
|
||||||
if(line[0] == 'U')
|
if(line[0] == 'U')
|
||||||
res.dir[1] = -1;
|
|
||||||
if(line[0] == 'D')
|
|
||||||
res.dir[1] = 1;
|
|
||||||
if(line[0] == 'L')
|
|
||||||
res.dir[0] = -1;
|
res.dir[0] = -1;
|
||||||
if(line[0] == 'R')
|
if(line[0] == 'D')
|
||||||
res.dir[0] = 1;
|
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);
|
res.rep = ft_atoi(line + 2);
|
||||||
return(res);
|
return(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void move_tail(int tail[2], int head[2])
|
static void move_tail(int tail[2], int head[2])
|
||||||
{
|
{
|
||||||
int dist_up;
|
float dist;
|
||||||
int dist_side;
|
|
||||||
|
|
||||||
dist_up = tail[0] - head[0];
|
dist = sqrt(pow(tail[0] - head[0], 2) + pow(tail[1] - head[1], 2));
|
||||||
dist_side = tail[1] - head[1];
|
if(dist < 1.5)
|
||||||
if(dist_up == -2)
|
return;
|
||||||
tail[0] = head[0] - 1;
|
if(tail[0] < head[0])
|
||||||
if(dist_up == 2)
|
tail[0]++;
|
||||||
tail[0] = head[0] + 1;
|
else if(tail[0] > head[0])
|
||||||
if(dist_side == -2)
|
tail[0]--;
|
||||||
tail[1] = head[1] - 1;
|
if(tail[1] < head[1])
|
||||||
if(dist_side == 2)
|
tail[1]++;
|
||||||
tail[1] = head[1] + 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)
|
long int resolve_part1(char *input, char **split)
|
||||||
@ -62,22 +100,34 @@ long int resolve_part1(char *input, char **split)
|
|||||||
int head[2];
|
int head[2];
|
||||||
int tail[2];
|
int tail[2];
|
||||||
t_instruction instr;
|
t_instruction instr;
|
||||||
|
t_pos *lst;
|
||||||
|
t_pos *tmp;
|
||||||
|
long int res;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
ft_bzero(head, sizeof(int) * 2);
|
ft_bzero(head, sizeof(int) * 2);
|
||||||
ft_bzero(tail, sizeof(int) * 2);
|
ft_bzero(tail, sizeof(int) * 2);
|
||||||
|
lst = 0;
|
||||||
while(split[i])
|
while(split[i])
|
||||||
{
|
{
|
||||||
instr = get_instruction(split[i]);
|
instr = get_instruction(split[i]);
|
||||||
while(instr.rep > 0)
|
while(instr.rep > 0)
|
||||||
{
|
{
|
||||||
|
add_pos(&lst, tail);
|
||||||
head[0] += instr.dir[0];
|
head[0] += instr.dir[0];
|
||||||
head[1] += instr.dir[1];
|
head[1] += instr.dir[1];
|
||||||
printf("%d, %d\n", head[0], head[1]);
|
|
||||||
move_tail(tail, head);
|
move_tail(tail, head);
|
||||||
instr.rep--;
|
instr.rep--;
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return(0);
|
add_pos(&lst, tail);
|
||||||
|
res = len_pos(lst);
|
||||||
|
while(lst)
|
||||||
|
{
|
||||||
|
tmp = lst->next;
|
||||||
|
free(lst);
|
||||||
|
lst = tmp;
|
||||||
|
}
|
||||||
|
return(res);
|
||||||
}
|
}
|
||||||
|
124
2022/9/part2.c
124
2022/9/part2.c
@ -6,17 +6,135 @@
|
|||||||
/* 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/07/17 23:18:02 by tomoron ### ########.fr */
|
/* Updated: 2024/12/19 18:23:03 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
#include "libft/libft.h"
|
#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)
|
long int resolve_part2(char *input, char **split)
|
||||||
{
|
{
|
||||||
(void)input;
|
(void)input;
|
||||||
(void)split;
|
int i;
|
||||||
return(0);
|
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);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/11/01 12:50:02 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 dir[2];
|
||||||
int rep;
|
int rep;
|
||||||
} t_instruction;
|
} t_instruction;
|
||||||
|
|
||||||
|
typedef struct s_pos
|
||||||
|
{
|
||||||
|
int pos[2];
|
||||||
|
struct s_pos *next;
|
||||||
|
} t_pos;
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user