-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_swap.c
92 lines (84 loc) · 2.07 KB
/
push_swap.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: joleksia <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/15 09:07:17 by joleksia #+# #+# */
/* Updated: 2025/01/23 08:33:39 by joleksia ### ########.fr */
/* */
/* ************************************************************************** */
#include "./push_swap.h"
#include "libft/libft.h"
static int __ps_single_arg(t_stack *s, char *av);
int main(int ac, char **av)
{
t_stack a;
t_stack b;
if (ac < 2)
return (1);
else if (ac == 2)
{
if (!__ps_single_arg(&a, av[1]))
return (write(STDERR_FILENO, "Error\n", 6));
}
else
{
ps_stack(&a, ac - 1);
if (!ps_fill(&a, av + 1))
{
ps_free(&a);
return (write(STDERR_FILENO, "Error\n", 6));
}
}
ps_stack(&b, a.siz);
push_swap(&a, &b);
ps_free(&a);
ps_free(&b);
return (0);
}
int push_swap(t_stack *a, t_stack *b)
{
if (!ps_issorted(*a))
{
if (a->cnt <= 3)
{
while (!ps_issorted(*a))
ps_sort3(a);
}
else
{
ps_sort_atob(a, b);
while (!ps_issorted(*a))
ps_sort3(a);
ps_sort_btoa(a, b);
}
}
return (1);
}
int ps_issorted(t_stack stack)
{
while (stack.cnt-- > 1)
{
if (stack.dat[stack.cnt] < stack.dat[stack.cnt - 1])
return (0);
}
return (1);
}
static int __ps_single_arg(t_stack *s, char *av)
{
char **split;
split = ft_split(av, ' ');
if (!split)
return (0);
ps_stack(s, ft_arrsiz2d((void **) split));
if (!ps_fill(s, split))
{
ft_free2d((void **) split);
ps_free(s);
return (0);
}
ft_free2d((void **) split);
return (1);
}