-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_return.c
76 lines (69 loc) · 2 KB
/
ft_return.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_return.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marias-e <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/13 13:28:59 by marias-e #+# #+# */
/* Updated: 2023/02/08 17:00:18 by marias-e ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int ft_fastest_path(t_list **stack_a, size_t index)
{
if ((*stack_a)->index > index && ft_lstlast(*stack_a)->index < index)
return (0);
if (ft_min_max(stack_a, index))
return (ft_path_mm(stack_a));
return (ft_general_path(stack_a, index));
}
static int ft_check_groups(t_list **stack_b, size_t current)
{
t_list *iter;
iter = *stack_b;
while (iter)
{
if (iter->group == current)
return (0);
iter = iter->next;
}
return (1);
}
static void ft_fix_order(t_list **stack_a, t_list **mov)
{
int n;
int r;
t_list *iter;
n = 1;
r = 1;
iter = (*stack_a);
if (!ft_check_order(*stack_a))
return ;
while (iter->index < iter->next->index)
{
n++;
iter = iter->next;
}
iter = ft_lstlast(*stack_a);
while (iter->index > iter->prev->index)
{
r++;
iter = iter->prev;
}
if (n > r)
ft_rotator_a(stack_a, mov, -r);
else
ft_rotator_a(stack_a, mov, n);
}
void ft_return(t_list **stack_a, t_list **stack_b, t_list **mov,
size_t current)
{
while (*stack_b)
{
while (ft_check_groups(stack_b, current))
current--;
ft_fit(stack_a, stack_b, mov, current);
}
ft_fix_order(stack_a, mov);
}