forked from sisittu99/push_swap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.c
132 lines (122 loc) · 2.91 KB
/
order.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* order.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcerchi <mcerchi@student.42roma.it> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/17 09:56:05 by mcerchi #+# #+# */
/* Updated: 2022/02/26 14:49:42 by mcerchi ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int ft_best_nbr_a(t_list *stack_a, int size, int *arr, int max)
{
int pos;
int i;
pos = 0;
i = 0;
while (stack_a != NULL)
{
i = 0;
while (stack_a->content != arr[i] && i < max)
{
if (i == max - 1)
return (pos);
i++;
}
stack_a = stack_a->next;
pos++;
}
size = -1;
return (size);
}
int ft_best_comb_helper(int *arr_a, int *arr_b, int *tmp, int size)
{
int i;
int pos;
pos = 0;
i = -1;
while (++i < size)
{
if (tmp[i] < tmp[pos])
pos = i;
}
free(arr_a);
free(arr_b);
free(tmp);
return (pos);
}
int ft_best_comb(int *arr_a, int *arr_b, int size)
{
int *tmp;
int i;
i = -1;
tmp = (int *) malloc (sizeof(int) * size);
if (!tmp || !arr_a || !arr_b)
ft_display_exit();
while (++i < size)
{
if ((arr_a[i] > 0 && arr_b[i] > 0)
|| (arr_a[i] < 0 && arr_b[i] < 0))
tmp[i] = ft_max_nbr(arr_a[i], arr_b[i]);
else
{
if (arr_a[i] < 0)
arr_a[i] *= -1;
if (arr_b[i] < 0)
arr_b[i] *= -1;
tmp[i] = arr_a[i] + arr_b[i];
}
}
return (ft_best_comb_helper(arr_a, arr_b, tmp, size));
}
int ft_move_a(int a, int b, t_list **stack_a, t_list **stack_b)
{
while (a < 0 && b < 0)
{
ft_rrr(stack_a, stack_b);
a++;
b++;
}
while (a > 0 && b > 0)
{
ft_rr(stack_a, stack_b);
a--;
b--;
}
if (a < 0)
while (a++ < 0)
ft_rra(stack_a);
else if (a > 0)
while (a-- > 0)
ft_ra(stack_a);
return (b);
}
int ft_best_nbr_b(t_list **stack_b, int size_b, t_list **stack_a, int size_a)
{
int *arr_b;
int *arr_a;
t_list *tmp_b;
int i;
i = -1;
tmp_b = *stack_b;
arr_a = (int *) malloc (sizeof(int) * size_b);
arr_b = (int *) malloc (sizeof(int) * size_b);
if (!arr_b || !arr_a)
ft_display_exit();
while (++i < size_b)
arr_b[i] = ft_the_needed_b(i, size_b);
i = -1;
while (++i < size_b && tmp_b != NULL)
{
arr_a[i] = ft_the_needed_a(*stack_a, tmp_b->content, size_a);
tmp_b = tmp_b->next;
}
i = ft_best_comb(ft_intcpy(arr_a, size_b),
ft_intcpy(arr_b, size_b), size_b);
i = ft_move_a(arr_a[i], arr_b[i], stack_a, stack_b);
free(arr_a);
free(arr_b);
return (i);
}