-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore.c
108 lines (99 loc) · 2.66 KB
/
score.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* score.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bnidia <bnidia@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 08:42:42 by bnidia #+# #+# */
/* Updated: 2022/05/21 02:21:08 by bnidia ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include <limits.h>
void score_count(t_listc *stack_a, t_listc *stack_b, int *score)
{
t_listc *a;
t_listc *b;
int len_b;
int i;
b = stack_b;
a = stack_a;
i = 0;
*score = INT_MAX;
len_b = ft_lstc_size(b);
while (i < len_b)
{
reset_scores(b);
score_count_a(a, b);
b->score_rb = i;
b->score_rrb = len_b - i;
if (b->score_rrb >= b->score_rb)
b->score_rrb = 0;
else
b->score_rb = 0;
if (*score > b->score_rb + b->score_ra + b->score_rrb + b->score_rra)
*score = b->score_rb + b->score_ra + b->score_rrb + b->score_rra;
b = b->next;
i++;
}
}
void score_process(t_listc **stack_a, t_listc **stack_b, int score)
{
t_listc *b;
b = *stack_b;
while (b->score_rb + b->score_ra + b->score_rrb + b->score_rra != score)
b = b->next;
score_rr_rrr(b, stack_a, stack_b, true);
while (b->score_ra-- > 0)
ra(stack_a, true);
while (b->score_rb-- > 0)
rb(stack_b, true);
while (b->score_rra-- > 0)
rra(stack_a, true);
while (b->score_rrb-- > 0)
rrb(stack_b, true);
pa(stack_a, stack_b, true);
}
void score_rr_rrr(t_listc *b, t_listc **stack_a, t_listc **stack_b, bool p)
{
while (b->score_rb > 0 && b->score_ra > 0)
{
rr(stack_a, stack_b, p);
b->score_rb--;
b->score_ra--;
}
while (b->score_rrb > 0 && b->score_rra > 0)
{
rrr(stack_a, stack_b, p);
b->score_rrb--;
b->score_rra--;
}
}
void reset_scores(t_listc *b)
{
b->score_ra = 0;
b->score_rb = 0;
b->score_rra = 0;
b->score_rrb = 0;
}
void score_count_a(t_listc *a, t_listc *b)
{
int len_a;
int j;
j = 0;
len_a = ft_lstc_size(a);
while (j < len_a)
{
a = a->next;
b->score_ra++;
if (b->value > a->prev->value && b->value < a->value)
break ;
j++;
}
b->score_rra = len_a - b->score_ra;
if (b->score_rra >= b->score_ra)
b->score_rra = 0;
else
b->score_ra = 0;
}