-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulate0.c
76 lines (70 loc) · 2.22 KB
/
simulate0.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* simulate0.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: joleksia <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/19 11:49:36 by joleksia #+# #+# */
/* Updated: 2025/01/22 11:54:12 by joleksia ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int __ps_check_lowest(int *lowest, t_stack *s0, t_stack *s1, int *op);
static int __ps_oper_mod(t_oper *oper, int op0, int op1);
int ps_sim(t_stack *s0, t_stack *s1, t_oper *oper, char mod)
{
int lowest;
int op[2];
size_t iter;
op[0] = 0;
op[1] = 0;
lowest = INT_MAX;
iter = -1;
while (++iter < s0->cnt)
{
op[0] = iter;
if (mod == 'b')
op[1] = ps_dest_in_stack(s1, s0->dat[iter], mod);
else if (mod == 'a')
op[1] = ps_dest_in_stack(s1, s0->dat[iter], mod);
if (__ps_check_lowest(&lowest, s0, s1, op))
{
if (mod == 'b')
__ps_oper_mod(oper, op[0], op[1]);
else if (mod == 'a')
__ps_oper_mod(oper, op[1], op[0]);
}
}
return (1);
}
static int __ps_check_lowest(int *lowest, t_stack *s0, t_stack *s1, int *op)
{
if (*lowest > op[0] + op[1])
{
*lowest = op[0] + op[1];
return (1);
}
else if (*lowest > ((int) s0->cnt - op[0]) + op[1])
{
*lowest = ((int) s0->cnt - op[0]) + op[1];
return (1);
}
else if (*lowest > op[0] + ((int) s1->cnt - op[1]))
{
*lowest = op[0] + ((int) s1->cnt - op[1]);
return (1);
}
else if (*lowest > ((int) s0->cnt - op[0]) + ((int) s1->cnt - op[1]))
{
*lowest = ((int) s0->cnt - op[0]) + ((int) s1->cnt - op[1]);
return (1);
}
return (0);
}
static int __ps_oper_mod(t_oper *oper, int op0, int op1)
{
oper->ras = op0;
oper->rbs = op1;
return (1);
}