79 lines
1.8 KiB
C
79 lines
1.8 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* moves.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/08/14 06:19:17 by aortigos #+# #+# */
|
|
/* Updated: 2024/08/14 06:24:42 by aortigos ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "push_swap.h"
|
|
|
|
static void reverse_both(t_stack **a, t_stack **b, int *cost_a, int *cost_b)
|
|
{
|
|
while (*cost_a < 0 && *cost_b < 0)
|
|
{
|
|
(*cost_a)++;
|
|
(*cost_b)++;
|
|
ft_rrr(a, b);
|
|
}
|
|
}
|
|
|
|
static void 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)--;
|
|
ft_rr(a, b);
|
|
}
|
|
}
|
|
|
|
static void rotate_a(t_stack **a, int *cost)
|
|
{
|
|
while (*cost)
|
|
{
|
|
if (*cost > 0)
|
|
{
|
|
ft_ra(a);
|
|
(*cost)--;
|
|
}
|
|
else if (*cost < 0)
|
|
{
|
|
ft_rra(a);
|
|
(*cost)++;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void rotate_b(t_stack **b, int *cost)
|
|
{
|
|
while (*cost)
|
|
{
|
|
if (*cost > 0)
|
|
{
|
|
ft_rb(b);
|
|
(*cost)--;
|
|
}
|
|
else if (*cost < 0)
|
|
{
|
|
ft_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)
|
|
reverse_both(a, b, &cost_a, &cost_b);
|
|
else if (cost_a > 0 && cost_b > 0)
|
|
rotate_both(a, b, &cost_a, &cost_b);
|
|
rotate_a(a, &cost_a);
|
|
rotate_b(b, &cost_b);
|
|
ft_pa(a, b);
|
|
}
|