Push Swap

This commit is contained in:
Angel Ortigosa Perez
2025-09-08 14:02:27 +02:00
commit 31f2054f1c
16 changed files with 1036 additions and 0 deletions

78
moves.c Normal file
View File

@@ -0,0 +1,78 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}