-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute_cheapest_move.c
More file actions
128 lines (120 loc) · 4.08 KB
/
execute_cheapest_move.c
File metadata and controls
128 lines (120 loc) · 4.08 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execute_cheapest_move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cborrome <cborrome@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/16 17:32:22 by cborrome #+# #+# */
/* Updated: 2025/02/17 11:04:06 by cborrome ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void case_1_above_half_size(t_data *data, t_move *move, int min_index);
void case_2_lower_half_size(t_data *data, t_move *move, int min_index);
void case_3_a_above_b_lower(t_data *data, t_move *move, int min_index);
void case_4_a_lower_b_above(t_data *data, t_move *move, int min_index);
void execute_cheapest_move(t_data *data, t_move *move)
{
int min_index;
int i;
min_index = 0;
i = 1;
while (i < data->size_b)
{
if (move[i].cost < move[min_index].cost)
min_index = i;
i++;
}
case_1_above_half_size(data, move, min_index);
case_2_lower_half_size(data, move, min_index);
case_3_a_above_b_lower(data, move, min_index);
case_4_a_lower_b_above(data, move, min_index);
push_to_a(data);
}
void case_1_above_half_size(t_data *data, t_move *move, int min_index)
{
if (move[min_index].target_index >= (data->size_a / 2) && \
move[min_index].index >= (data->size_b / 2))
{
if (move[min_index].moves_a > move[min_index].moves_b)
{
while (move[min_index].moves_b > 0)
reverse_rotate_both(data, move, min_index);
while (move[min_index].moves_a > 0)
reverse_rotate_a(data, move, min_index);
}
else if (move[min_index].moves_a < move[min_index].moves_b)
{
while (move[min_index].moves_a > 0)
reverse_rotate_both(data, move, min_index);
while (move[min_index].moves_b > 0)
reverse_rotate_b(data, move, min_index);
}
else if (move[min_index].moves_a == move[min_index].moves_b)
{
while (move[min_index].moves_a > 0 && move[min_index].moves_b > 0)
reverse_rotate_both(data, move, min_index);
}
}
}
void case_2_lower_half_size(t_data *data, t_move *move, int min_index)
{
if ((move[min_index].target_index < (data->size_a / 2) && \
move[min_index].index < (data->size_b / 2)))
{
if (move[min_index].moves_a > move[min_index].moves_b)
{
while (move[min_index].moves_b > 0)
rotate_both(data, move, min_index);
while (move[min_index].moves_a > 0)
rotate_a(data, move, min_index);
}
else if (move[min_index].moves_a < move[min_index].moves_b)
{
while (move[min_index].moves_a > 0)
rotate_both(data, move, min_index);
while (move[min_index].moves_b > 0)
rotate_b(data, move, min_index);
}
else if (move[min_index].moves_a == move[min_index].moves_b)
{
while (move[min_index].moves_a > 0 && move[min_index].moves_b > 0)
rotate_both(data, move, min_index);
}
}
}
void case_3_a_above_b_lower(t_data *data, t_move *move, int min_index)
{
if ((move[min_index].target_index >= (data->size_a / 2) && \
move[min_index].index < (data->size_b / 2)))
{
while (move[min_index].moves_a > 0)
{
reverse_rotate(data->stack_a, data->size_a, 'a');
move[min_index].moves_a--;
}
while (move[min_index].moves_b > 0)
{
rotate(data->stack_b, data->size_b, 'b');
move[min_index].moves_b--;
}
}
}
void case_4_a_lower_b_above(t_data *data, t_move *move, int min_index)
{
if ((move[min_index].target_index < (data->size_a / 2) && \
move[min_index].index >= (data->size_b / 2)))
{
while (move[min_index].moves_a > 0)
{
rotate(data->stack_a, data->size_a, 'a');
move[min_index].moves_a--;
}
while (move[min_index].moves_b > 0)
{
reverse_rotate(data->stack_b, data->size_b, 'b');
move[min_index].moves_b--;
}
}
}