-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_b.c
81 lines (72 loc) · 2.17 KB
/
calc_b.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* calc_b.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: joandre- <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/31 03:41:09 by joandre- #+# #+# */
/* Updated: 2024/04/02 03:29:42 by joandre- ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
unsigned int calc_b2a_reverse(t_stack *a, t_stack *b, int n)
{
unsigned int c;
c = 0;
if (index_numb(b, n))
c = stack_size(b) - index_numb(b, n);
if (target_a(a, n))
c += stack_size(a) - target_a(a, n);
return (c);
}
unsigned int calc_b2a_rotate(t_stack *a, t_stack *b, int n)
{
if (index_numb(b, n) >= target_a(a, n))
return (index_numb(b, n));
else
return (target_a(a, n));
}
unsigned int calc_b2a_rrotate(t_stack *a, t_stack *b, int n)
{
unsigned int c;
c = 0;
if (index_numb(b, n))
c = stack_size(b) - index_numb(b, n);
return (c + target_a(a, n));
}
unsigned int calc_b2a_rreverse(t_stack *a, t_stack *b, int n)
{
unsigned int c;
c = index_numb(b, n);
if (target_a(a, n))
c += stack_size(a) - target_a(a, n);
return (c);
}
int cheap_b(t_stack *a, t_stack *b)
{
int n;
unsigned int p;
unsigned int c;
t_stack *t;
n = b->numb;
p = calc_b2a_reverse(a, b, b->numb);
t = b;
while (t)
{
c = calc_b2a_reverse(a, b, t->numb);
if (c > calc_b2a_rotate(a, b, t->numb))
c = calc_b2a_rotate(a, b, t->numb);
if (c > calc_b2a_rrotate(a, b, t->numb))
c = calc_b2a_rrotate(a, b, t->numb);
if (c > calc_b2a_rreverse(a, b, t->numb))
c = calc_b2a_rreverse(a, b, t->numb);
if (p > c)
{
p = c;
n = t->numb;
}
t = t->next;
}
return (n);
}