diff --git a/Makefile b/Makefile index 594e8ec..388a4ac 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/01 11:03:22 by adjoly #+# #+# # -# Updated: 2024/03/11 15:45:33 by adjoly ### ########.fr # +# Updated: 2024/03/13 13:21:22 by adjoly ### ########.fr # # # # **************************************************************************** # @@ -19,7 +19,6 @@ OBJSDIR = obj/ SRCDIR = src/ SRCS = main.c \ - check_error.c \ print_stack.c \ parsing.c \ stack/ft_stackadd_back.c \ @@ -37,7 +36,9 @@ SRCS = main.c \ algo/algo.c \ algo/insertion.c \ algo/median.c \ + algo/small_mouv.c \ utils/is_sorted.c \ + utils/print_error.c \ OBJS = $(addprefix $(OBJSDIR), $(SRCS:.c=.o)) diff --git a/libft b/libft index 63a5504..4ce5195 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 63a5504b82ded717184024b4e6783b92b289ee89 +Subproject commit 4ce51959edefb1e886670e784f755571abdd9274 diff --git a/mouv b/mouv new file mode 100644 index 0000000..5bf7973 --- /dev/null +++ b/mouv @@ -0,0 +1,8 @@ +1 2 3 +3 2 1 ra ra ok + +1 3 2 rra sa ok +2 3 1 rra ok + +2 1 3 sa ok +3 1 2 ra diff --git a/obj/algo/algo.o b/obj/algo/algo.o deleted file mode 100644 index d7fa48e..0000000 Binary files a/obj/algo/algo.o and /dev/null differ diff --git a/obj/algo/get_min_max.o b/obj/algo/get_min_max.o deleted file mode 100644 index 9760ce0..0000000 Binary files a/obj/algo/get_min_max.o and /dev/null differ diff --git a/obj/algo/insertion.o b/obj/algo/insertion.o deleted file mode 100644 index 67d01f6..0000000 Binary files a/obj/algo/insertion.o and /dev/null differ diff --git a/obj/algo/median.o b/obj/algo/median.o deleted file mode 100644 index d33084d..0000000 Binary files a/obj/algo/median.o and /dev/null differ diff --git a/obj/check_error.o b/obj/check_error.o deleted file mode 100644 index 255fb2d..0000000 Binary files a/obj/check_error.o and /dev/null differ diff --git a/obj/main.o b/obj/main.o deleted file mode 100644 index 3d12878..0000000 Binary files a/obj/main.o and /dev/null differ diff --git a/obj/operations/ft_pushstack.o b/obj/operations/ft_pushstack.o deleted file mode 100644 index 549d5dd..0000000 Binary files a/obj/operations/ft_pushstack.o and /dev/null differ diff --git a/obj/operations/ft_reverserotate.o b/obj/operations/ft_reverserotate.o deleted file mode 100644 index 89ad696..0000000 Binary files a/obj/operations/ft_reverserotate.o and /dev/null differ diff --git a/obj/operations/ft_rotatestack.o b/obj/operations/ft_rotatestack.o deleted file mode 100644 index 455e36c..0000000 Binary files a/obj/operations/ft_rotatestack.o and /dev/null differ diff --git a/obj/operations/ft_swapstack.o b/obj/operations/ft_swapstack.o deleted file mode 100644 index 5949e10..0000000 Binary files a/obj/operations/ft_swapstack.o and /dev/null differ diff --git a/obj/parsing.o b/obj/parsing.o deleted file mode 100644 index e376074..0000000 Binary files a/obj/parsing.o and /dev/null differ diff --git a/obj/print_stack.o b/obj/print_stack.o deleted file mode 100644 index 33eb899..0000000 Binary files a/obj/print_stack.o and /dev/null differ diff --git a/obj/stack/ft_stackadd_back.o b/obj/stack/ft_stackadd_back.o deleted file mode 100644 index 0a4c0d1..0000000 Binary files a/obj/stack/ft_stackadd_back.o and /dev/null differ diff --git a/obj/stack/ft_stackadd_front.o b/obj/stack/ft_stackadd_front.o deleted file mode 100644 index 0a79f34..0000000 Binary files a/obj/stack/ft_stackadd_front.o and /dev/null differ diff --git a/obj/stack/ft_stackclear.o b/obj/stack/ft_stackclear.o deleted file mode 100644 index 613092a..0000000 Binary files a/obj/stack/ft_stackclear.o and /dev/null differ diff --git a/obj/stack/ft_stackdelone.o b/obj/stack/ft_stackdelone.o deleted file mode 100644 index 846ca39..0000000 Binary files a/obj/stack/ft_stackdelone.o and /dev/null differ diff --git a/obj/stack/ft_stacklast.o b/obj/stack/ft_stacklast.o deleted file mode 100644 index 91c8035..0000000 Binary files a/obj/stack/ft_stacklast.o and /dev/null differ diff --git a/obj/stack/ft_stacknew.o b/obj/stack/ft_stacknew.o deleted file mode 100644 index dd20558..0000000 Binary files a/obj/stack/ft_stacknew.o and /dev/null differ diff --git a/obj/stack/ft_stacksize.o b/obj/stack/ft_stacksize.o deleted file mode 100644 index 1625cb2..0000000 Binary files a/obj/stack/ft_stacksize.o and /dev/null differ diff --git a/obj/utils/is_sorted.o b/obj/utils/is_sorted.o deleted file mode 100644 index 0905af1..0000000 Binary files a/obj/utils/is_sorted.o and /dev/null differ diff --git a/push_swap b/push_swap deleted file mode 100755 index 95fa249..0000000 Binary files a/push_swap and /dev/null differ diff --git a/src/algo/algo.c b/src/algo/algo.c index b07fd49..56b1f55 100644 --- a/src/algo/algo.c +++ b/src/algo/algo.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/21 17:29:26 by adjoly #+# #+# */ -/* Updated: 2024/03/11 16:06:26 by adjoly ### ########.fr */ +/* Updated: 2024/03/12 15:37:38 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,9 +49,13 @@ void ft_algo(t_stack **stack_a, t_stack **stack_b) } if (ft_stacksize(*stack_a) == 3) { + sort_three(stack_a); + return ; } if (ft_stacksize(*stack_a) == 5) { + sort_five(stack_a, stack_b); + return ; } send_to_stack_b(stack_a, stack_b); while_insert(stack_a, stack_b); diff --git a/src/algo/insertion.c b/src/algo/insertion.c index fb0f5e1..304d439 100644 --- a/src/algo/insertion.c +++ b/src/algo/insertion.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/23 14:49:14 by adjoly #+# #+# */ -/* Updated: 2024/03/11 16:03:14 by adjoly ### ########.fr */ +/* Updated: 2024/03/12 14:42:06 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,6 @@ 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) @@ -26,7 +24,6 @@ void prepare_to_send_a(t_stack **stack_b, size_t lowest_cost_b) } else { - i = 0; while (stack_b && *stack_b && lowest_cost_b < ft_stacksize(*stack_b)) { ft_reverserotate_b(stack_b); diff --git a/src/algo/small_mouv.c b/src/algo/small_mouv.c index 09e138e..14fa0d1 100644 --- a/src/algo/small_mouv.c +++ b/src/algo/small_mouv.c @@ -6,16 +6,58 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/11 16:12:47 by adjoly #+# #+# */ -/* Updated: 2024/03/11 16:24:11 by adjoly ### ########.fr */ +/* Updated: 2024/03/12 17:20:50 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "../push_swap.h" +#include -void sort_three(t_stack **stack_a) +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_swap_a(*stack_a); + else if (get_stack_max(stack_a) == 2 || get_stack_max(stack_a) == 0) + ft_rotatestack_a(stack_a); + else if (get_stack_max(stack_a) == 1) ft_reverserotate_a(stack_a); + if (ft_is_sort(stack_a) == FALSE) + sort_three(stack_a); +} + +void push_min_to_b(t_stack **stack_a, t_stack **stack_b) +{ + size_t i_min; + size_t i; + + i_min = get_stack_min(stack_a); + if (i_min < (ft_stacksize(*stack_a) / 2)) + { + while (i_min > 0) + { + ft_rotatestack_a(stack_a); + i_min--; + } + } + else + { + i = ft_stacksize(*stack_a); + while (i_min < i) + { + ft_reverserotate_a(stack_a); + i_min++; + } + } + ft_push_b(stack_a, stack_b); +} + +void sort_five(t_stack **stack_a, t_stack **stack_b) +{ + push_min_to_b(stack_a, stack_b); + push_min_to_b(stack_a, stack_b); + sort_three(stack_a); + ft_push_a(stack_a, stack_b); + ft_push_a(stack_a, stack_b); + if (ft_is_sort(stack_a) == FALSE) + ft_swap_a(*stack_a); } diff --git a/src/check_error.c b/src/check_error.c deleted file mode 100644 index 381d78b..0000000 --- a/src/check_error.c +++ /dev/null @@ -1,60 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* check_error.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* 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; - } -} diff --git a/src/main.c b/src/main.c index 9c2f5c0..478955f 100644 --- a/src/main.c +++ b/src/main.c @@ -6,70 +6,78 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/04 12:14:22 by adjoly #+# #+# */ -/* Updated: 2024/03/08 14:17:09 by adjoly ### ########.fr */ +/* Updated: 2024/03/13 14:44:42 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" -void ft_check_args(char **av) +void check_invalid_char(char **av) { char **tmp; - char *tmp_av; + char *tmp_args; tmp = av; tmp++; while (*tmp) { - tmp_av = *tmp; - while (*tmp_av) + tmp_args = *tmp; + while (*tmp_args) { - 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++; + if (!ft_isdigit(*tmp_args) && *tmp_args != '-' && *tmp_args != '+' && *tmp_args != 32) + ft_senderror(); + tmp_args++; } tmp++; } } + +void check_double(t_stack **stack) +{ + t_stack *tmp; + t_stack *check_dup; + + tmp = *stack; + while (tmp) + { + check_dup = tmp->next; + while (check_dup) + { + if (check_dup->nb == tmp->nb) + { + ft_stackclear(stack); + ft_senderror(); + } + check_dup = check_dup->next; + } + tmp = tmp->next; + } +} + 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); + return (EXIT_SUCCESS); + check_invalid_char(av); stack_b = NULL; stack_a = ft_parsing(av); if (ft_stacksize(stack_a) <= 1) { ft_stackclear(&stack_a); - exit(EXIT_SUCCESS); + return (EXIT_SUCCESS); } if (ft_is_sort(&stack_a) == TRUE) { ft_stackclear(&stack_a); - exit(EXIT_SUCCESS); + return (EXIT_SUCCESS); } - ft_check_double(&stack_a); + check_double(&stack_a); ft_algo(&stack_a, &stack_b); ft_stackclear(&stack_a); ft_stackclear(&stack_b); - return (0); + return (EXIT_SUCCESS); } diff --git a/src/mouv b/src/mouv index caebc11..d24d848 100644 --- a/src/mouv +++ b/src/mouv @@ -1,8 +1,8 @@ -1 2 3 nope -1 3 2 sa ra +1 2 3 +3 2 1 ra ra -3 2 1 sa rra -3 1 2 ra - -2 1 3 sa 2 3 1 rra +1 3 2 rra sa + +3 1 2 ra +2 1 3 sa diff --git a/src/parsing.c b/src/parsing.c index e4a6edc..f8e87e6 100644 --- a/src/parsing.c +++ b/src/parsing.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/04 12:05:43 by adjoly #+# #+# */ -/* Updated: 2024/03/11 15:58:28 by adjoly ### ########.fr */ +/* Updated: 2024/03/13 13:05:17 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/push_swap.h b/src/push_swap.h index 744ab00..abc85be 100644 --- a/src/push_swap.h +++ b/src/push_swap.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/04 12:18:29 by adjoly #+# #+# */ -/* Updated: 2024/03/11 16:07:50 by adjoly ### ########.fr */ +/* Updated: 2024/03/13 14:35:19 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,15 +55,15 @@ 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); +void sort_three(t_stack **stack_a); +void sort_five(t_stack **stack_a, t_stack **stack_b); // 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