Push_swap/srcs/calc_cheap.c

56 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* calc_cheap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/03 11:32:29 by lfirmin #+# #+# */
/* Updated: 2024/11/03 18:05:31 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "../include/pushswap.h"
int calc_move(t_stack *stack_a, t_stack *stack_b, int index_b)
{
int index_a;
int value_b;
value_b = return_value(stack_b, index_b);
index_a = calc_index_a(stack_a, value_b);
return (calc_move_stack(stack_b, index_b) \
+ calc_move_stack(stack_a, index_a));
}
int calc_cheaper(t_stack *stack_a, t_stack *stack_b)
{
int index;
int cheaper;
int cheaper_index;
t_node *curr;
curr = stack_b->top;
index = 1;
cheaper_index = 1;
cheaper = calc_move(stack_a, stack_b, index);
index++;
while (curr->next)
{
if (cheaper > calc_move(stack_a, stack_b, index))
{
cheaper = calc_move(stack_a, stack_b, index);
cheaper_index = index;
}
index++;
curr = curr->next;
}
return (cheaper_index);
}
int calc_move_stack(t_stack *stack, int index)
{
if (index <= stack->size / 2)
return (index - 1);
return (stack->size - index + 1);
}