working algo 😂
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
visualizer-push-swap/
|
2
.gitmodules
vendored
Normal file
2
.gitmodules
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
[submodule "libft"]
|
||||
url = git@github.com:KeyZox71/libft_new.git
|
5
Makefile
5
Makefile
@ -6,7 +6,7 @@
|
||||
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/11/01 11:03:22 by adjoly #+# #+# #
|
||||
# Updated: 2024/02/13 14:04:13 by adjoly ### ########.fr #
|
||||
# Updated: 2024/02/21 17:40:53 by adjoly ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -15,6 +15,9 @@ NAME = push_swap
|
||||
CC = cc
|
||||
|
||||
SRCS = main.c \
|
||||
algo.c \
|
||||
check_error.c \
|
||||
print_stack.c \
|
||||
parsing.c \
|
||||
stack/ft_stackadd_back.c \
|
||||
stack/ft_stackadd_front.c \
|
||||
|
61
algo.c
Normal file
61
algo.c
Normal file
@ -0,0 +1,61 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* algo.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/21 17:29:26 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/21 19:19:55 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft/libft.h"
|
||||
#include "push_swap.h"
|
||||
#include <unistd.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);
|
||||
}
|
||||
|
||||
void ft_algo(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
size_t max;
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while ((*stack_a))
|
||||
ft_push_b(stack_a, stack_b);
|
||||
while (*stack_b)
|
||||
{
|
||||
max = get_stack_max(stack_b);
|
||||
i = 0;
|
||||
while (i < max)
|
||||
{
|
||||
ft_rotatestack_b(stack_b);
|
||||
i++;
|
||||
}
|
||||
ft_push_a(stack_a, stack_b);
|
||||
}
|
||||
}
|
||||
|
66
check_error.c
Normal file
66
check_error.c
Normal file
@ -0,0 +1,66 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* check_error.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/15 13:45:25 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/18 18:30:08 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft/libft.h"
|
||||
#include "push_swap.h"
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void ft_check_args_format(char **av)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
|
||||
i = 0;
|
||||
av++;
|
||||
while (*av)
|
||||
{
|
||||
j = 0;
|
||||
while (**av)
|
||||
{
|
||||
if (!ft_isdigit(**av) && **av != 32 && **av != '-' && **av != '+')
|
||||
{
|
||||
ft_putendl_fd("Error", STDERR_FILENO);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
j++;
|
||||
(*av)++;
|
||||
}
|
||||
(*av) -= j;
|
||||
i++;
|
||||
av++;
|
||||
}
|
||||
av -= i;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
2
libft
2
libft
Submodule libft updated: 6af3df4973...41548b6c53
34
main.c
34
main.c
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 12:14:22 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/13 14:04:47 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/02/21 17:29:15 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,31 +15,23 @@
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_stack *stack_a;
|
||||
t_stack *tmp;
|
||||
t_stack *stack_b;
|
||||
|
||||
stack_a = ft_parsing(ac, av);
|
||||
tmp = stack_a;
|
||||
if (ac < 2)
|
||||
{
|
||||
ft_putendl_fd("Error", STDERR_FILENO);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
ft_check_args_format(av);
|
||||
stack_b = NULL;
|
||||
while (tmp)
|
||||
stack_a = ft_parsing(ac, av);
|
||||
if (ft_stacksize(stack_a) <= 1)
|
||||
{
|
||||
ft_printf("%d\n", tmp->nb);
|
||||
tmp = tmp->next;
|
||||
}
|
||||
ft_reverserotate_a(&stack_a);
|
||||
tmp = stack_a;
|
||||
while (tmp)
|
||||
{
|
||||
ft_printf("%d\n", tmp->nb);
|
||||
tmp = tmp->next;
|
||||
}
|
||||
ft_printf("stackb\n");
|
||||
tmp = stack_b;
|
||||
while (tmp)
|
||||
{
|
||||
ft_printf("%d\n", tmp->nb);
|
||||
tmp = tmp->next;
|
||||
ft_stackclear(&stack_a);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
ft_check_double(&stack_a);
|
||||
ft_algo(&stack_a, &stack_b);
|
||||
ft_stackclear(&stack_a);
|
||||
ft_stackclear(&stack_b);
|
||||
return (0);
|
||||
|
@ -6,17 +6,20 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/08 18:08:51 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/12 17:24:55 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/02/21 17:55:12 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;
|
||||
|
||||
ft_putendl_fd("pb", 1);
|
||||
|
||||
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;
|
||||
@ -27,7 +30,9 @@ void ft_push_b(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
t_stack *tmp_b;
|
||||
|
||||
ft_putendl_fd("pb", 1);
|
||||
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;
|
||||
|
Binary file not shown.
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 13:41:25 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/13 14:05:13 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/02/15 13:30:20 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -30,13 +30,28 @@ void ft_reverserotate(t_stack **stack)
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
Binary file not shown.
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:53:55 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/13 13:36:51 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/02/15 13:32:27 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -16,7 +16,9 @@ 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;
|
||||
@ -26,13 +28,28 @@ void ft_rotatestack(t_stack **stack)
|
||||
|
||||
void ft_rotatestack_a(t_stack **stack_a)
|
||||
{
|
||||
if (!stack_a || !(*stack_a))
|
||||
return ;
|
||||
ft_putendl_fd("ra", STDOUT_FILENO);
|
||||
ft_rotatestack(stack_a);
|
||||
}
|
||||
|
||||
void ft_rotatestack_b(t_stack **stack_b)
|
||||
{
|
||||
if (!stack_b || !(*stack_b))
|
||||
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))
|
||||
return ;
|
||||
if (!stack_b || !(*stack_b))
|
||||
return ;
|
||||
ft_putendl_fd("rr", STDOUT_FILENO);
|
||||
ft_rotatestack(stack_a);
|
||||
ft_rotatestack(stack_b);
|
||||
}
|
||||
|
||||
|
Binary file not shown.
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/08 17:55:21 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/08 18:10:02 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/02/15 13:36:44 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,7 +15,9 @@
|
||||
void ft_swapstack(t_stack *stack)
|
||||
{
|
||||
int tmp;
|
||||
|
||||
|
||||
if (!stack)
|
||||
return ;
|
||||
tmp = stack->nb;
|
||||
stack->nb = stack->next->nb;
|
||||
stack->next->nb = tmp;
|
||||
@ -23,19 +25,25 @@ void ft_swapstack(t_stack *stack)
|
||||
|
||||
void ft_swap_a(t_stack *stack_a)
|
||||
{
|
||||
ft_putendl_fd("sa", 1);
|
||||
if (!stack_a)
|
||||
return ;
|
||||
ft_putendl_fd("sa", STDOUT_FILENO);
|
||||
ft_swapstack(stack_a);
|
||||
}
|
||||
|
||||
void ft_swap_b(t_stack *stack_b)
|
||||
{
|
||||
ft_putendl_fd("sb", 1);
|
||||
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)
|
||||
{
|
||||
ft_putendl_fd("ss", 1);
|
||||
if (!stack_a || !stack_b)
|
||||
return ;
|
||||
ft_putendl_fd("ss", STDOUT_FILENO);
|
||||
ft_swapstack(stack_a);
|
||||
ft_swapstack(stack_b);
|
||||
}
|
||||
|
Binary file not shown.
27
print_stack.c
Normal file
27
print_stack.c
Normal file
@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* print_stack.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/15 11:05:52 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/15 11:07:42 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft/libft.h"
|
||||
#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;
|
||||
}
|
||||
}
|
11
push_swap.h
11
push_swap.h
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 12:18:29 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/13 14:03:29 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/02/21 17:39:21 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -21,7 +21,11 @@ typedef struct s_stack
|
||||
|
||||
#include "libft/libft.h"
|
||||
|
||||
void ft_check_args_format(char **av);
|
||||
void ft_check_double(t_stack **stack);
|
||||
|
||||
t_stack *ft_parsing(int ac, char **av);
|
||||
void ft_print_stack(t_stack *stack);
|
||||
|
||||
void ft_stackadd_back(t_stack **stack, t_stack *new);
|
||||
void ft_stackadd_front(t_stack **stack, t_stack *new);
|
||||
@ -41,7 +45,12 @@ 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);
|
||||
|
||||
void ft_algo(t_stack **stack_a, t_stack **stack_b);
|
||||
|
||||
#endif
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user