-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimised_push_b.c
105 lines (97 loc) · 2.47 KB
/
optimised_push_b.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* optimised_push_b.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhlim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/08 16:04:21 by zhlim #+# #+# */
/* Updated: 2023/08/10 17:09:50 by zhlim ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void run_op2(t_list **stack_a, t_list **stack_b, t_content *content, char c)
{
if (content->rrb)
{
single_reverse_rotate(stack_b, "rrb\n");
content->rrb--;
}
else if (content->rrr)
{
double_reverse_rotate(stack_a, stack_b, "rrr\n");
content->rrr--;
}
else if (c == 'a')
{
push(stack_b, stack_a, "pa\n");
return ;
}
else if (c == 'b')
{
push(stack_a, stack_b, "pb\n");
return ;
}
else
return ;
run_op2(stack_a, stack_b, content, c);
}
void run_op(t_list **stack_a, t_list **stack_b, t_content *content, char c)
{
if (content->ra)
{
single_rotate(stack_a, "ra\n");
content->ra--;
}
else if (content->rb)
{
single_rotate(stack_b, "rb\n");
content->rb--;
}
else if (content->rr)
{
double_rotate(stack_a, stack_b, "rr\n");
content->rr--;
}
else if (content->rra)
{
single_reverse_rotate(stack_a, "rra\n");
content->rra--;
}
else
return ;
run_op(stack_a, stack_b, content, c);
}
void to_push(t_list **stack_a, t_list **stack_b, int index, char c)
{
t_list *tmp;
if (c == 'a')
tmp = *stack_b;
else if (c == 'b')
tmp = *stack_a;
while (index--)
tmp = tmp->next;
run_op(stack_a, stack_b, tmp->content, c);
run_op2(stack_a, stack_b, tmp->content, c);
}
void init_op(t_op *op)
{
op->to_push = 0;
op->biggest = 0;
op->smallest = 0;
op->big_index = 0;
op->small_index = 0;
op->i = 0;
op->cost = 0;
op->cheapest = 0;
}
void optimised_push_b(t_list **stack_a, t_list **stack_b)
{
t_op op;
while (ft_lstsize(*stack_a) != 3)
{
init_op(&op);
calculate_cost_b(*stack_a, *stack_b, &op);
to_push(stack_a, stack_b, op.to_push, 'b');
}
}