mirror of
https://github.com/Ladebeze66/pushswap.git
synced 2025-12-15 13:46:55 +01:00
80 lines
1.9 KiB
C
Executable File
80 lines
1.9 KiB
C
Executable File
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* do_move.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/07/31 20:00:13 by fgras-ca #+# #+# */
|
|
/* Updated: 2023/07/31 20:13:28 by fgras-ca ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "push_swap.h"
|
|
|
|
static void do_rev_rotate_both(t_stack **a, t_stack **b,
|
|
int *cost_a, int *cost_b)
|
|
{
|
|
while (*cost_a < 0 && *cost_b < 0)
|
|
{
|
|
(*cost_a)++;
|
|
(*cost_b)++;
|
|
do_rrr(a, b);
|
|
}
|
|
}
|
|
|
|
static void do_rotate_both(t_stack **a, t_stack **b, int *cost_a, int *cost_b)
|
|
{
|
|
while (*cost_a > 0 && *cost_b > 0)
|
|
{
|
|
(*cost_a)--;
|
|
(*cost_b)--;
|
|
do_rr(a, b);
|
|
}
|
|
}
|
|
|
|
static void do_rotate_a(t_stack **a, int *cost)
|
|
{
|
|
while (*cost)
|
|
{
|
|
if (*cost > 0)
|
|
{
|
|
do_ra(a);
|
|
(*cost)--;
|
|
}
|
|
else if (*cost < 0)
|
|
{
|
|
do_rra(a);
|
|
(*cost)++;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void do_rotate_b(t_stack **b, int *cost)
|
|
{
|
|
while (*cost)
|
|
{
|
|
if (*cost > 0)
|
|
{
|
|
do_rb(b);
|
|
(*cost)--;
|
|
}
|
|
else if (*cost < 0)
|
|
{
|
|
do_rrb(b);
|
|
(*cost)++;
|
|
}
|
|
}
|
|
}
|
|
|
|
void do_move(t_stack **a, t_stack **b, int cost_a, int cost_b)
|
|
{
|
|
if (cost_a < 0 && cost_b < 0)
|
|
do_rev_rotate_both(a, b, &cost_a, &cost_b);
|
|
else if (cost_a > 0 && cost_b > 0)
|
|
do_rotate_both(a, b, &cost_a, &cost_b);
|
|
do_rotate_a(a, &cost_a);
|
|
do_rotate_b(b, &cost_b);
|
|
do_pa(a, b);
|
|
}
|