-
Notifications
You must be signed in to change notification settings - Fork 0
/
pushswap.c
91 lines (81 loc) · 2.25 KB
/
pushswap.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pushswap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebennamr <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 18:01:53 by ebennamr #+# #+# */
/* Updated: 2023/02/14 18:33:36 by ebennamr ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
static void tree_sort(t_int2 *t, int im)
{
if (im == 0 && t->pt[1] > t->pt[2])
{
rx(t, RA);
sx(t, SA);
}
else if (im == 0 && t->pt[1] < t->pt[2])
rx(t, RA);
else if (im == 1 && t->pt[0] > t->pt[2])
rrx(t, RRA);
else if (im == 1 && t->pt[0] < t->pt[2])
{
rrx(t, RRA);
sx(t, SA);
}
else if (im == 2 && t->pt[0] > t->pt[1])
sx(t, SA);
}
static void alogo(t_data *data)
{
t_cmd cmd;
while (data->stack_b->len > 0)
{
cmd = searchofbest_move(data);
excute_move(data, cmd);
}
rotate_to_sort(data);
}
static void step2(t_data *data)
{
t_int2 *lis;
if (data->stack_a->len == 2)
rotate_to_sort(data);
else if (data->stack_a->len == 3)
tree_sort(data->stack_a, indexofmax(data->stack_a->pt, 3));
else if (data->stack_a->len == 5)
{
px(data->stack_a, data->stack_b, data->maxisze, PB);
px(data->stack_a, data->stack_b, data->maxisze, PB);
tree_sort(data->stack_a, indexofmax(data->stack_a->pt, 3));
alogo(data);
}
else
{
lis = get_lis(data->stack_a->pt, data->stack_a->len);
extract_lis(data, lis);
alogo(data);
free(lis->pt);
free(lis);
}
}
static void step1(t_data *data, int ac, char **av)
{
char *tmp;
tmp = join_args(ac, av);
parse(data, tmp);
checksort(data->stack_a);
free(tmp);
}
int main(int ac, char **av)
{
t_data data;
if (ac == 1)
exit(0);
step1(&data, ac, av);
step2(&data);
checksort(data.stack_a);
}