-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.c
79 lines (73 loc) · 2.01 KB
/
move.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erigolon <erigolon@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 12:35:25 by erigolon #+# #+# */
/* Updated: 2023/06/12 15:16:33 by erigolon ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/push_swap.h"
static void do_rev_rotate_both(t_stack **a, t_stack **b,
int *cost_a, int *cost_b)
{
while (*cost_a < 0 && *cost_b < 0)
{
(*cost_a)++;
(*cost_b)++;
do_rrr(a, b);
}
}
static void do_rotate_both(t_stack **a, t_stack **b, int *cost_a, int *cost_b)
{
while (*cost_a > 0 && *cost_b > 0)
{
(*cost_a)--;
(*cost_b)--;
do_rr(a, b);
}
}
static void do_rotate_a(t_stack **stack_a, int *cost_a)
{
while (*cost_a)
{
if (*cost_a > 0)
{
do_ra(stack_a);
(*cost_a)--;
}
else if (*cost_a < 0)
{
do_rra(stack_a);
(*cost_a)++;
}
}
}
static void do_rotate_b(t_stack **stack_b, int *cost_b)
{
while (*cost_b)
{
if (*cost_b > 0)
{
do_rb(stack_b);
(*cost_b)--;
}
else if (*cost_b < 0)
{
do_rrb(stack_b);
(*cost_b)++;
}
}
}
void do_move(t_stack **stack_a, t_stack **stack_b, int cost_a, int cost_b)
{
if (cost_a < 0 && cost_b < 0)
do_rev_rotate_both(stack_a, stack_b, &cost_a, &cost_b);
else if (cost_a > 0 && cost_b > 0)
do_rotate_both(stack_a, stack_b, &cost_a, &cost_b);
do_rotate_a(stack_a, &cost_a);
do_rotate_b(stack_b, &cost_b);
do_pa(stack_a, stack_b);
}