-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo_excute.c
83 lines (75 loc) · 2.08 KB
/
algo_excute.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* algo_excute.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebennamr <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/07 14:43:43 by ebennamr #+# #+# */
/* Updated: 2023/02/13 16:03:26 by ebennamr ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
static void repeat_double(t_data *data, int num, int type)
{
while (num > 0)
{
if (type == TYPE_RRX)
rrr(data, RRR);
else
rr(data, RR);
num--;
}
}
static void repeat_a(t_int2 *data, int type, int num)
{
while (num > 0)
{
if (type == TYPE_RRX)
rrx(data, RRA);
else
rx(data, RA);
num--;
}
}
static void repeat_b(t_int2 *data, int type, int num)
{
while (num > 0)
{
if (type == TYPE_RRX)
rrx(data, RRB);
else
rx(data, RB);
num--;
}
}
void excute_move(t_data *data, t_cmd t)
{
int min_val;
while (t.a_move > 0 || t.b_move > 0)
{
if (t.b_move && t.a_move && t.a_type == t.b_type)
{
repeat_double(data, min(t.a_move, t.b_move), t.a_type);
min_val = min(t.a_move, t.b_move);
t.a_move -= min_val;
t.b_move -= min_val;
}
else
{
repeat_a(data->stack_a, t.a_type, t.a_move);
repeat_b(data->stack_b, t.b_type, t.b_move);
break ;
}
}
px(data->stack_b, data->stack_a, data->maxisze, PA);
}
void rotate_to_sort(t_data *data)
{
int id;
id = indexofmax(data->stack_a->pt, data->stack_a->len);
if (id < data->stack_a->len / 2)
repeat_a(data->stack_a, TYPE_RX, id + 1);
else
repeat_a(data->stack_a, TYPE_RRX, data->stack_a->len - id - 1);
}