-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctioncal.c
More file actions
131 lines (121 loc) · 2.6 KB
/
functioncal.c
File metadata and controls
131 lines (121 loc) · 2.6 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
129
130
131
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* functioncal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: musyilma <musyilma@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/09 15:20:18 by musyilma #+# #+# */
/* Updated: 2025/02/11 09:55:30 by musyilma ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include <unistd.h>
int functionindex(t_list *b, int number)
{
int i;
i = 1;
while (b->data != number)
{
i++;
b = b->next;
}
return (i);
}
static int functionmove(int i, int j)
{
if (i >= 0 && j >= 0)
{
if (i < j)
i = j;
}
else if (i < 0 && j >= 0)
{
i = i * -1;
i = i + j;
}
else if (i >= 0 && j < 0)
{
j = j * -1;
i = i + j;
}
else if (i < 0 && j < 0)
{
if (i > j)
i = j;
i = i * -1;
}
return (i);
}
static int funtionnumberup(t_general *gen, int numberb, int indexa)
{
int indexb;
int a;
int b;
int move;
a = 0;
b = 0;
indexb = functionindex(gen->b, numberb) - 1;
if (sl(gen->a) / 2 >= indexa)
a = (indexa);
else if (sl(gen->a) / 2 < indexa)
a = (indexa - sl(gen->a));
if (sl(gen->b) / 2 >= indexb)
b = (indexb);
else if (sl(gen->b) / 2 < indexb)
b = (indexb - sl(gen->b));
move = functionmove(a, b);
return (move);
}
int functionnumberfindb(int number, t_list *b, int min)
{
int smallnumber;
int bignumber;
smallnumber = -2147483648;
bignumber = -2147483648;
while (b != NULL)
{
if (number > b->data)
{
if (smallnumber < b->data)
{
smallnumber = b->data;
min++;
}
}
else
{
if (bignumber < b->data)
bignumber = b->data;
}
b = b->next;
}
if (min > 0)
return (smallnumber);
return (bignumber);
}
int functionindexpush(t_general *gen, int minnumber)
{
int numberb;
int index;
int move;
int minmove;
t_list *a;
minmove = 2147483647;
index = 1;
a = gen->a;
move = 0;
while (a != NULL)
{
numberb = functionnumberfindb(a->data, gen->b, 0);
move = funtionnumberup(gen, numberb, index - 1);
if (move < minmove)
{
minmove = move;
minnumber = a->data;
}
index++;
a = a->next;
}
return (minnumber);
}