need to fix

This commit is contained in:
Maoake Teriierooiterai
2026-01-15 18:42:15 +01:00
parent 469320374d
commit afa8785379
10 changed files with 1633 additions and 11 deletions

View File

@@ -13,7 +13,7 @@
#include "push_swap.h"
#include "medium_headers.h"
#include <stdlib.h>
#include <stdio.h>
static int path_to_end(t_stacks *stacks, int max_range, int range, char c)
{
t_stack *tmp;
@@ -95,6 +95,7 @@ void bucket_algo(t_stacks *stacks, t_tab *preset, int range)
tmp = preset;
while(stacks->a)
pb(stacks);
//stacks->print = 0;
while (preset)
{
push_range_to_b(stacks, preset, range);
@@ -106,7 +107,5 @@ void bucket_algo(t_stacks *stacks, t_tab *preset, int range)
free(preset);
preset = tmp;
}
while (stacks->b)
pa(stacks);
return ;
}

View File

@@ -12,6 +12,22 @@
#include "push_swap.h"
#include "medium_headers.h"
#include <stdio.h>
static int move_next(t_stack *tmp, int value)
{
int i;
i = 0;
if (tmp->previous->value < value)
return (70000);
while (tmp->value < value)
{
tmp = tmp->next;
i++;
}
return (i);
}
static int number_move_reverse(t_stacks *stacks, int value, char start_end)
{
@@ -22,11 +38,7 @@ static int number_move_reverse(t_stacks *stacks, int value, char start_end)
tmp = stacks->a;
if (start_end == 's')
{
while (tmp->value < value)
{
tmp = tmp->next;
i++;
}
i = move_next(tmp, value);
}
else
{
@@ -40,7 +52,7 @@ static int number_move_reverse(t_stacks *stacks, int value, char start_end)
return (i);
}
static int sort_path(t_stacks *stacks, int value)
int sort_path(t_stacks *stacks, int value)
{
int start_path;
int end_path;
@@ -75,10 +87,25 @@ static void sort_little_pile(t_stacks *stacks)
void push_range_to_b(t_stacks *stacks, t_tab *one_preset, int range)
{
int value;
int i;
i = 0;
while (one_preset->nb_in > 0)
{
if (i > 0)
{
value = get_value_finded(stacks, one_preset, range);
opti_path(stacks, one_preset, range, value);
}
normal_move_path(stacks, one_preset, range);
opti2_move_path(stacks, one_preset, range);
sort_little_pile(stacks);
while (stacks->a->value > stacks->a->previous->value)
ra(stacks);
one_preset->nb_in--;
i++;
if (!check_order(stacks->a))
return ;
}
}

View File

@@ -0,0 +1,119 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_utils_tree.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/15 12:20:06 by mteriier #+# #+# */
/* Updated: 2026/01/15 12:20:07 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "medium_headers.h"
#include <stdio.h>
void opti2_move_path(t_stacks *stacks, t_tab *one_preset, int range)
{
int value;
int len;
value = stacks->b->value;
len = stack_a_len(stacks);
if (in_range(value, one_preset->max_range, range) && len > 0)
{
while (value < stacks->a->previous->value && !check_order(stacks->a))
rra(stacks);
}
}
static int get_pre_move_b(t_stacks *stacks, t_tab *one_preset, int range)
{
int i;
t_stack *tmp;
i = 0;
tmp = assign_stack(stacks, 'b');
if (wich_path(stacks, one_preset->max_range, range, 'b'))
{
while (!in_range(tmp->value, one_preset->max_range, range))
{
i++;
tmp = tmp->next;
}
}
else
while (!in_range(tmp->value, one_preset->max_range, range))
{
i++;
tmp = tmp->previous;
}
return (i);
}
static int get_pre_move_a(t_stacks *stacks, int val)
{
int i;
t_stack *tmp;
i = 0;
tmp = assign_stack(stacks, 'a');
if (sort_path(stacks, val))
{
while (val > tmp->value)
{
i++;
tmp = tmp->next;
}
}
else
{
tmp = tmp->previous;
while (val < tmp->value)
{
i++;
tmp = tmp->previous;
}
}
return (i);
}
static int get_min_move(t_stacks *stacks, t_tab *one_preset, int range, int val)
{
int b;
int a;
a = get_pre_move_a(stacks, val);
b = get_pre_move_b(stacks, one_preset, range);
if (a < b)
return (a);
else
return (b);
}
void opti_path(t_stacks *stacks, t_tab *one_preset, int range, int val)
{
int move;
int i;
i = 0;
if (sort_path(stacks, val)
&& wich_path(stacks, one_preset->max_range, range, 'b'))
{
move = get_min_move(stacks, one_preset, range, val);
while (i < move)
{
rr(stacks);
i++;
}
}
else if (!sort_path(stacks, val)
&& !wich_path(stacks, one_preset->max_range, range, 'b'))
{
move = get_min_move(stacks, one_preset, range, val);
while (i < move)
{
rrr(stacks);
i++;
}
}
}

View File

@@ -12,6 +12,24 @@
#include "push_swap.h"
#include "medium_headers.h"
#include <stdio.h>
int get_value_finded(t_stacks *stacks, t_tab *one_preset, int range)
{
int value;
t_stack *tmp;
tmp = assign_stack(stacks, 'b');
if (wich_path(stacks, one_preset->max_range, range, 'b'))
{
while (!in_range(tmp->value, one_preset->max_range, range))
tmp = tmp->next;
}
else
while (!in_range(tmp->value, one_preset->max_range, range))
tmp = tmp->previous;
value = tmp->value;
return (value);
}
void normal_move_path(t_stacks *stacks, t_tab *one_preset, int range)
{