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

58
cost.c Normal file
View File

@@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cost.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/14 06:25:35 by aortigos #+# #+# */
/* Updated: 2024/08/15 06:27:14 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void cost(t_stack **stack_a, t_stack **stack_b)
{
t_stack *tmp_a;
t_stack *tmp_b;
int size_a;
int size_b;
tmp_a = *stack_a;
tmp_b = *stack_b;
size_a = get_stack_size(tmp_a);
size_b = get_stack_size(tmp_b);
while (tmp_b)
{
tmp_b->cost_b = tmp_b->pos;
if (tmp_b->pos > size_b / 2)
tmp_b->cost_b = (size_b - tmp_b->pos) * -1;
tmp_b->cost_a = tmp_b->target;
if (tmp_b->target > size_a / 2)
tmp_b->cost_a = (size_a - tmp_b->target) * -1;
tmp_b = tmp_b->next;
}
}
void cheapest_move(t_stack **stack_a, t_stack **stack_b)
{
t_stack *tmp;
int cheapest;
int cost_a;
int cost_b;
tmp = *stack_b;
cheapest = INT_MAX;
while (tmp)
{
if (abs(tmp->cost_a) + abs(tmp->cost_b) < cheapest)
{
cheapest = abs(tmp->cost_a) + abs(tmp->cost_b);
cost_a = tmp->cost_a;
cost_b = tmp->cost_b;
}
tmp = tmp->next;
}
do_move(stack_a, stack_b, cost_a, cost_b);
}