norm && started mini mouv
This commit is contained in:
60
src/algo/algo.c
Normal file
60
src/algo/algo.c
Normal file
@ -0,0 +1,60 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* algo.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/21 17:29:26 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 16:06:26 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
void send_to_stack_b(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
size_t stack_size;
|
||||
int q_one;
|
||||
int median;
|
||||
|
||||
while (*stack_a)
|
||||
{
|
||||
median = find_median(stack_a);
|
||||
q_one = find_q_one(stack_a);
|
||||
stack_size = ft_stacksize(*stack_a);
|
||||
while (stack_size != 0)
|
||||
{
|
||||
if ((*stack_a)->nb <= median)
|
||||
{
|
||||
ft_push_b(stack_a, stack_b);
|
||||
if (*stack_b && (*stack_b)->nb <= q_one)
|
||||
ft_rotatestack_b(stack_b);
|
||||
}
|
||||
else
|
||||
ft_rotatestack_a(stack_a);
|
||||
stack_size--;
|
||||
}
|
||||
}
|
||||
if (*stack_a)
|
||||
ft_push_b(stack_a, stack_b);
|
||||
}
|
||||
|
||||
void ft_algo(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
if (ft_stacksize(*stack_a) == 2)
|
||||
{
|
||||
ft_swap_a(*stack_a);
|
||||
return ;
|
||||
}
|
||||
if (ft_stacksize(*stack_a) == 3)
|
||||
{
|
||||
}
|
||||
if (ft_stacksize(*stack_a) == 5)
|
||||
{
|
||||
}
|
||||
send_to_stack_b(stack_a, stack_b);
|
||||
while_insert(stack_a, stack_b);
|
||||
while (ft_is_sort(stack_a) == FALSE)
|
||||
ft_rotatestack_a(stack_a);
|
||||
}
|
61
src/algo/get_min_max.c
Normal file
61
src/algo/get_min_max.c
Normal file
@ -0,0 +1,61 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_min_max.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/11 15:23:26 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 15:59:26 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
size_t get_stack_max(t_stack **stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
size_t i;
|
||||
size_t i_max;
|
||||
int max;
|
||||
|
||||
tmp = *stack;
|
||||
max = tmp->nb;
|
||||
i_max = 0;
|
||||
i = 0;
|
||||
while (tmp)
|
||||
{
|
||||
if (tmp->nb > max)
|
||||
{
|
||||
max = tmp->nb;
|
||||
i_max = i;
|
||||
}
|
||||
tmp = tmp->next;
|
||||
i++;
|
||||
}
|
||||
return (i_max);
|
||||
}
|
||||
|
||||
size_t get_stack_min(t_stack **stack)
|
||||
{
|
||||
size_t i;
|
||||
size_t i_min;
|
||||
int nb_min;
|
||||
t_stack *tmp;
|
||||
|
||||
i = 0;
|
||||
i_min = 0;
|
||||
nb_min = INT_MAX;
|
||||
tmp = *stack;
|
||||
while (tmp)
|
||||
{
|
||||
if (tmp->nb < nb_min)
|
||||
{
|
||||
i_min = i;
|
||||
nb_min = tmp->nb;
|
||||
}
|
||||
tmp = tmp->next;
|
||||
i++;
|
||||
}
|
||||
return (i_min);
|
||||
}
|
117
src/algo/insertion.c
Normal file
117
src/algo/insertion.c
Normal file
@ -0,0 +1,117 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* insertion.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/23 14:49:14 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 16:03:14 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
void prepare_to_send_a(t_stack **stack_b, size_t lowest_cost_b)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (lowest_cost_b < (ft_stacksize(*stack_b) / 2))
|
||||
{
|
||||
while (stack_b && *stack_b && lowest_cost_b > 0)
|
||||
{
|
||||
ft_rotatestack_b(stack_b);
|
||||
lowest_cost_b--;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
i = 0;
|
||||
while (stack_b && *stack_b && lowest_cost_b < ft_stacksize(*stack_b))
|
||||
{
|
||||
ft_reverserotate_b(stack_b);
|
||||
lowest_cost_b++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t get_index(t_stack **stack, int nb)
|
||||
{
|
||||
t_stack *tmp;
|
||||
size_t i;
|
||||
|
||||
i = 1;
|
||||
tmp = (*stack);
|
||||
while (tmp && tmp->next)
|
||||
{
|
||||
if (tmp->nb < nb && tmp->next->nb > nb)
|
||||
return (i);
|
||||
tmp = tmp->next;
|
||||
i++;
|
||||
}
|
||||
if (tmp && nb > tmp->nb && nb < (*stack)->nb)
|
||||
return (0);
|
||||
return (get_stack_min(stack));
|
||||
}
|
||||
|
||||
size_t get_lowest_cost(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
t_stack *tmp;
|
||||
size_t i_lowest_cost;
|
||||
size_t lowest_cost_b;
|
||||
size_t actual_cost_b;
|
||||
|
||||
i_lowest_cost = get_index(stack_a, (*stack_b)->nb);
|
||||
tmp = *stack_b;
|
||||
actual_cost_b = 0;
|
||||
lowest_cost_b = 0;
|
||||
while (tmp)
|
||||
{
|
||||
if ((get_index(stack_a, tmp->nb) < i_lowest_cost))
|
||||
{
|
||||
i_lowest_cost = get_index(stack_a, tmp->nb);
|
||||
lowest_cost_b = actual_cost_b;
|
||||
}
|
||||
actual_cost_b++;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
prepare_to_send_a(stack_b, lowest_cost_b);
|
||||
return (i_lowest_cost);
|
||||
}
|
||||
|
||||
void insert_nb(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
size_t i;
|
||||
size_t new_i_elem;
|
||||
size_t stack_size;
|
||||
|
||||
i = 0;
|
||||
new_i_elem = get_lowest_cost(stack_a, stack_b);
|
||||
stack_size = ft_stacksize(*stack_a);
|
||||
if (new_i_elem < (stack_size / 2))
|
||||
{
|
||||
while (i < new_i_elem)
|
||||
{
|
||||
ft_rotatestack_a(stack_a);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
i = stack_size;
|
||||
while (i > new_i_elem)
|
||||
{
|
||||
ft_reverserotate_a(stack_a);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
ft_push_a(stack_a, stack_b);
|
||||
}
|
||||
|
||||
void while_insert(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
while (*stack_b)
|
||||
{
|
||||
insert_nb(stack_a, stack_b);
|
||||
}
|
||||
}
|
77
src/algo/median.c
Normal file
77
src/algo/median.c
Normal file
@ -0,0 +1,77 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* median.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/22 11:27:04 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 16:05:40 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
t_boolean is_median(t_stack **stack, int nb)
|
||||
{
|
||||
t_stack *tmp;
|
||||
int count;
|
||||
|
||||
tmp = *stack;
|
||||
count = 0;
|
||||
while (tmp)
|
||||
{
|
||||
count += (tmp->nb < nb) - (tmp->nb > nb);
|
||||
tmp = tmp->next;
|
||||
}
|
||||
if (count == -1 || count == 0 || count == 1)
|
||||
return (TRUE);
|
||||
return (FALSE);
|
||||
}
|
||||
|
||||
int find_median(t_stack **stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
|
||||
tmp = *stack;
|
||||
while (tmp)
|
||||
{
|
||||
if (is_median(stack, tmp->nb) == TRUE)
|
||||
return (tmp->nb);
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return (ERROR);
|
||||
}
|
||||
|
||||
t_boolean is_q_one(t_stack **stack, int nb)
|
||||
{
|
||||
t_stack *tmp;
|
||||
int tmp_count;
|
||||
int count;
|
||||
|
||||
tmp = *stack;
|
||||
count = 0;
|
||||
while (tmp)
|
||||
{
|
||||
tmp_count = (tmp->nb < nb) + (tmp->nb < nb) + (tmp->nb < nb);
|
||||
count += tmp_count - (tmp->nb > nb);
|
||||
tmp = tmp->next;
|
||||
}
|
||||
if (count == -1 || count == 0 || count == 1)
|
||||
return (TRUE);
|
||||
return (FALSE);
|
||||
}
|
||||
|
||||
int find_q_one(t_stack **stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
|
||||
tmp = *stack;
|
||||
while (tmp)
|
||||
{
|
||||
if (is_q_one(stack, tmp->nb) == TRUE)
|
||||
return (tmp->nb);
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return (ERROR);
|
||||
}
|
21
src/algo/small_mouv.c
Normal file
21
src/algo/small_mouv.c
Normal file
@ -0,0 +1,21 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* small_mouv.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/11 16:12:47 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 16:21:54 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
void sort_three(t_stack **stack_a)
|
||||
{
|
||||
if ((*stack_a)->nb > (*stack_a)->next->nb)
|
||||
ft_swap_a(*stack_a);
|
||||
if (get_stack_max(stack_a) == 2)
|
||||
ft_reverserotate_a(stack_a);
|
||||
}
|
60
src/check_error.c
Normal file
60
src/check_error.c
Normal file
@ -0,0 +1,60 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* check_error.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/15 13:45:25 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 15:56:15 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
void ft_check_args_format(char **av)
|
||||
{
|
||||
char **tmp;
|
||||
char *tmp_av;
|
||||
|
||||
tmp = av;
|
||||
tmp++;
|
||||
while (*tmp)
|
||||
{
|
||||
tmp_av = *tmp;
|
||||
while (*tmp_av)
|
||||
{
|
||||
if (!ft_isdigit(*tmp_av) && *tmp_av != 32 \
|
||||
&& *tmp_av != '-' && *tmp_av != '+')
|
||||
{
|
||||
ft_putendl_fd("Error", STDERR_FILENO);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
tmp_av++;
|
||||
}
|
||||
tmp++;
|
||||
}
|
||||
}
|
||||
|
||||
void ft_check_double(t_stack **stack)
|
||||
{
|
||||
t_stack *index;
|
||||
t_stack *tmp;
|
||||
|
||||
index = *stack;
|
||||
while (index)
|
||||
{
|
||||
tmp = index->next;
|
||||
while (tmp)
|
||||
{
|
||||
if (index->nb == tmp->nb)
|
||||
{
|
||||
ft_stackclear(stack);
|
||||
ft_putendl_fd("Error", STDERR_FILENO);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
tmp = tmp->next;
|
||||
}
|
||||
index = index->next;
|
||||
}
|
||||
}
|
75
src/main.c
Normal file
75
src/main.c
Normal file
@ -0,0 +1,75 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 12:14:22 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/08 14:17:09 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
void ft_check_args(char **av)
|
||||
{
|
||||
char **tmp;
|
||||
char *tmp_av;
|
||||
|
||||
tmp = av;
|
||||
tmp++;
|
||||
while (*tmp)
|
||||
{
|
||||
tmp_av = *tmp;
|
||||
while (*tmp_av)
|
||||
{
|
||||
if ((*tmp_av == '+' || *tmp_av == '-'))
|
||||
{
|
||||
if (!ft_isdigit(*tmp_av + 1))
|
||||
{
|
||||
ft_putendl_fd("Error", STDERR_FILENO);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
else
|
||||
{
|
||||
while (!*tmp_av && ft_isdigit(*tmp_av))
|
||||
tmp_av++;
|
||||
}
|
||||
}
|
||||
tmp_av++;
|
||||
}
|
||||
tmp++;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_stack *stack_a;
|
||||
t_stack *stack_b;
|
||||
|
||||
if (ac < 2)
|
||||
{
|
||||
ft_putendl_fd("Error", STDERR_FILENO);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
ft_check_args_format(av);
|
||||
ft_check_args(av);
|
||||
stack_b = NULL;
|
||||
stack_a = ft_parsing(av);
|
||||
if (ft_stacksize(stack_a) <= 1)
|
||||
{
|
||||
ft_stackclear(&stack_a);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
if (ft_is_sort(&stack_a) == TRUE)
|
||||
{
|
||||
ft_stackclear(&stack_a);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
ft_check_double(&stack_a);
|
||||
ft_algo(&stack_a, &stack_b);
|
||||
ft_stackclear(&stack_a);
|
||||
ft_stackclear(&stack_b);
|
||||
return (0);
|
||||
}
|
8
src/mouv
Normal file
8
src/mouv
Normal file
@ -0,0 +1,8 @@
|
||||
1 2 3 nope
|
||||
1 3 2 sa ra
|
||||
|
||||
3 2 1 sa rra
|
||||
3 1 2 ra
|
||||
|
||||
2 1 3 sa
|
||||
2 3 1 rra
|
40
src/operations/ft_pushstack.c
Normal file
40
src/operations/ft_pushstack.c
Normal file
@ -0,0 +1,40 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_pushstack.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/08 18:08:51 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 15:10:03 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
#include <unistd.h>
|
||||
|
||||
void ft_push_a(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
t_stack *tmp_a;
|
||||
|
||||
if (!stack_b || !(*stack_b))
|
||||
return ;
|
||||
ft_putendl_fd("pa", STDOUT_FILENO);
|
||||
tmp_a = *stack_b;
|
||||
*stack_b = (*stack_b)->next;
|
||||
tmp_a->next = *stack_a;
|
||||
*stack_a = tmp_a;
|
||||
}
|
||||
|
||||
void ft_push_b(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
t_stack *tmp_b;
|
||||
|
||||
if (!stack_a || !(*stack_a))
|
||||
return ;
|
||||
ft_putendl_fd("pb", STDOUT_FILENO);
|
||||
tmp_b = *stack_a;
|
||||
*stack_a = (*stack_a)->next;
|
||||
tmp_b->next = *stack_b;
|
||||
*stack_b = tmp_b;
|
||||
}
|
54
src/operations/ft_reverserotate.c
Normal file
54
src/operations/ft_reverserotate.c
Normal file
@ -0,0 +1,54 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_reverserotate.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 13:41:25 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 15:10:13 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
void ft_reverserotate(t_stack **stack)
|
||||
{
|
||||
t_stack *bfrlast;
|
||||
t_stack *last;
|
||||
t_stack *stack_start;
|
||||
|
||||
bfrlast = ft_stackbeforelast(*stack);
|
||||
last = ft_stacklast(*stack);
|
||||
bfrlast->next = NULL;
|
||||
stack_start = (*stack);
|
||||
(*stack) = last;
|
||||
(*stack)->next = stack_start;
|
||||
}
|
||||
|
||||
void ft_reverserotate_a(t_stack **stack_a)
|
||||
{
|
||||
if (!stack_a || !(*stack_a) || !(*stack_a)->next)
|
||||
return ;
|
||||
ft_putendl_fd("rra", STDOUT_FILENO);
|
||||
ft_reverserotate(stack_a);
|
||||
}
|
||||
|
||||
void ft_reverserotate_b(t_stack **stack_b)
|
||||
{
|
||||
if (!stack_b || !(*stack_b) || !(*stack_b)->next)
|
||||
return ;
|
||||
ft_putendl_fd("rrb", STDOUT_FILENO);
|
||||
ft_reverserotate(stack_b);
|
||||
}
|
||||
|
||||
void ft_reverserotate_r(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
if (!stack_a || !(*stack_a) || !(*stack_a)->next)
|
||||
return ;
|
||||
if (!stack_b || !(*stack_b) || !(*stack_b)->next)
|
||||
return ;
|
||||
ft_putendl_fd("rrr", STDOUT_FILENO);
|
||||
ft_reverserotate(stack_a);
|
||||
ft_reverserotate(stack_b);
|
||||
}
|
54
src/operations/ft_rotatestack.c
Normal file
54
src/operations/ft_rotatestack.c
Normal file
@ -0,0 +1,54 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_rotatestack.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:53:55 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 15:09:40 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
void ft_rotatestack(t_stack **stack)
|
||||
{
|
||||
t_stack *tmp_last;
|
||||
t_stack *start;
|
||||
|
||||
if (!stack || !(*stack))
|
||||
return ;
|
||||
start = (*stack)->next;
|
||||
tmp_last = ft_stacklast(*stack);
|
||||
tmp_last->next = *stack;
|
||||
(*stack)->next = NULL;
|
||||
(*stack) = start;
|
||||
}
|
||||
|
||||
void ft_rotatestack_a(t_stack **stack_a)
|
||||
{
|
||||
if (!stack_a || !(*stack_a) || !(*stack_a)->next)
|
||||
return ;
|
||||
ft_putendl_fd("ra", STDOUT_FILENO);
|
||||
ft_rotatestack(stack_a);
|
||||
}
|
||||
|
||||
void ft_rotatestack_b(t_stack **stack_b)
|
||||
{
|
||||
if (!stack_b || !(*stack_b) || !(*stack_b)->next)
|
||||
return ;
|
||||
ft_putendl_fd("rb", STDOUT_FILENO);
|
||||
ft_rotatestack(stack_b);
|
||||
}
|
||||
|
||||
void ft_rotatestack_r(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
if (!stack_a || !(*stack_a) || !(*stack_a)->next)
|
||||
return ;
|
||||
if (!stack_b || !(*stack_b) || !(*stack_b)->next)
|
||||
return ;
|
||||
ft_putendl_fd("rr", STDOUT_FILENO);
|
||||
ft_rotatestack(stack_a);
|
||||
ft_rotatestack(stack_b);
|
||||
}
|
49
src/operations/ft_swapstack.c
Normal file
49
src/operations/ft_swapstack.c
Normal file
@ -0,0 +1,49 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_swapstack.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/08 17:55:21 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 15:09:06 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
void ft_swapstack(t_stack *stack)
|
||||
{
|
||||
int tmp;
|
||||
|
||||
if (!stack)
|
||||
return ;
|
||||
tmp = stack->nb;
|
||||
stack->nb = stack->next->nb;
|
||||
stack->next->nb = tmp;
|
||||
}
|
||||
|
||||
void ft_swap_a(t_stack *stack_a)
|
||||
{
|
||||
if (!stack_a)
|
||||
return ;
|
||||
ft_putendl_fd("sa", STDOUT_FILENO);
|
||||
ft_swapstack(stack_a);
|
||||
}
|
||||
|
||||
void ft_swap_b(t_stack *stack_b)
|
||||
{
|
||||
if (!stack_b)
|
||||
return ;
|
||||
ft_putendl_fd("sb", STDOUT_FILENO);
|
||||
ft_swapstack(stack_b);
|
||||
}
|
||||
|
||||
void ft_stack_ss(t_stack *stack_a, t_stack *stack_b)
|
||||
{
|
||||
if (!stack_a || !stack_b)
|
||||
return ;
|
||||
ft_putendl_fd("ss", STDOUT_FILENO);
|
||||
ft_swapstack(stack_a);
|
||||
ft_swapstack(stack_b);
|
||||
}
|
68
src/parsing.c
Normal file
68
src/parsing.c
Normal file
@ -0,0 +1,68 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parsing.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 12:05:43 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 15:58:28 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
size_t ft_nbrlen(char *s)
|
||||
{
|
||||
char *tmp;
|
||||
char *nbrlen;
|
||||
|
||||
tmp = s;
|
||||
while (*tmp || *tmp == '-' || *tmp == '+' || *tmp == '0')
|
||||
tmp++;
|
||||
nbrlen = tmp;
|
||||
while (*nbrlen)
|
||||
nbrlen++;
|
||||
return (nbrlen - tmp);
|
||||
}
|
||||
|
||||
void ft_freearr(char **arr)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (arr[i])
|
||||
{
|
||||
free(arr[i]);
|
||||
i++;
|
||||
}
|
||||
free(arr);
|
||||
}
|
||||
|
||||
t_stack *ft_parsing(char **av)
|
||||
{
|
||||
t_stack *parsed_data;
|
||||
char **tmp_split;
|
||||
char **split;
|
||||
char **tmp_av;
|
||||
|
||||
tmp_av = av;
|
||||
tmp_av++;
|
||||
parsed_data = NULL;
|
||||
while (*tmp_av)
|
||||
{
|
||||
split = ft_split(*tmp_av, ' ');
|
||||
tmp_split = split;
|
||||
while (*tmp_split)
|
||||
{
|
||||
if (ft_nbrlen(*tmp_split) > 11 || ft_atoll(*tmp_split) > 2147483647 \
|
||||
|| ft_atoll(*tmp_split) < -2147483648)
|
||||
ft_senderror();
|
||||
ft_stackadd_back(&parsed_data, ft_stacknew(ft_atoi(*tmp_split)));
|
||||
tmp_split++;
|
||||
}
|
||||
ft_freearr(split);
|
||||
tmp_av++;
|
||||
}
|
||||
return (parsed_data);
|
||||
}
|
26
src/print_stack.c
Normal file
26
src/print_stack.c
Normal file
@ -0,0 +1,26 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* print_stack.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/15 11:05:52 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/04 12:36:31 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
void ft_print_stack(t_stack *stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
|
||||
tmp = stack;
|
||||
while (tmp)
|
||||
{
|
||||
ft_putnbr(tmp->nb);
|
||||
ft_putchar('\n');
|
||||
tmp = tmp->next;
|
||||
}
|
||||
}
|
73
src/push_swap.h
Normal file
73
src/push_swap.h
Normal file
@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* push_swap.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 12:18:29 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 16:07:50 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PUSH_SWAP_H
|
||||
# define PUSH_SWAP_H
|
||||
|
||||
# include "../libft/libft.h"
|
||||
|
||||
typedef struct s_stack
|
||||
{
|
||||
int nb;
|
||||
struct s_stack *next;
|
||||
} t_stack;
|
||||
|
||||
// stack
|
||||
void ft_stackadd_back(t_stack **stack, t_stack *new);
|
||||
void ft_stackadd_front(t_stack **stack, t_stack *new);
|
||||
void ft_stackclear(t_stack **stack);
|
||||
void ft_stackdelone(t_stack *stack);
|
||||
t_stack *ft_stacklast(t_stack *stack);
|
||||
t_stack *ft_stacknew(int content);
|
||||
size_t ft_stacksize(t_stack *stack);
|
||||
t_stack *ft_stackbeforelast(t_stack *stack);
|
||||
|
||||
// operations
|
||||
void ft_swap_a(t_stack *stack_a);
|
||||
void ft_swap_b(t_stack *stack_b);
|
||||
void ft_stack_ss(t_stack *stack_a, t_stack *stack_b);
|
||||
|
||||
void ft_push_a(t_stack **stack_a, t_stack **stack_b);
|
||||
void ft_push_b(t_stack **stack_a, t_stack **stack_b);
|
||||
|
||||
void ft_rotatestack_a(t_stack **stack_a);
|
||||
void ft_rotatestack_b(t_stack **stack_b);
|
||||
void ft_rotatestack_r(t_stack **stack_a, t_stack **stack_b);
|
||||
|
||||
void ft_reverserotate_a(t_stack **stack_a);
|
||||
void ft_reverserotate_b(t_stack **stack_b);
|
||||
void ft_reverserotate_r(t_stack **stack_a, t_stack **stack_b);
|
||||
|
||||
// algo
|
||||
void ft_algo(t_stack **stack_a, t_stack **stack_b);
|
||||
int find_median(t_stack **stack);
|
||||
void insert_nb(t_stack **stack_a, t_stack **stack_b);
|
||||
void while_insert(t_stack **stack_a, t_stack **stack_b);
|
||||
size_t get_lowest_cost(t_stack **stack_a, t_stack **stack_b);
|
||||
t_boolean ft_is_sort(t_stack **stack);
|
||||
int find_q_one(t_stack **stack);
|
||||
|
||||
// get_min_max
|
||||
size_t get_stack_max(t_stack **stack);
|
||||
size_t get_stack_min(t_stack **stack);
|
||||
|
||||
// error checkin
|
||||
void ft_check_args_format(char **av);
|
||||
t_stack *ft_parsing(char **av);
|
||||
void ft_check_double(t_stack **stack);
|
||||
void ft_print_stack(t_stack *stack);
|
||||
|
||||
// utils
|
||||
t_boolean ft_is_sort(t_stack **stack);
|
||||
void ft_senderror(void);
|
||||
|
||||
#endif
|
25
src/stack/ft_stackadd_back.c
Normal file
25
src/stack/ft_stackadd_back.c
Normal file
@ -0,0 +1,25 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_stackadd_back.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/05 11:40:39 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/22 13:32:31 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
void ft_stackadd_back(t_stack **stack, t_stack *new)
|
||||
{
|
||||
if (!stack)
|
||||
return ;
|
||||
if (!*stack)
|
||||
{
|
||||
(*stack) = new;
|
||||
return ;
|
||||
}
|
||||
ft_stacklast(*stack)->next = new;
|
||||
}
|
23
src/stack/ft_stackadd_front.c
Normal file
23
src/stack/ft_stackadd_front.c
Normal file
@ -0,0 +1,23 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_stackadd_front.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 18:52:38 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/22 13:32:21 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
void ft_stackadd_front(t_stack **stack, t_stack *new)
|
||||
{
|
||||
if (!stack)
|
||||
return ;
|
||||
if (!new)
|
||||
return ;
|
||||
new->next = *stack;
|
||||
*stack = new;
|
||||
}
|
26
src/stack/ft_stackclear.c
Normal file
26
src/stack/ft_stackclear.c
Normal file
@ -0,0 +1,26 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_stackclear.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 18:54:56 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/22 13:32:09 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
void ft_stackclear(t_stack **stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
|
||||
tmp = NULL;
|
||||
while (stack && *stack)
|
||||
{
|
||||
tmp = (*stack)->next;
|
||||
ft_stackdelone(*stack);
|
||||
*stack = tmp;
|
||||
}
|
||||
}
|
20
src/stack/ft_stackdelone.c
Normal file
20
src/stack/ft_stackdelone.c
Normal file
@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_stackdelone.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/06 10:27:14 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/22 13:31:53 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
void ft_stackdelone(t_stack *stack)
|
||||
{
|
||||
if (stack == NULL)
|
||||
return ;
|
||||
free(stack);
|
||||
}
|
37
src/stack/ft_stacklast.c
Normal file
37
src/stack/ft_stacklast.c
Normal file
@ -0,0 +1,37 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_stacklast.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/06 10:18:01 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/22 13:33:16 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
t_stack *ft_stacklast(t_stack *stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
|
||||
if (!stack)
|
||||
return (NULL);
|
||||
tmp = stack;
|
||||
while (tmp->next)
|
||||
tmp = tmp->next;
|
||||
return (tmp);
|
||||
}
|
||||
|
||||
t_stack *ft_stackbeforelast(t_stack *stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
|
||||
if (!stack)
|
||||
return (NULL);
|
||||
tmp = stack;
|
||||
while (tmp && tmp->next && tmp->next->next)
|
||||
tmp = tmp->next;
|
||||
return (tmp);
|
||||
}
|
25
src/stack/ft_stacknew.c
Normal file
25
src/stack/ft_stacknew.c
Normal file
@ -0,0 +1,25 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_stacknew.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/05 11:45:19 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/22 13:31:23 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
t_stack *ft_stacknew(int nb)
|
||||
{
|
||||
t_stack *stack;
|
||||
|
||||
stack = malloc(sizeof(t_stack));
|
||||
if (!stack)
|
||||
return (NULL);
|
||||
stack->nb = nb;
|
||||
stack->next = NULL;
|
||||
return (stack);
|
||||
}
|
26
src/stack/ft_stacksize.c
Normal file
26
src/stack/ft_stacksize.c
Normal file
@ -0,0 +1,26 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_stacksize.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/05 11:42:40 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/07 11:30:13 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
size_t ft_stacksize(t_stack *stack)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (stack)
|
||||
{
|
||||
stack = stack->next;
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
25
src/utils/is_sorted.c
Normal file
25
src/utils/is_sorted.c
Normal file
@ -0,0 +1,25 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* is_sorted.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/11 15:39:00 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 15:41:20 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
t_boolean ft_is_sort(t_stack **stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
|
||||
tmp = *stack;
|
||||
while (tmp->next && tmp->nb < tmp->next->nb)
|
||||
tmp = tmp->next;
|
||||
if (!tmp->next)
|
||||
return (TRUE);
|
||||
return (FALSE);
|
||||
}
|
19
src/utils/print_error.c
Normal file
19
src/utils/print_error.c
Normal file
@ -0,0 +1,19 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* print_error.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/11 15:47:05 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 15:54:09 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
void ft_senderror(void)
|
||||
{
|
||||
ft_putendl_fd("Error", STDOUT_FILENO);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
Reference in New Issue
Block a user