-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoves.c
148 lines (139 loc) · 3.57 KB
/
moves.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* moves.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yorlians <yorlians@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/30 20:54:53 by yorlians #+# #+# */
/* Updated: 2023/05/31 08:10:25 by yorlians ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
/*
perform a specific number of double rotations on both stacks, helping to
rearrange the elements in a desired manner.
*/
void do_double_rot(t_stack **stack_a, t_stack **stack_b, int *ra, int *rb)
{
int rotations_a;
int rotations_b;
rotations_a = *ra;
rotations_b = *rb;
while (rotations_a > 0 && rotations_b > 0)
{
if (rotations_a && rotations_b)
{
rotate_both(stack_a, stack_b, 1);
rotations_a--;
rotations_b--;
}
}
while (rotations_a < 0 && rotations_b < 0)
{
if (rotations_a && rotations_b)
{
rev_rot_both(stack_a, stack_b, 1);
rotations_a++;
rotations_b++;
}
}
*ra = rotations_a;
*rb = rotations_b;
}
/*
perform single rotation on both stacks based on given number of rotations,
helping to rearrange the elements in a desired manner.
*/
void do_single_rot(t_stack **stack_a, t_stack **stack_b, int ra, int rb)
{
while (ra || rb)
{
if (ra > 0)
{
rotate_a(stack_a, 1);
ra--;
}
else if (ra < 0)
{
rev_rot_a(stack_a, 1);
ra++;
}
if (rb > 0)
{
rotate_b(stack_b, 1);
rb--;
}
else if (rb < 0)
{
rev_rot_b(stack_b, 1);
rb++;
}
}
}
/*
perform rotations on both stacks without printing any output. it
takes the number of rotations as input and executes the rotations
accordingly.
*/
void rot_no_print(t_stack **stack_a, t_stack **stack_b, int ra, int rb)
{
while (ra || rb)
{
if (ra > 0)
{
rotate_a(stack_a, 0);
ra--;
}
if (rb > 0)
{
rotate_b(stack_b, 0);
rb--;
}
}
}
/*
save the number of rotations for further analysis and
decision-making based on the total number of moves
required.
*/
int save_moves(t_ab *rotations, int new_ra, int new_rb)
{
int moves;
moves = 0;
rotations -> a = new_ra;
rotations -> b = new_rb;
if (new_ra < 0)
new_ra *= -1;
if (new_rb < 0)
new_rb *= -1;
moves = new_ra + new_rb;
return (moves);
}
/*
find optimal (min) moves to transfer elements from stack_b to
stack_a in order to achieve a desired sorting order.
*/
t_ab find_moves(t_stack *stack_a, t_stack *stack_b, t_ab size)
{
t_ab temp_rot;
t_ab rot;
int moves;
temp_rot.b = 0;
moves = -1;
while (stack_b)
{
temp_rot.a = find_rotations_a(stack_a, stack_b);
if (temp_rot.a + temp_rot.b < moves || moves == -1)
moves = save_moves(&rot, temp_rot.a, temp_rot.b);
if (size.a - temp_rot.a + temp_rot.b < moves)
moves = save_moves(&rot, temp_rot.a - size.a, temp_rot.b);
if (temp_rot.a + size.b - temp_rot.b < moves)
moves = save_moves(&rot, temp_rot.a, temp_rot.b - size.b);
if ((size.a - temp_rot.a) + (size.b - temp_rot.b) < moves)
moves = save_moves(&rot, temp_rot.a - size.a, temp_rot.b - size.b);
temp_rot.b++;
stack_b = stack_b -> next;
}
return (rot);
}