-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_swap_magic.c
More file actions
62 lines (56 loc) · 1.87 KB
/
Copy pathpush_swap_magic.c
File metadata and controls
62 lines (56 loc) · 1.87 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap_magic.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: isel-azz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/11 07:46:07 by isel-azz #+# #+# */
/* Updated: 2024/05/11 07:46:11 by isel-azz ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void insert(t_stack_needs **a, t_stack_needs **b)
{
t_stack_needs *priority_node;
priority_node = get_priority(*b);
if (priority_node->up_down
&& priority_node->target->up_down)
rotate_stacks(a, b, priority_node);
else if (!(priority_node->up_down)
&& !(priority_node->target->up_down))
reverse_rotate_stacks(a, b, priority_node);
complete(b, priority_node, 'b');
complete(a, priority_node->target, 'a');
push_a(a, b);
}
void start_nodes(t_stack_needs *a, t_stack_needs *b)
{
set_positions(a);
set_positions(b);
set_targets(a, b);
set_push_cost(a, b);
set_priority(b);
}
void push_swap_magic(t_stack_needs **a, t_stack_needs **b)
{
t_stack_needs *smallest;
int a_len;
a_len = stack_len(*a);
while (a_len-- > 3)
push_b(a, b);
sort_three(a);
while (*b)
{
start_nodes(*a, *b);
insert(a, b);
}
set_positions(*a);
smallest = get_smallest(*a);
if (smallest->up_down)
while (*a != smallest)
rotate_a(a);
else
while (*a != smallest)
reverse_rotate_a(a);
}