-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_calc.c
120 lines (110 loc) · 3.09 KB
/
move_calc.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move_calc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brulutaj <brulutaj@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/26 15:08:34 by brulutaj #+# #+# */
/* Updated: 2024/04/30 17:49:16 by brulutaj ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_bestmoves check_moves_b(t_dll *stack_b, int value_b, t_bestmoves moves)
{
int index;
int size;
index = 0;
size = dll_size(stack_b);
while (stack_b)
{
if (stack_b->value == value_b)
{
index = stack_b->index;
break ;
}
stack_b = stack_b->next;
}
if (index > (size - 1) / 2)
moves.r_rot_b = size - index;
else
moves.rot_b = index;
return (moves);
}
t_bestmoves check_moves_a(t_dll *stack_a, int value_b, t_bestmoves moves)
{
int index;
int size;
int diff;
index = 0;
diff = INT_MAX;
size = dll_size(stack_a);
if (find_min_value(stack_a) > value_b)
index = find_min_index(stack_a);
if (find_max_value(stack_a) < value_b)
index = find_max_value(stack_a);
while (stack_a)
{
if (stack_a->value > value_b && diff > stack_a->value - value_b)
{
diff = stack_a->value - value_b;
index = stack_a->index;
}
stack_a = stack_a->next;
}
if (index > (size - 1) / 2)
moves.r_rot_a = size - index;
else
moves.rot_a = index;
return (moves);
}
t_bestmoves organize_moves(t_bestmoves moves)
{
if (moves.rot_a >= moves.rot_b)
{
moves.rot_both = moves.rot_b;
moves = bestmoves_operations(moves, ROTATE);
}
else if (moves.rot_a < moves.rot_b)
{
moves.rot_both = moves.rot_a;
moves = bestmoves_operations(moves, ROTATE);
}
if (moves.r_rot_a >= moves.r_rot_b)
{
moves.r_rot_both = moves.r_rot_b;
moves = bestmoves_operations(moves, R_ROTATE);
}
else if (moves.r_rot_a < moves.r_rot_b)
{
moves.r_rot_both = moves.r_rot_a;
moves = bestmoves_operations(moves, R_ROTATE);
}
moves = bestmoves_operations(moves, STAY);
return (moves);
}
t_bestmoves get_moves(t_dll *stack_a, t_dll *stack_b, int value_b)
{
t_bestmoves moves;
moves = (t_bestmoves){0};
moves = check_moves_b(stack_b, value_b, moves);
moves = check_moves_a(stack_a, value_b, moves);
moves = organize_moves(moves);
return (moves);
}
t_bestmoves get_best_moves(t_dll *stack_a, t_dll *stack_b)
{
t_bestmoves best;
t_bestmoves next;
t_dll *tmp_b;
tmp_b = stack_b;
best = get_moves(stack_a, stack_b, stack_b->value);
while (tmp_b->next)
{
tmp_b = tmp_b->next;
next = get_moves(stack_a, stack_b, tmp_b->value);
if (best.tot_moves > next.tot_moves)
best = bestmoves_cpy(next);
}
return (best);
}