-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
126 lines (116 loc) · 2.63 KB
/
main.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zwalad <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/02 22:30:17 by zwalad #+# #+# */
/* Updated: 2022/03/16 23:41:26 by zwalad ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_swap *push_three(t_swap *a)
{
t_swap *tmp1;
t_swap *tmp2;
tmp1 = a->next;
tmp2 = tmp1->next;
if (a->stack > tmp1->stack && tmp1->stack > tmp2->stack)
{
a = swap_a(a);
a = r_rotate_a(a);
}
if (a->stack > tmp1->stack && a->stack < tmp2->stack)
a = swap_a(a);
if (a->stack > tmp2->stack && tmp2->stack > tmp1->stack)
a = rotate_a(a);
if (a->stack < tmp2->stack && tmp1->stack > tmp2->stack)
{
a = swap_a(a);
a = rotate_a(a);
}
if (a->stack < tmp1->stack && a->stack > tmp2->stack)
a = r_rotate_a(a);
return (a);
}
t_stacks *push_five2(t_stacks *p)
{
int i;
int j;
int k;
i = low_find(p->a);
j = le_index(i, p->a);
k = le_len(p->a);
if (j <= (k / 2))
{
while (j >= 1)
{
p->a = rotate_a(p->a);
j--;
}
}
else if (j > (k / 2))
{
while (j < k)
{
p->a = r_rotate_a(p->a);
j++;
}
}
return (p);
}
void only_two(t_swap *a)
{
t_swap *tmp;
tmp = a->next;
if (a->stack > tmp->stack)
swap_a(a);
exit(1);
}
t_stacks *push_five(t_stacks *p, int i)
{
if (i == 3)
only_two(p->a);
if (i == 4)
sub_three(p->a);
if (i == 6)
{
p = push_five2(p);
p->b = push_b(p);
}
p = push_five2(p);
p->b = push_b(p);
p->a = push_three(p->a);
p->a = push_a(p);
if (i == 6)
p->a = push_a(p);
return (p);
}
int main(int argc, char *argv[])
{
t_stacks p;
int i;
i = 1;
p.arg = argc - 1;
if (argc == 2)
herror(1);
check_dup(argv);
p = *stack_init(&p, argv, argc);
while (argv[i] != NULL)
ps_lstadd_back(p.a, ps_lstnew(ps_atoi(argv[i++])));
p.a = p.a->next;
if (check_sorted(&p))
exit(1);
if (argc <= 6)
p = *push_five(&p, argc);
if (argc > 6)
ps_looot(&p);
/* while (p.a != NULL)
{
printf("p.a = %d\n", p.a->stack);
p.a = p.a->next;
} */
system("leaks push_swap");
return (0);
}