mirror of
https://github.com/DavidGailleton/42-Push_Swap.git
synced 2026-01-27 08:41:58 +00:00
finish my shit on medium
This commit is contained in:
@@ -13,65 +13,6 @@
|
||||
#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;
|
||||
t_stack *start;
|
||||
int i;
|
||||
int first_pass;
|
||||
|
||||
tmp = assign_stack(stacks, c);
|
||||
start = assign_stack(stacks, c);
|
||||
tmp = tmp->previous;
|
||||
i = 0;
|
||||
first_pass = 1;
|
||||
while (first_pass || tmp != start->previous)
|
||||
{
|
||||
first_pass = 0;
|
||||
if (in_range(tmp->value, max_range, range))
|
||||
tmp = start;
|
||||
tmp = tmp->previous;
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
static int path_to_start(t_stacks *stacks, int max_range, int range, char c)
|
||||
{
|
||||
t_stack *tmp;
|
||||
t_stack *start;
|
||||
int i;
|
||||
int first_pass;
|
||||
|
||||
tmp = assign_stack(stacks, c);
|
||||
start = assign_stack(stacks, c);
|
||||
i = 0;
|
||||
first_pass = 1;
|
||||
while (first_pass || tmp != start)
|
||||
{
|
||||
first_pass = 0;
|
||||
if (in_range(tmp->value, max_range, range))
|
||||
{
|
||||
tmp = start->previous;
|
||||
}
|
||||
tmp = tmp->next;
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
int wich_path(t_stacks *stacks, int max_range, int range, char c)
|
||||
{
|
||||
int path_start;
|
||||
int path_end;
|
||||
|
||||
path_start = path_to_start(stacks, max_range, range, c);
|
||||
path_end = path_to_end(stacks, max_range, range, c);
|
||||
if (path_start < path_end)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int stack_len(t_stack *stack)
|
||||
{
|
||||
@@ -95,7 +36,6 @@ 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);
|
||||
|
||||
94
algorithms/medium/move_patha.c
Normal file
94
algorithms/medium/move_patha.c
Normal file
@@ -0,0 +1,94 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* move_patha.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/16 06:20:47 by mteriier #+# #+# */
|
||||
/* Updated: 2026/01/16 06:20:55 by mteriier ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "medium_headers.h"
|
||||
#include "push_swap.h"
|
||||
#include <stdio.h>
|
||||
static int is_border_a(int value, t_stacks *stacks)
|
||||
{
|
||||
t_stack *tmp;
|
||||
|
||||
if (!stacks->a)
|
||||
return (1);
|
||||
tmp = assign_stack(stacks, 'a');
|
||||
if ((value < tmp->value && check_order(tmp))
|
||||
|| (value > tmp->value && value > tmp->previous->value
|
||||
&& check_order(stacks->a)))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int move_left_to_right(int value, t_stacks *stacks, int is_move)
|
||||
{
|
||||
int i;
|
||||
t_stack *tmp;
|
||||
|
||||
i = 0;
|
||||
tmp = assign_stack(stacks, 'a');
|
||||
while (value > tmp->value && !is_move)
|
||||
{
|
||||
tmp = tmp->next;
|
||||
i++;
|
||||
}
|
||||
while (value > stacks->a->value && is_move)
|
||||
ra(stacks);
|
||||
return (i);
|
||||
}
|
||||
|
||||
int move_right_to_left(int value, t_stacks *stacks, int is_move)
|
||||
{
|
||||
int i;
|
||||
t_stack *tmp;
|
||||
|
||||
tmp = assign_stack(stacks, 'a');
|
||||
i = 0;
|
||||
while (value < tmp->previous->value && !is_move)
|
||||
{
|
||||
tmp = tmp->previous;
|
||||
i++;
|
||||
}
|
||||
while (value < stacks->a->previous->value && is_move)
|
||||
rra(stacks);
|
||||
return (i);
|
||||
}
|
||||
|
||||
int wich_path_a(int value, t_stacks *stacks)
|
||||
{
|
||||
t_stack *tmp;
|
||||
int move_from_left;
|
||||
int move_from_right;
|
||||
|
||||
tmp = assign_stack(stacks, 'a');
|
||||
if (is_border_a(value, stacks))
|
||||
return (-1);
|
||||
move_from_left = move_left_to_right(value, stacks, 0);
|
||||
move_from_right = move_right_to_left(value, stacks, 0);
|
||||
if ((check_order(stacks->a) && move_from_left < move_from_right)
|
||||
|| (move_from_left > move_from_right && !check_order(stacks->a)))
|
||||
return (1);
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
|
||||
void move_ra_rra(int value, t_stacks *stacks)
|
||||
{
|
||||
if (is_border_a(value, stacks))
|
||||
return ;
|
||||
if (wich_path_a(value, stacks) == 1)
|
||||
{
|
||||
move_left_to_right(value, stacks, 1);
|
||||
}
|
||||
else if (wich_path_a(value, stacks) == 0)
|
||||
{
|
||||
move_right_to_left(value, stacks, 1);
|
||||
}
|
||||
}
|
||||
113
algorithms/medium/move_pathb.c
Normal file
113
algorithms/medium/move_pathb.c
Normal file
@@ -0,0 +1,113 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* move_pathb.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/16 06:20:38 by mteriier #+# #+# */
|
||||
/* Updated: 2026/01/16 06:20:42 by mteriier ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "medium_headers.h"
|
||||
#include "push_swap.h"
|
||||
|
||||
static int path_to_end(t_stacks *stacks, int max_range, int range, char c)
|
||||
{
|
||||
t_stack *tmp;
|
||||
t_stack *start;
|
||||
int i;
|
||||
int first_pass;
|
||||
|
||||
tmp = assign_stack(stacks, c);
|
||||
start = assign_stack(stacks, c);
|
||||
tmp = tmp->previous;
|
||||
i = 0;
|
||||
first_pass = 1;
|
||||
while (first_pass || tmp != start->previous)
|
||||
{
|
||||
first_pass = 0;
|
||||
if (in_range(tmp->value, max_range, range))
|
||||
tmp = start;
|
||||
tmp = tmp->previous;
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
static int path_to_start(t_stacks *stacks, int max_range, int range, char c)
|
||||
{
|
||||
t_stack *tmp;
|
||||
t_stack *start;
|
||||
int i;
|
||||
int first_pass;
|
||||
|
||||
tmp = assign_stack(stacks, c);
|
||||
start = assign_stack(stacks, c);
|
||||
i = 0;
|
||||
first_pass = 1;
|
||||
while (first_pass || tmp != start)
|
||||
{
|
||||
first_pass = 0;
|
||||
if (in_range(tmp->value, max_range, range))
|
||||
{
|
||||
tmp = start->previous;
|
||||
}
|
||||
tmp = tmp->next;
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
int wich_path(t_stacks *stacks, int max_range, int range, char c)
|
||||
{
|
||||
int path_start;
|
||||
int path_end;
|
||||
|
||||
path_start = path_to_start(stacks, max_range, range, c);
|
||||
path_end = path_to_end(stacks, max_range, range, c);
|
||||
if (path_start < path_end)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void normal_move_path(t_stacks *stacks, t_tab *one_preset, int range)
|
||||
{
|
||||
if (wich_path(stacks, one_preset->max_range, range, 'b'))
|
||||
{
|
||||
while (!in_range(stacks->b->value, one_preset->max_range, range))
|
||||
rb(stacks);
|
||||
}
|
||||
else
|
||||
{
|
||||
while (!in_range(stacks->b->value, one_preset->max_range, range))
|
||||
{
|
||||
rrb(stacks);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -13,76 +13,58 @@
|
||||
#include "push_swap.h"
|
||||
#include "medium_headers.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static int move_next(t_stack *tmp, int value)
|
||||
void move_rr_rrr(t_stacks *stacks, int move, char c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (tmp->previous->value < value)
|
||||
return (70000);
|
||||
while (tmp->value < value)
|
||||
while (i < move && c == 's')
|
||||
{
|
||||
tmp = tmp->next;
|
||||
rr(stacks);
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
while (i < move && c == 'e')
|
||||
{
|
||||
i++;
|
||||
rrr(stacks);
|
||||
}
|
||||
}
|
||||
|
||||
static int number_move_reverse(t_stacks *stacks, int value, char start_end)
|
||||
void put_in_order_ra_rra(t_stacks *stacks)
|
||||
{
|
||||
int i;
|
||||
t_stack *tmp;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
tmp = stacks->a;
|
||||
if (start_end == 's')
|
||||
while (!check_order(stacks->a) && i < 50)
|
||||
{
|
||||
i = move_next(tmp, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp = tmp->previous;
|
||||
while (tmp->value > value)
|
||||
{
|
||||
tmp = tmp->previous;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
int sort_path(t_stacks *stacks, int value)
|
||||
{
|
||||
int start_path;
|
||||
int end_path;
|
||||
|
||||
start_path = number_move_reverse(stacks, value, 's');
|
||||
if (start_path == 0)
|
||||
return (1);
|
||||
end_path = number_move_reverse(stacks, value, 'e');
|
||||
if (start_path < end_path)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void sort_little_pile(t_stacks *stacks)
|
||||
{
|
||||
if (!stacks->a)
|
||||
{
|
||||
pa(stacks);
|
||||
return ;
|
||||
}
|
||||
if (stacks->a->previous->value < stacks->b->value)
|
||||
{
|
||||
pa(stacks);
|
||||
ra(stacks);
|
||||
return ;
|
||||
i++;
|
||||
}
|
||||
if (sort_path(stacks, stacks->b->value))
|
||||
sort_from_left(stacks);
|
||||
else
|
||||
sort_from_right(stacks);
|
||||
}
|
||||
|
||||
void path_rr_rrr(int value, t_stacks *stacks, t_tab *one_preset, int range)
|
||||
{
|
||||
int move_a;
|
||||
int final_move;
|
||||
|
||||
final_move = get_pre_move_b(stacks, one_preset, range);
|
||||
if (wich_path_a(value, stacks) == 1
|
||||
&& wich_path(stacks, one_preset->max_range, range, 'b'))
|
||||
{
|
||||
move_a = move_left_to_right(value, stacks, 0);
|
||||
if (move_a < final_move)
|
||||
final_move = move_a;
|
||||
move_rr_rrr(stacks, final_move, 's');
|
||||
}
|
||||
else if (wich_path_a(value, stacks) == 0
|
||||
&& !wich_path(stacks, one_preset->max_range, range, 'b'))
|
||||
{
|
||||
move_a = move_right_to_left(value, stacks, 0);
|
||||
if (move_a < final_move)
|
||||
final_move = move_a;
|
||||
move_rr_rrr(stacks, final_move, 'e');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void push_range_to_b(t_stacks *stacks, t_tab *one_preset, int range)
|
||||
@@ -93,16 +75,15 @@ void push_range_to_b(t_stacks *stacks, t_tab *one_preset, int range)
|
||||
i = 0;
|
||||
while (one_preset->nb_in > 0)
|
||||
{
|
||||
if (i > 0)
|
||||
if (stacks->a)
|
||||
{
|
||||
value = get_value_finded(stacks, one_preset, range);
|
||||
opti_path(stacks, one_preset, range, value);
|
||||
path_rr_rrr(value, stacks, one_preset, range);
|
||||
}
|
||||
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);
|
||||
move_ra_rra(stacks->b->value, stacks);
|
||||
pa(stacks);
|
||||
put_in_order_ra_rra(stacks);
|
||||
one_preset->nb_in--;
|
||||
i++;
|
||||
if (!check_order(stacks->a))
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
#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;
|
||||
@@ -31,58 +31,6 @@ int get_value_finded(t_stacks *stacks, t_tab *one_preset, int range)
|
||||
return (value);
|
||||
}
|
||||
|
||||
void normal_move_path(t_stacks *stacks, t_tab *one_preset, int range)
|
||||
{
|
||||
if (wich_path(stacks, one_preset->max_range, range, 'b'))
|
||||
{
|
||||
while (!in_range(stacks->b->value, one_preset->max_range, range))
|
||||
rb(stacks);
|
||||
}
|
||||
else
|
||||
{
|
||||
while (!in_range(stacks->b->value, one_preset->max_range, range))
|
||||
{
|
||||
rrb(stacks);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sort_from_left(t_stacks *stacks)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (stacks->b->value > stacks->a->value)
|
||||
{
|
||||
ra(stacks);
|
||||
i++;
|
||||
}
|
||||
pa(stacks);
|
||||
while (i > 0)
|
||||
{
|
||||
rra(stacks);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
void sort_from_right(t_stacks *stacks)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (stacks->b->value < stacks->a->previous->value)
|
||||
{
|
||||
rra(stacks);
|
||||
i++;
|
||||
}
|
||||
pa(stacks);
|
||||
while (i >= 0)
|
||||
{
|
||||
ra(stacks);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
t_stack *assign_stack(t_stacks *stacks, char c)
|
||||
{
|
||||
t_stack *tmp;
|
||||
|
||||
Reference in New Issue
Block a user