-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_1.c
111 lines (101 loc) · 2.48 KB
/
utils_1.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
109
110
111
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brulutaj <brulutaj@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/25 15:45:54 by brulutaj #+# #+# */
/* Updated: 2024/05/02 17:41:38 by brulutaj ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int check_duplicates(char **mtx)
{
int i;
int j;
i = 0;
while (mtx && mtx[i])
{
j = i + 1;
while (mtx[j])
{
if (!ft_strncmp(mtx[i], mtx[j], 12))
return (0);
j++;
}
i++;
}
return (1);
}
int is_sorted(t_dll *pStack)
{
t_dll *tmp;
tmp = pStack;
if (!pStack)
return (0);
while (tmp->next)
{
if (tmp->value > tmp->next->value)
return (0);
tmp = tmp->next;
}
return (1);
}
int mtx_check(char **mtx)
{
int i;
int j;
i = 0;
while (mtx && mtx[i])
{
j = 0;
while (mtx[i][j])
{
if (mtx[i][j] == '-' || mtx[i][j] == '+')
{
if (!(mtx[i][j + 1] >= '0' && mtx[i][j + 1] <= '9'))
return (0);
if (j != 0 && mtx[i][j - 1] != ' ')
return (0);
}
else if (!(mtx[i][j] >= '0' && mtx[i][j] <= '9'))
return (0);
j++;
}
i++;
}
if (!check_duplicates(mtx))
return (0);
return (1);
}
t_bestmoves bestmoves_cpy(t_bestmoves moves)
{
t_bestmoves res;
res = (t_bestmoves){0};
res.rot_a = moves.rot_a;
res.rot_b = moves.rot_b;
res.rot_both = moves.rot_both;
res.r_rot_a = moves.r_rot_a;
res.r_rot_b = moves.r_rot_b;
res.r_rot_both = moves.r_rot_both;
res.tot_moves = moves.tot_moves;
return (res);
}
t_bestmoves bestmoves_operations(t_bestmoves moves, int flag_move)
{
if (flag_move == ROTATE)
{
moves.rot_a -= moves.rot_both;
moves.rot_b -= moves.rot_both;
}
if (flag_move == R_ROTATE)
{
moves.r_rot_a -= moves.r_rot_both;
moves.r_rot_b -= moves.r_rot_both;
}
if (flag_move == STAY)
moves.tot_moves = moves.rot_a + moves.rot_b + moves.rot_both \
+ moves.r_rot_a + moves.r_rot_b + moves.r_rot_both;
return (moves);
}