-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_swap.c
More file actions
84 lines (78 loc) · 2.12 KB
/
push_swap.c
File metadata and controls
84 lines (78 loc) · 2.12 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cumoncoq <cumoncoq@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/29 15:37:16 by cumoncoq #+# #+# */
/* Updated: 2023/11/30 19:56:10 by cumoncoq ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void ft_choose_and_push_a(t_ring **a, t_ring **b)
{
if (!b || !a)
return ;
ft_reset(*a);
ft_reset(*b);
ft_all_target_a(*a, *b);
ft_both_dist(*a);
ft_both_dist(*b);
ft_set_all_costs(*a);
ft_rotate_cheapest_a(a, b);
ft_push_b(a, b);
}
static void ft_choose_and_push_b(t_ring **a, t_ring **b)
{
if (!b || !a)
return ;
ft_reset(*a);
ft_reset(*b);
ft_all_target_b(*a, *b);
ft_both_dist(*a);
ft_both_dist(*b);
ft_set_all_costs(*b);
ft_rotate_cheapest_b(a, b);
ft_push_a(a, b);
}
static void ft_order(t_ring **a, t_ring **b)
{
if (ft_len_ring(*a) > 3)
ft_push_b(a, b);
if (ft_len_ring(*a) > 3)
ft_push_b(a, b);
while (ft_len_ring(*a) > 3)
ft_choose_and_push_a(a, b);
ft_order_three(a);
while (ft_len_ring(*b) > 0)
ft_choose_and_push_b(a, b);
ft_final_rotate(a);
}
int main(int ac, char **av)
{
t_ring *a;
t_ring *b;
a = NULL;
b = NULL;
if (ac < 2)
return (1);
ft_parse_args(ac, av, &a);
if (a && ft_check_double(&a))
{
ft_clear_ring(&a);
ft_putstr_fd("Error\n", 2);
}
if (!ft_is_sorted(a))
{
if (ft_len_ring(a) > 3)
ft_order(&a, &b);
else if (ft_len_ring(a) == 3)
ft_order_three(&a);
else if (ft_len_ring(a) == 2)
ft_swap_a(&a);
}
ft_clear_ring(&a);
ft_clear_ring(&b);
return (0);
}