-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove.c
More file actions
83 lines (76 loc) · 2 KB
/
move.c
File metadata and controls
83 lines (76 loc) · 2 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lsohler <lsohler@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/07 13:36:19 by lsohler #+# #+# */
/* Updated: 2023/04/13 15:25:54 by lsohler ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void do_rrr(t_list **pile_a, t_list **pile_b, int *cost_a, int *cost_b)
{
while (*cost_a < 0 && *cost_b < 0)
{
(*cost_a)++;
(*cost_b)++;
ft_rrr(pile_a, pile_b);
}
}
void do_rr(t_list **pile_a, t_list **pile_b, int *cost_a, int *cost_b)
{
while (*cost_a > 0 && *cost_b > 0)
{
(*cost_a)--;
(*cost_b)--;
ft_rr(pile_a, pile_b);
}
}
void rotate_a(t_list **pile_a, int *cost_a)
{
while (*cost_a)
{
if (*cost_a > 0)
{
ft_ra(pile_a);
(*cost_a)--;
}
else if (*cost_a < 0)
{
ft_rra(pile_a);
(*cost_a)++;
}
}
}
void rotate_b(t_list **pile_b, int *cost_b)
{
while (*cost_b)
{
if (*cost_b > 0)
{
ft_rb(pile_b);
(*cost_b)--;
}
else if (*cost_b < 0)
{
ft_rrb(pile_b);
(*cost_b)++;
}
}
}
void do_move(t_list **pile_a, t_list **pile_b, t_data *data)
{
int cost_a;
int cost_b;
cost_a = data->cost_a;
cost_b = data->cost_b;
if (cost_a < 0 && cost_b < 0)
do_rrr(pile_a, pile_b, &cost_a, &cost_b);
else if (cost_a > 0 && cost_b > 0)
do_rr(pile_a, pile_b, &cost_a, &cost_b);
rotate_a(pile_a, &cost_a);
rotate_b(pile_b, &cost_b);
ft_pa(pile_a, pile_b, data);
}