add 2022 day 9
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/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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user