-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_rotates.c
More file actions
77 lines (72 loc) · 2.27 KB
/
check_rotates.c
File metadata and controls
77 lines (72 loc) · 2.27 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* check_rotates.c :+: :+: */
/* +:+ */
/* By: rcorke <rcorke@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2019/08/17 16:58:56 by rcorke #+# #+# */
/* Updated: 2019/08/19 12:24:53 by rcorke ######## odam.nl */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int find_smallest_from_3(int a, int b, int c)
{
if (a < b && a < c)
return (a);
else if (b < a && b < c)
return (b);
else
return (c);
}
static int find_biggest_from_3(int a, int b, int c)
{
if (a > b && a > c)
return (a);
else if (b > a && b > c)
return (b);
else
return (c);
}
int check_rotate(t_ps *ps, char which_stack)
{
if (which_stack == 'a')
{
if (ps->len_b == 2 && ps->b[0] < ps->b[1])
return (rotate_both(ps));
else if (ps->len_b == 3)
if (find_smallest_from_3(ps->b[0], ps->b[1], ps->b[2]) == ps->b[0])
return (rotate_both(ps));
return (rotate_a(ps));
}
else
{
if (ps->len_a == 2 && ps->a[0] > ps->a[1])
return (rotate_both(ps));
else if (ps->len_b == 3)
if (find_biggest_from_3(ps->a[0], ps->a[1], ps->a[2]) == ps->a[0])
return (rotate_both(ps));
return (rotate_b(ps));
}
}
int check_r_rotate(t_ps *ps, char which_stack)
{
if (which_stack == 'a')
{
if (ps->len_b == 2 && ps->b[0] < ps->b[1])
return (rotate_both(ps));
else if (ps->len_b == 3)
if (find_smallest_from_3(ps->b[0], ps->b[1], ps->b[2]) == ps->b[1])
return (rotate_both(ps));
return (reverse_a(ps));
}
else
{
if (ps->len_a == 2 && ps->a[0] > ps->a[1])
return (rotate_both(ps));
else if (ps->len_b == 3)
if (find_biggest_from_3(ps->a[0], ps->a[1], ps->a[2]) == ps->a[1])
return (rotate_both(ps));
return (reverse_b(ps));
}
}