-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_index_b.c
More file actions
126 lines (115 loc) · 3.04 KB
/
utils_index_b.c
File metadata and controls
126 lines (115 loc) · 3.04 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_index_b.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: uyilmaz <uyilmaz@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/27 10:03:28 by uyilmaz #+# #+# */
/* Updated: 2023/03/06 20:19:52 by uyilmaz ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void find_where(int a, t_list *stack_b)
{
t_list *next;
t_list *actual;
actual = stack_b;
next = stack_b->next;
while (next)
{
t_data.index_b++;
if (a < *(actual->content) && a > *(next->content))
return ;
actual = next;
next = next->next;
}
}
void biggest_lowest(int a, t_list *stack_b)
{
t_list *ptr_b;
ptr_b = stack_b;
if (a > t_data.min_b && a < t_data.max_b)
return (find_where(a, stack_b));
if (a < t_data.min_b)
{
while (*(ptr_b->content) != t_data.max_b)
{
ptr_b = ptr_b->next;
t_data.index_b++;
}
}
else if (a > t_data.max_b)
{
while (*(ptr_b->content) != t_data.max_b)
{
ptr_b = ptr_b->next;
t_data.index_b++;
}
}
}
void is_it_first(int a, t_list *stack_b)
{
t_list *last;
last = t_data.stack_b;
while (last->next)
last = last->next;
if (a < *(last->content) && a > *(t_data.stack_b->content))
{
t_data.index_b = 0;
return ;
}
return (biggest_lowest(a, stack_b));
}
void decider(int target)
{
int current_cost_a;
int current_cost_b;
int current_all_cost;
if (t_data.index_a > t_data.size_a - t_data.index_a)
current_cost_a = -1 * (t_data.size_a - t_data.index_a);
else
current_cost_a = t_data.index_a;
if (t_data.index_b > t_data.size_b - t_data.index_b)
current_cost_b = -1 * (t_data.size_b - t_data.index_b);
else
current_cost_b = t_data.index_b;
if ((current_cost_a > 0 && current_cost_b > 0)
|| (current_cost_a < 0 && current_cost_b < 0))
current_all_cost = ft_abs_larger(current_cost_a, current_cost_b);
else
current_all_cost = ft_abs(current_cost_a) + ft_abs(current_cost_b);
if (current_all_cost < t_data.all_cost)
{
t_data.all_cost = current_all_cost;
t_data.target = target;
t_data.cost_a = current_cost_a;
t_data.cost_b = current_cost_b;
}
}
void get_biggest_top(int i)
{
t_list *biggest;
biggest = t_data.stack_b;
while (*(biggest->content) != t_data.max_b)
{
biggest = biggest->next;
i++;
}
if (i < t_data.size_b - i)
{
while (*(t_data.stack_b->content) != t_data.max_b)
{
rb(&t_data.stack_b);
ft_putstr("rb\n");
}
}
else
{
while (*(t_data.stack_b->content) != t_data.max_b)
{
rrb(&t_data.stack_b);
ft_putstr("rrb\n");
}
}
}