This commit is contained in:
HexPloR
2024-06-24 00:05:59 +02:00
commit 4c83562ca9
81 changed files with 3228 additions and 0 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
+25
View File
@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* assign_param_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 19:51:13 by yantoine #+# #+# */
/* Updated: 2024/03/20 13:26:37 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void assign_param_list(t_Liste *liste)
{
if (!liste->premier)
return ;
list_lenght(liste);
list_index(liste);
find_min(liste);
find_max(liste);
find_last(liste);
find_moyenne(liste);
set_precedant(liste);
}
+91
View File
@@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* big_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 21:04:05 by yantoine #+# #+# */
/* Updated: 2024/03/21 18:11:09 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
/*
#include "../includes/push_swap.h"
static int wich_algo(t_Liste *stack_a, t_Liste *stack_b)
{
if (stack_a->premier->number > stack_b->max)
return (1);
else if (stack_a->premier->number < stack_b->min)
return (2);
else
return (3);
}
static void exec_algo(t_Liste *stack_a, t_Liste *stack_b, int algo)
{
int i;
i = 0;
if (algo == 1)
push(stack_a, stack_b, 'b');
else if (algo == 2)
{
push(stack_a, stack_b, 'b');
rotate(stack_b, 'b');
}
else
{
if (stack_a->premier->number >= stack_b->moyenne)
{
while (stack_a->premier->number < stack_b->premier->number)
{
rotate(stack_b, 'b');
i++;
}
while (stack_a->premier->number >= stack_b->premier->number \
&& stack_a->premier->number <= stack_b->dernier->number)
push(stack_a, stack_b, 'b');
while (i > 0)
{
if (stack_a->premier->number > stack_b->premier->number \
&& stack_a->premier->number < stack_b->dernier->number)
push(stack_a, stack_b, 'b');
reverse_rotate(stack_b, 'b');
i--;
}
}
else
{
while (stack_a->premier->number < stack_b->premier->number \
|| stack_a->premier->number > stack_b->dernier->number)
{
reverse_rotate(stack_b, 'b');
i++;
}
while (stack_a->premier->number >= stack_b->premier->number \
&& stack_a->premier->number <= stack_b->dernier->number)
push(stack_a, stack_b, 'b');
while (i >= 0)
{
if (stack_a->premier->number < stack_b->premier->number \
&& stack_a->premier->number > stack_b->dernier->number)
push(stack_a, stack_b, 'b');
rotate(stack_b, 'b');
i--;
}
}
}
}
void big_sort(t_Liste *stack_a, t_Liste *stack_b)
{
int size;
size = stack_a->length;
while (stack_a->length > 1)
exec_algo(stack_a, stack_b, wich_algo(stack_a, stack_b));
while (stack_a->length != size)
push(stack_a, stack_b, 'a');
}
*/
+36
View File
@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* condition.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/21 18:03:40 by yantoine #+# #+# */
/* Updated: 2024/03/21 23:43:04 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
int condition(t_Element *actuel_a, t_Element *actuel_b, t_Liste *stack_b)
{
if (actuel_a->number < stack_b->min)
{
if (actuel_b->number == stack_b->max)
{
return (1);
}
}
else if (actuel_a->number > stack_b->max)
{
if (actuel_b->number == stack_b->max)
{
return (1);
}
}
else if (actuel_a->number > actuel_b->number \
&& actuel_a->number < actuel_b->precedant_boucle->number)
{
return (1);
}
return (0);
}
+24
View File
@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* find_last.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/18 20:15:26 by yantoine #+# #+# */
/* Updated: 2024/03/18 20:16:57 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void find_last(t_Liste *liste)
{
t_Element *tmp;
if (!liste->premier)
return ;
tmp = liste->premier;
while (tmp->suivant)
tmp = tmp->suivant;
liste->dernier = tmp;
}
+51
View File
@@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* find_lower_rank.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/21 18:07:46 by yantoine #+# #+# */
/* Updated: 2024/03/21 23:43:16 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
static int lower_instruction(t_Element *element)
{
int lower;
lower = 0;
lower = element->instruction;
element->wich_instr = 0;
if (lower > element->instruction_ra)
{
lower = element->instruction_ra;
element->wich_instr = 1;
}
if (lower > element->instruction_rb)
{
lower = element->instruction_rb;
element->wich_instr = 2;
}
if (lower > element->instruction_rab)
{
lower = element->instruction_rab;
element->wich_instr = 3;
}
return (lower);
}
void find_lower_rank(t_Liste *stack)
{
t_Element *actuel;
actuel = stack->premier;
stack->lower_rank = actuel;
while (actuel)
{
if (lower_instruction(actuel) < lower_instruction(stack->lower_rank))
stack->lower_rank = actuel;
actuel = actuel->suivant;
}
}
+30
View File
@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* find_max.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/18 20:13:28 by yantoine #+# #+# */
/* Updated: 2024/03/18 20:14:44 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void find_max(t_Liste *liste)
{
t_Element *tmp;
int max;
if (!liste->premier)
return ;
tmp = liste->premier;
max = tmp->number;
while (tmp)
{
if (tmp->number > max)
max = tmp->number;
tmp = tmp->suivant;
}
liste->max = max;
}
+30
View File
@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* find_min.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 18:10:13 by yantoine #+# #+# */
/* Updated: 2024/03/13 20:18:53 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void find_min(t_Liste *liste)
{
t_Element *tmp;
int min;
if (!liste->premier)
return ;
tmp = liste->premier;
min = tmp->number;
while (tmp)
{
if (tmp->number < min)
min = tmp->number;
tmp = tmp->suivant;
}
liste->min = min;
}
+30
View File
@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* find_moyenne.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/18 01:14:19 by yantoine #+# #+# */
/* Updated: 2024/03/18 01:47:52 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void find_moyenne(t_Liste *liste)
{
t_Element *actuel;
int moyenne;
int nbr;
moyenne = 0;
nbr = 0;
actuel = liste->premier;
while (actuel)
{
moyenne += actuel->number;
actuel = actuel->suivant;
nbr++;
}
liste->moyenne = moyenne / nbr;
}
+134
View File
@@ -0,0 +1,134 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* find_quicker.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/19 12:31:16 by yantoine #+# #+# */
/* Updated: 2024/04/29 11:10:44 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
//faire pour moyenne haute et moyenne basse
static void boucle(t_Liste *stack_a, t_Liste *stack_b)
{
int i;
int j;
t_Element *actuel_a;
t_Element *actuel_b;
i = 0;
actuel_a = stack_a->premier;
while (actuel_a)
{
j = 0;
actuel_b = stack_b->premier;
while (actuel_b)
{
if (condition(actuel_a, actuel_b, stack_b))
{
actuel_a->instruction = j + i ;
actuel_a->ptr_num = actuel_b->number;
}
actuel_b = actuel_b->suivant;
j++;
}
actuel_a = actuel_a->suivant;
i++;
}
}
static void boucle_inverse_a(t_Liste *stack_a, t_Liste *stack_b)
{
int i;
int j;
t_Element *actuel_a;
t_Element *actuel_b;
i = 0;
actuel_a = stack_a->dernier;
while (actuel_a)
{
j = 0;
actuel_b = stack_b->premier;
while (actuel_b)
{
if (condition(actuel_a, actuel_b, stack_b))
{
actuel_a->instruction_ra = j + i;
actuel_a->ptr_num = actuel_b->number;
}
actuel_b = actuel_b->suivant;
j++;
}
actuel_a = actuel_a->precedant;
i++;
}
}
static void boucle_inverse_b(t_Liste *stack_a, t_Liste *stack_b)
{
int i;
int j;
t_Element *actuel_a;
t_Element *actuel_b;
i = 0;
actuel_a = stack_a->premier;
while (actuel_a)
{
j = 0;
actuel_b = stack_b->dernier;
while (actuel_b)
{
if (condition(actuel_a, actuel_b, stack_b))
{
actuel_a->instruction_rb = j + i;
actuel_a->ptr_num = actuel_b->number;
}
actuel_b = actuel_b->precedant;
j++;
}
actuel_a = actuel_a->suivant;
i++;
}
}
static void boucle_inverse_ab(t_Liste *stack_a, t_Liste *stack_b)
{
int i;
int j;
t_Element *actuel_a;
t_Element *actuel_b;
i = 0;
actuel_a = stack_a->dernier;
while (actuel_a)
{
j = 0;
actuel_b = stack_b->dernier;
while (actuel_b)
{
if (condition(actuel_a, actuel_b, stack_b))
{
actuel_a->instruction_rab = j + i;
actuel_a->ptr_num = actuel_b->number;
}
actuel_b = actuel_b->precedant;
j++;
}
actuel_a = actuel_a->precedant;
i++;
}
}
void find_quicker(t_Liste *stack_a, t_Liste *stack_b)
{
boucle(stack_a, stack_b);
boucle_inverse_a(stack_a, stack_b);
boucle_inverse_b(stack_a, stack_b);
boucle_inverse_ab(stack_a, stack_b);
find_lower_rank(stack_a);
}
+40
View File
@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* free_stack.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 18:26:53 by yantoine #+# #+# */
/* Updated: 2024/03/14 22:58:30 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void free_stack(t_Liste *stack_a, t_Liste *stack_b)
{
t_Element *tmp;
if (stack_a)
{
while (stack_a->premier)
{
tmp = stack_a->premier;
stack_a->premier = stack_a->premier->suivant;
free(tmp);
}
}
if (stack_b)
{
while (stack_b->premier)
{
tmp = stack_b->premier;
stack_b->premier = stack_b->premier->suivant;
free(tmp);
}
}
if (stack_a)
free(stack_a);
if (stack_b)
free(stack_b);
}
+28
View File
@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_list_find.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 00:28:07 by yantoine #+# #+# */
/* Updated: 2024/03/13 00:30:59 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
t_Element *ft_list_find(t_Liste *liste, int nb)
{
t_Element *element;
if (!liste)
return (NULL);
element = liste->premier;
while (element)
{
if (element->number == nb)
return (element);
element = element->suivant;
}
return (NULL);
}
+38
View File
@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_list_push_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 16:46:06 by yantoine #+# #+# */
/* Updated: 2024/03/21 22:49:32 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void ft_list_push_back(t_Liste *liste, int valeur)
{
t_Element *element;
t_Element *current;
element = malloc(sizeof(*element));
if (!element)
return ;
element->number = valeur;
element->ptr_num = 0;
element->instruction = 0;
element->instruction_ra = 0;
element->instruction_rb = 0;
element->instruction_rab = 0;
element->suivant = NULL;
if (liste->premier == NULL)
liste->premier = element;
else
{
current = liste->premier;
while (current->suivant)
current = current->suivant;
current->suivant = element;
}
}
+30
View File
@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* is_sorted.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/12 21:19:44 by yantoine #+# #+# */
/* Updated: 2024/04/26 16:42:18 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
int is_sorted(t_Liste *stack_a)
{
t_Element *current;
if (!stack_a)
return (0);
if (stack_a->length == 0)
return (1);
current = stack_a->premier;
while (current->suivant)
{
if (current->number > current->suivant->number)
return (0);
current = current->suivant;
}
return (1);
}
+27
View File
@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_index.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 20:41:18 by yantoine #+# #+# */
/* Updated: 2024/03/13 20:41:42 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void list_index(t_Liste *liste)
{
t_Element *tmp;
int i;
i = 0;
tmp = liste->premier;
while (tmp)
{
tmp->index = i;
tmp = tmp->suivant;
i++;
}
}
+29
View File
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_lenght.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/12 23:57:34 by yantoine #+# #+# */
/* Updated: 2024/03/13 20:20:04 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void list_lenght(t_Liste *liste)
{
t_Element *current;
int i;
if (!liste || !liste->premier)
return ;
i = 0;
current = liste->premier;
while (current)
{
current = current->suivant;
i++;
}
liste->length = i;
}
+82
View File
@@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/12 14:15:12 by yantoine #+# #+# */
/* Updated: 2024/04/26 15:14:46 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
static int manage_arg(t_Liste *stack_a, int argc, char **argv)
{
if (argc == 2)
{
if (!parse_arg(stack_a, argv[1]))
return (0);
}
else
{
if (!parse_args(stack_a, argc, argv))
return (0);
}
list_lenght(stack_a);
return (1);
}
static t_Liste *init_stacks(t_Liste *stack_a)
{
stack_a = malloc(sizeof(t_Liste));
if (!stack_a)
{
write(2, "Error\n", 6);
return (NULL);
}
stack_a->premier = NULL;
stack_a->length = 0;
stack_a->moyenne = 0;
stack_a->lower_rank = NULL;
return (stack_a);
}
static void anti_25_ligne(t_Liste *stack_a, t_Liste *stack_b)
{
if (!is_sorted(stack_a))
{
if (stack_a->length == 2)
swap(stack_a, 'a');
else if (stack_a->length == 3)
sort_3(stack_a);
else if (stack_a->length == 4)
sort_4(stack_a, stack_b);
else if (stack_a->length == 5)
sort_5(stack_a, stack_b);
else
turc(stack_a, stack_b);
}
}
int main(int argc, char **argv)
{
t_Liste *stack_a;
t_Liste *stack_b;
stack_a = NULL;
stack_b = NULL;
stack_a = init_stacks(stack_a);
stack_b = init_stacks(stack_b);
if (argc < 2 || !manage_arg(stack_a, argc, argv))
{
free_stack(stack_a, stack_b);
if (argc != 1)
write(2, "Error\n", 6);
return (0);
}
assign_param_list(stack_a);
anti_25_ligne(stack_a, stack_b);
free_stack(stack_a, stack_b);
return (0);
}
+56
View File
@@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* normal_trie.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/21 19:47:09 by yantoine #+# #+# */
/* Updated: 2024/04/26 16:47:10 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void normal_trie(t_Liste *stack_a, t_Liste *stack_b)
{
if (!stack_a || !stack_b)
return ;
while (stack_a->premier != stack_a->lower_rank \
&& stack_b->premier->number != stack_a->lower_rank->ptr_num)
rotate_both(stack_a, stack_b);
while (stack_a->premier != stack_a->lower_rank)
rotate(stack_a, 'a');
while (stack_b->premier->number != stack_a->lower_rank->ptr_num)
rotate(stack_b, 'b');
push(stack_a, stack_b, 'b');
}
void reverse_a_trie(t_Liste *stack_a, t_Liste *stack_b)
{
while (stack_a->premier != stack_a->lower_rank)
reverse_rotate(stack_a, 'a');
while (stack_b->premier->number != stack_a->lower_rank->ptr_num)
rotate(stack_b, 'b');
push(stack_a, stack_b, 'b');
}
void reverse_b_trie(t_Liste *stack_a, t_Liste *stack_b)
{
while (stack_a->premier != stack_a->lower_rank)
rotate(stack_a, 'a');
while (stack_b->premier->number != stack_a->lower_rank->ptr_num)
reverse_rotate(stack_b, 'b');
push(stack_a, stack_b, 'b');
}
void reverse_ab_trie(t_Liste *stack_a, t_Liste *stack_b)
{
while (stack_a->premier != stack_a->lower_rank \
&& stack_b->premier->number != stack_a->lower_rank->ptr_num)
reverse_rotate_both(stack_a, stack_b);
while (stack_a->premier != stack_a->lower_rank)
reverse_rotate(stack_a, 'a');
while (stack_b->premier->number != stack_a->lower_rank->ptr_num)
reverse_rotate(stack_b, 'b');
push(stack_a, stack_b, 'b');
}
+82
View File
@@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_arg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/12 14:31:18 by yantoine #+# #+# */
/* Updated: 2024/04/29 11:11:48 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
static void ft_free_split(char **split)
{
int i;
i = 0;
if (!split)
return ;
while (split[i])
{
free(split[i]);
i++;
}
free(split);
}
static int verif_string(char *string)
{
int i;
i = 0;
while (string[i])
{
if (!ft_isdigit(string[i]) && \
string[i] != '-' && string[i] != '+')
return (0);
i++;
}
return (1);
}
static int ft_atoi_custom(char *string, t_Liste *stack)
{
long int nb;
if (!string || stack == NULL)
return (0);
nb = ft_atoi(string);
if (nb > INT_MAX || nb < INT_MIN)
return (0);
if (ft_list_find(stack, nb))
return (0);
ft_list_push_back(stack, nb);
return (1);
}
int parse_arg(t_Liste *stack_a, char *arg)
{
int i;
char **split;
i = 0;
split = ft_split(arg, ' ');
while (split[i])
{
if (!verif_string(split[i]))
{
ft_free_split(split);
return (0);
}
if (!ft_atoi_custom(split[i], stack_a))
{
ft_free_split(split);
return (0);
}
i++;
}
ft_free_split(split);
return (1);
}
+26
View File
@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/12 14:36:07 by yantoine #+# #+# */
/* Updated: 2024/03/14 22:09:01 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
int parse_args(t_Liste *stack_a, int argc, char **argv)
{
int i;
i = 1;
while (i < argc)
{
if (!parse_arg(stack_a, argv[i]))
return (0);
i++;
}
return (1);
}
+47
View File
@@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 18:01:44 by yantoine #+# #+# */
/* Updated: 2024/04/29 11:12:14 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
static void anti_25(t_Liste *stack_a, t_Liste *stack_b)
{
assign_param_list(stack_a);
assign_param_list(stack_b);
}
void push(t_Liste *stack_a, t_Liste *stack_b, char c)
{
t_Element *tmp;
if (c == 'a')
{
tmp = stack_b->premier;
if (tmp)
{
stack_b->premier = stack_b->premier->suivant;
tmp->suivant = stack_a->premier;
stack_a->premier = tmp;
}
write(1, "pa\n", 3);
}
else if (c == 'b')
{
tmp = stack_a->premier;
if (tmp)
{
stack_a->premier = stack_a->premier->suivant;
tmp->suivant = stack_b->premier;
stack_b->premier = tmp;
}
write(1, "pb\n", 3);
}
anti_25(stack_a, stack_b);
}
+20
View File
@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_two_number.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/19 12:28:15 by yantoine #+# #+# */
/* Updated: 2024/03/19 12:34:04 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void push_two_number(t_Liste *stack_a, t_Liste *stack_b)
{
push(stack_a, stack_b, 'b');
push(stack_a, stack_b, 'b');
if (is_sorted(stack_b))
swap(stack_b, 'b');
}
+40
View File
@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* reverse_rotate.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 17:43:31 by yantoine #+# #+# */
/* Updated: 2024/03/13 20:05:48 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void reverse_rotate(t_Liste *liste, char c)
{
t_Element *tmp;
t_Element *tmp2;
if (liste->length < 2)
return ;
tmp = liste->premier;
while (tmp->suivant->suivant)
tmp = tmp->suivant;
tmp2 = tmp->suivant;
tmp->suivant = NULL;
tmp2->suivant = liste->premier;
liste->premier = tmp2;
if (c == 'a')
write(1, "rra\n", 4);
else if (c == 'b')
write(1, "rrb\n", 4);
assign_param_list(liste);
}
void reverse_rotate_both(t_Liste *stack_a, t_Liste *stack_b)
{
reverse_rotate(stack_a, 0);
reverse_rotate(stack_b, 0);
write(1, "rrr\n", 4);
}
+37
View File
@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rotate.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 17:16:48 by yantoine #+# #+# */
/* Updated: 2024/03/13 20:06:58 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void rotate(t_Liste *liste, char c)
{
t_Element *tmp;
if (liste->length < 2)
return ;
tmp = liste->premier;
liste->premier = liste->premier->suivant;
liste->dernier->suivant = tmp;
liste->dernier = tmp;
tmp->suivant = NULL;
if (c == 'a')
write(1, "ra\n", 3);
else if (c == 'b')
write(1, "rb\n", 3);
assign_param_list(liste);
}
void rotate_both(t_Liste *stack_a, t_Liste *stack_b)
{
rotate(stack_a, 0);
rotate(stack_b, 0);
write(1, "rr\n", 3);
}
+33
View File
@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* set_precedant.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/20 13:22:29 by yantoine #+# #+# */
/* Updated: 2024/03/21 23:48:37 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void set_precedant(t_Liste *liste)
{
t_Element *actuel;
t_Element *precedant;
actuel = liste->premier;
while (actuel)
{
precedant = actuel;
actuel = actuel->suivant;
if (!actuel)
break ;
actuel->precedant = precedant;
actuel->precedant_boucle = precedant;
if (actuel->suivant == NULL)
break ;
}
liste->premier->precedant = NULL;
liste->premier->precedant_boucle = liste->dernier;
}
+59
View File
@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 00:49:31 by yantoine #+# #+# */
/* Updated: 2024/03/14 23:18:56 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
static void anti_25(t_Liste *stack_a, int min, int max)
{
if (stack_a->premier->suivant->number == max)
{
if (stack_a->premier->suivant->suivant->number == min)
reverse_rotate(stack_a, 'a');
else
{
reverse_rotate(stack_a, 'a');
swap(stack_a, 'a');
}
}
else
{
if (stack_a->premier->suivant->suivant->number == max)
swap(stack_a, 'a');
else
{
reverse_rotate(stack_a, 'a');
swap(stack_a, 'a');
}
}
}
void sort_3(t_Liste *stack_a)
{
int min;
int max;
if (is_sorted(stack_a))
return ;
min = stack_a->min;
max = stack_a->max;
if (stack_a->premier->number == max)
{
if (stack_a->premier->suivant->number == min)
rotate(stack_a, 'a');
else
{
swap(stack_a, 'a');
reverse_rotate(stack_a, 'a');
}
}
else
anti_25(stack_a, min, max);
}
+46
View File
@@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_4.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 20:34:09 by yantoine #+# #+# */
/* Updated: 2024/03/14 23:22:34 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void suite(t_Liste *stack_a, t_Liste *stack_b)
{
sort_3(stack_a);
push(stack_a, stack_b, 'a');
if (!is_sorted(stack_a))
rotate(stack_a, 'a');
}
void sort_4(t_Liste *stack_a, t_Liste *stack_b)
{
int min;
int max;
int i;
if (is_sorted(stack_a))
return ;
min = stack_a->min;
max = stack_a->max;
i = 0;
while (i < 4)
{
if (stack_a->premier->number == min \
|| stack_a->premier->number == max)
{
push(stack_a, stack_b, 'b');
break ;
}
else
rotate(stack_a, 'a');
i++;
}
suite(stack_a, stack_b);
}
+41
View File
@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_5.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 16:58:35 by yantoine #+# #+# */
/* Updated: 2024/03/14 22:57:44 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void sort_5(t_Liste *stack_a, t_Liste *stack_b)
{
int min;
int max;
int i;
if (is_sorted(stack_a))
return ;
min = stack_a->min;
max = stack_a->max;
i = 0;
while (i < 5)
{
if (stack_a->premier->number == min \
|| stack_a->premier->number == max)
{
push(stack_a, stack_b, 'b');
break ;
}
else
rotate(stack_a, 'a');
i++;
}
sort_4(stack_a, stack_b);
push(stack_a, stack_b, 'a');
if (!is_sorted(stack_a))
rotate(stack_a, 'a');
}
+36
View File
@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 17:34:45 by yantoine #+# #+# */
/* Updated: 2024/03/14 22:27:09 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
void swap(t_Liste *liste, char c)
{
t_Element *tmp;
if (liste->length < 2)
return ;
tmp = liste->premier;
liste->premier = liste->premier->suivant;
tmp->suivant = liste->premier->suivant;
liste->premier->suivant = tmp;
if (c == 'a')
write(1, "sa\n", 3);
else if (c == 'b')
write(1, "sb\n", 3);
assign_param_list(liste);
}
void swap_both(t_Liste *stack_a, t_Liste *stack_b)
{
swap(stack_a, 0);
swap(stack_b, 0);
write(1, "ss\n", 3);
}
+100
View File
@@ -0,0 +1,100 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* turc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yantoine <yantoine@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/19 12:14:47 by yantoine #+# #+# */
/* Updated: 2024/03/23 23:05:29 by yantoine ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
/*
void print_stacks(t_Liste *stack_a, t_Liste *stack_b)
{
t_Element *actuel_a;
t_Element *actuel_b;
actuel_a = stack_a->premier;
actuel_b = stack_b->premier;
printf("\n");
printf("\n");
while (actuel_a || actuel_b)
{
if (actuel_a)
{
printf("instruction %d__", actuel_a->instruction);
printf("%d__", actuel_a->instruction_ra);
printf("%d__", actuel_a->instruction_rb);
printf("%d__|", actuel_a->instruction_rab);
printf("%ld ", actuel_a->number);
actuel_a = actuel_a->suivant;
}
else
printf(" ");
if (actuel_b)
{
printf("%ld\n", actuel_b->number);
actuel_b = actuel_b->suivant;
}
else
printf("\n");
}
printf("\n");
}
*/
static void wich_algo(t_Liste *stack_a, t_Liste *stack_b)
{
if (stack_a->lower_rank->wich_instr == 0)
{
normal_trie(stack_a, stack_b);
}
else if (stack_a->lower_rank->wich_instr == 1)
{
reverse_a_trie(stack_a, stack_b);
}
else if (stack_a->lower_rank->wich_instr == 2)
{
reverse_b_trie(stack_a, stack_b);
}
else
{
reverse_ab_trie(stack_a, stack_b);
}
}
static void anti_25_ligne(t_Liste *stack_a, t_Liste *stack_b, long length)
{
while (stack_a->length > 1)
{
find_quicker(stack_a, stack_b);
wich_algo(stack_a, stack_b);
}
if (stack_b->premier->number >= stack_b->moyenne)
{
while (stack_b->premier->number != stack_b->max)
rotate(stack_b, 'b');
}
else
{
while (stack_b->premier->number != stack_b->max)
reverse_rotate(stack_b, 'b');
}
while (stack_a->length != length)
{
if (stack_a->dernier->number > stack_b->premier->number \
&& stack_a->dernier->number < stack_a->premier->number)
reverse_rotate(stack_a, 'a');
push(stack_a, stack_b, 'a');
}
}
void turc(t_Liste *stack_a, t_Liste *stack_b)
{
long length;
length = stack_a->length;
push_two_number(stack_a, stack_b);
anti_25_ligne(stack_a, stack_b, length);
}