diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..10d1df0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +visualizer-push-swap/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..8122dac --- /dev/null +++ b/.gitmodules @@ -0,0 +1,2 @@ +[submodule "libft"] + url = git@github.com:KeyZox71/libft_new.git diff --git a/Makefile b/Makefile index 4a4a49b..16c3c56 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # 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 \ diff --git a/algo.c b/algo.c new file mode 100644 index 0000000..13111ed --- /dev/null +++ b/algo.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* algo.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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); + } +} + diff --git a/check_error.c b/check_error.c new file mode 100644 index 0000000..9d880ff --- /dev/null +++ b/check_error.c @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include + +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; + } +} diff --git a/libft b/libft index 6af3df4..41548b6 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 6af3df4973756d3db79646a4c454d6b3de735b21 +Subproject commit 41548b6c5301f6a4f8d13baf9af9233d0e3a775d diff --git a/main.c b/main.c index b632e10..63d11cb 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/main.o b/main.o deleted file mode 100644 index b483b56..0000000 Binary files a/main.o and /dev/null differ diff --git a/operations/ft_pushstack.c b/operations/ft_pushstack.c index 827003a..8a7edbf 100644 --- a/operations/ft_pushstack.c +++ b/operations/ft_pushstack.c @@ -6,17 +6,20 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 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; diff --git a/operations/ft_pushstack.o b/operations/ft_pushstack.o deleted file mode 100644 index a52b646..0000000 Binary files a/operations/ft_pushstack.o and /dev/null differ diff --git a/operations/ft_reverserotate.c b/operations/ft_reverserotate.c index 0e6043f..7aeb3b3 100644 --- a/operations/ft_reverserotate.c +++ b/operations/ft_reverserotate.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); +} + diff --git a/operations/ft_reverserotate.o b/operations/ft_reverserotate.o deleted file mode 100644 index 0617fec..0000000 Binary files a/operations/ft_reverserotate.o and /dev/null differ diff --git a/operations/ft_rotatestack.c b/operations/ft_rotatestack.c index adb8102..e691fd5 100644 --- a/operations/ft_rotatestack.c +++ b/operations/ft_rotatestack.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); +} + diff --git a/operations/ft_rotatestack.o b/operations/ft_rotatestack.o deleted file mode 100644 index 88a3d21..0000000 Binary files a/operations/ft_rotatestack.o and /dev/null differ diff --git a/operations/ft_swapstack.c b/operations/ft_swapstack.c index 32fe222..9d437e3 100644 --- a/operations/ft_swapstack.c +++ b/operations/ft_swapstack.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/operations/ft_swapstack.o b/operations/ft_swapstack.o deleted file mode 100644 index 937a0a5..0000000 Binary files a/operations/ft_swapstack.o and /dev/null differ diff --git a/parsing.o b/parsing.o deleted file mode 100644 index ac178cc..0000000 Binary files a/parsing.o and /dev/null differ diff --git a/print_stack.c b/print_stack.c new file mode 100644 index 0000000..657c96c --- /dev/null +++ b/print_stack.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_stack.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; + } +} diff --git a/push_swap b/push_swap deleted file mode 100755 index de29711..0000000 Binary files a/push_swap and /dev/null differ diff --git a/push_swap.h b/push_swap.h index 6b7202d..6888319 100644 --- a/push_swap.h +++ b/push_swap.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/stack/ft_stackadd_back.o b/stack/ft_stackadd_back.o deleted file mode 100644 index 97fb729..0000000 Binary files a/stack/ft_stackadd_back.o and /dev/null differ diff --git a/stack/ft_stackadd_front.o b/stack/ft_stackadd_front.o deleted file mode 100644 index 5e426db..0000000 Binary files a/stack/ft_stackadd_front.o and /dev/null differ diff --git a/stack/ft_stackclear.o b/stack/ft_stackclear.o deleted file mode 100644 index 6503cd3..0000000 Binary files a/stack/ft_stackclear.o and /dev/null differ diff --git a/stack/ft_stackdelone.o b/stack/ft_stackdelone.o deleted file mode 100644 index 1d6f151..0000000 Binary files a/stack/ft_stackdelone.o and /dev/null differ diff --git a/stack/ft_stacklast.o b/stack/ft_stacklast.o deleted file mode 100644 index 3b99007..0000000 Binary files a/stack/ft_stacklast.o and /dev/null differ diff --git a/stack/ft_stacknew.o b/stack/ft_stacknew.o deleted file mode 100644 index 885d29f..0000000 Binary files a/stack/ft_stacknew.o and /dev/null differ diff --git a/stack/ft_stacksize.o b/stack/ft_stacksize.o deleted file mode 100644 index 7590633..0000000 Binary files a/stack/ft_stacksize.o and /dev/null differ