├── code ├── en.subject.pdf ├── srcs │ ├── shared │ │ ├── algo_use.c │ │ ├── moves_nor.c │ │ ├── simple_free.c │ │ ├── orderby.c │ │ ├── ok_ko.c │ │ ├── use_libft.c │ │ ├── initial.c │ │ ├── swap_op.c │ │ ├── moves.c │ │ ├── errors.c │ │ ├── swap_case.c │ │ ├── checker_rd.c │ │ ├── to_use.c │ │ ├── print.c │ │ ├── list.c │ │ ├── use_libft1.c │ │ ├── algo_norm.c │ │ ├── algo_min_d.c │ │ ├── gen_sort.c │ │ ├── algo_min_c.c │ │ ├── list_op.c │ │ ├── algo_min.c │ │ ├── algo_d.c │ │ ├── algo_disp.c │ │ ├── swap_disp.c │ │ ├── algo_c.c │ │ ├── algo_500_c.c │ │ ├── option_v.c │ │ ├── algo.c │ │ └── algo_500.c │ ├── checker.c │ └── swap.c ├── Makefile └── includes │ └── push_swap.h └── README.md /code/en.subject.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zainabdnaya/Push_Swap_42/HEAD/code/en.subject.pdf -------------------------------------------------------------------------------- /code/srcs/shared/algo_use.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* algo_use.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zdnaya +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/10 15:07:20 by zdnaya #+# #+# */ 9 | /* Updated: 2021/04/12 16:53:25 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | int check_under_pivot(t_stack *a, int pivot) 16 | { 17 | while (a) 18 | { 19 | if (a->number <= pivot) 20 | return (1); 21 | a = a->next; 22 | } 23 | return (0); 24 | } 25 | 26 | int check_upper_pivot(t_stack *a, int pivot) 27 | { 28 | while (a) 29 | { 30 | if (a->number == pivot) 31 | return (1); 32 | a = a->next; 33 | } 34 | return (0); 35 | } 36 | 37 | int get_min(t_stack *a) 38 | { 39 | int min; 40 | 41 | min = a->number; 42 | while (a) 43 | { 44 | if (a->number < min) 45 | min = a->number; 46 | a = a->next; 47 | } 48 | return (min); 49 | } 50 | -------------------------------------------------------------------------------- /code/srcs/shared/moves_nor.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* moves_nor.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zdnaya +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/20 16:11:37 by zdnaya #+# #+# */ 9 | /* Updated: 2021/04/21 16:11:49 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void push_norm(t_stack **a, t_stack **b) 16 | { 17 | *b = (t_stack *)malloc(sizeof(t_stack)); 18 | (*b)->previous = NULL; 19 | (*b)->number = (*a)->number; 20 | (*b)->next = NULL; 21 | (*a) = (*a)->next; 22 | (*a)->previous = NULL; 23 | } 24 | 25 | void push_stack(t_stack **a, t_stack **b, t_stack *new) 26 | { 27 | new = NULL; 28 | if (*b == NULL && *a) 29 | push_norm(a, b); 30 | else 31 | { 32 | if (*a) 33 | { 34 | new = (t_stack *)malloc(sizeof(t_stack)); 35 | new->number = (*a)->number; 36 | new->next = NULL; 37 | new->previous = NULL; 38 | add_front(b, new); 39 | (*a) = (*a)->next; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /code/srcs/shared/simple_free.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* simple_free.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zdnaya +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/02 15:45:17 by zdnaya #+# #+# */ 9 | /* Updated: 2021/04/20 16:41:47 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void free_arg(char **arg) 16 | { 17 | if (*arg) 18 | free(*arg); 19 | *arg = NULL; 20 | } 21 | 22 | void ft_free_split(char **split) 23 | { 24 | int i; 25 | 26 | i = 0; 27 | while (split[i]) 28 | { 29 | free(split[i]); 30 | i++; 31 | } 32 | free(split); 33 | } 34 | 35 | void free_single_stack(t_stack **stack) 36 | { 37 | free(*stack); 38 | *stack = NULL; 39 | } 40 | 41 | void free_stack(t_stack **stack) 42 | { 43 | t_stack *tmp; 44 | 45 | if (*stack) 46 | { 47 | while (*stack) 48 | { 49 | tmp = (*stack)->next; 50 | free(*stack); 51 | *stack = NULL; 52 | (*stack) = tmp; 53 | } 54 | free(*stack); 55 | *stack = NULL; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /code/srcs/shared/orderby.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* orderby.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zainabdnayagmail.com number; 50 | i++; 51 | a = a->next; 52 | } 53 | a = tmp; 54 | avg = (int)(k / i); 55 | return (avg); 56 | } 57 | 58 | char **normal(t_all *all, int ac, char **av) 59 | { 60 | if (ac == 2) 61 | all->split = ft_split(av[1], ' '); 62 | else 63 | all->split = &av[1]; 64 | return (all->split); 65 | } 66 | -------------------------------------------------------------------------------- /code/srcs/shared/ok_ko.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ok_ko.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zainabdnayagmail.com split); 21 | check_ascii(all->split); 22 | all->a = put_in_list(all, all->split, new); 23 | all->len = size_list(all->a); 24 | return (all); 25 | } 26 | 27 | void checker_sort(t_all *all) 28 | { 29 | if (check_sort(&all->a, all->len) == 1) 30 | { 31 | free_stack(&all->a); 32 | free_stack(&all->b); 33 | ft_putstr_fd("OK\n", 1); 34 | exit(1); 35 | } 36 | else 37 | { 38 | free_stack(&all->a); 39 | free_stack(&all->b); 40 | ft_putstr_fd("KO\n", 1); 41 | exit(1); 42 | } 43 | } 44 | 45 | int check_sort(t_stack **a, int len) 46 | { 47 | t_stack *tmp; 48 | 49 | if (size_list(*a) != len) 50 | return (0); 51 | (void)len; 52 | tmp = (*a); 53 | while (tmp && tmp->next) 54 | { 55 | if ((tmp)->next != NULL && ((tmp)->number < (tmp)->next->number)) 56 | (tmp) = (tmp)->next; 57 | else 58 | return (0); 59 | } 60 | return (1); 61 | } 62 | -------------------------------------------------------------------------------- /code/srcs/shared/use_libft.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* use_libft.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zdnaya +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/16 00:13:19 by zainabdnaya #+# #+# */ 9 | /* Updated: 2021/04/20 12:17:54 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | int ft_isalpha(int c) 16 | { 17 | if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) 18 | return (1); 19 | else 20 | return (0); 21 | } 22 | 23 | void ft_putstr_fd(char *s, int fd) 24 | { 25 | int i; 26 | 27 | i = 0; 28 | if (!s) 29 | return ; 30 | while (s[i]) 31 | write(fd, &s[i++], 1); 32 | } 33 | 34 | void ft_putnbr_fd(int n, int fd) 35 | { 36 | if (n < 0) 37 | { 38 | if (n == -2147483648) 39 | ft_putstr_fd("-2147483648", fd); 40 | else 41 | { 42 | n = n * (-1); 43 | ft_putchar_fd('-', fd); 44 | ft_putnbr_fd(n, fd); 45 | } 46 | } 47 | else 48 | { 49 | if (n >= 10) 50 | { 51 | ft_putnbr_fd(n / 10, fd); 52 | ft_putchar_fd(n % 10 + '0', fd); 53 | } 54 | else 55 | ft_putchar_fd(n + '0', fd); 56 | } 57 | } 58 | 59 | void ft_putchar_fd(char c, int fd) 60 | { 61 | write(fd, &c, 1); 62 | } 63 | 64 | void ft_putendl_fd(char *s, int fd) 65 | { 66 | ft_putstr_fd(s, fd); 67 | ft_putchar_fd('\n', fd); 68 | } 69 | -------------------------------------------------------------------------------- /code/srcs/shared/initial.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* initial.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zdnaya +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/13 10:46:37 by zdnaya #+# #+# */ 9 | /* Updated: 2021/04/21 15:35:42 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | t_all *initial(t_all *all) 16 | { 17 | all = (t_all *)malloc(sizeof(t_all)); 18 | if (all == NULL) 19 | exit(1); 20 | all->a = NULL; 21 | all->b = NULL; 22 | all->len = 0; 23 | all->line = NULL; 24 | all->split = NULL; 25 | all->print = 0; 26 | all->color = 0; 27 | return (all); 28 | } 29 | 30 | void ft_bzero(void *s, size_t n) 31 | { 32 | unsigned int i; 33 | char *str; 34 | 35 | i = 0; 36 | str = s; 37 | while (i < n) 38 | { 39 | str[i] = '\0'; 40 | i++; 41 | } 42 | } 43 | 44 | int condition(char *line) 45 | { 46 | if (!ft_strcmp(line, "sa") || !ft_strcmp(line, "ra")) 47 | return (1); 48 | if (!ft_strcmp(line, "rra") || !ft_strcmp(line, "sb")) 49 | return (1); 50 | if (!ft_strcmp(line, "rb") || !ft_strcmp(line, "rrb")) 51 | return (1); 52 | if (!ft_strcmp(line, "pb") || !ft_strcmp(line, "pa")) 53 | return (1); 54 | if (!ft_strcmp(line, "ss")) 55 | return (1); 56 | if (!ft_strcmp(line, "rrr") || !ft_strcmp(line, "rr")) 57 | return (1); 58 | else 59 | return (0); 60 | } 61 | -------------------------------------------------------------------------------- /code/srcs/shared/swap_op.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* swap_op.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zdnaya +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/16 16:48:46 by zdnaya #+# #+# */ 9 | /* Updated: 2021/04/20 15:23:59 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void swap_c(t_all *all, int ac, char **av) 16 | { 17 | if (ac == 3) 18 | all->split = ft_split(av[2], ' '); 19 | else 20 | all->split = &av[2]; 21 | all = fill_in(all); 22 | if (all->len <= 10) 23 | sort_min_c(&(all->a), &(all->b), all->len); 24 | else if (all->len > 10 && all->len < 200) 25 | algo_1_c(&(all->a), &(all->b), all->len); 26 | else 27 | algo_c(&(all->a), &(all->b), all->len); 28 | free_stack(&all->a); 29 | free_arg(&all->line); 30 | if (ac == 3) 31 | ft_free_split(all->split); 32 | } 33 | 34 | void swap_sh(t_all *all, int ac, char **av) 35 | { 36 | t_stack *new; 37 | 38 | new = NULL; 39 | if (ac == 3) 40 | all->split = ft_split(av[2], ' '); 41 | else 42 | all->split = &av[2]; 43 | all = fill_in(all); 44 | print_all(all->a, all->b); 45 | if (all->len <= 10) 46 | sort_min_d(&(all->a), &(all->b), all->len); 47 | else if (all->len > 10 && all->len < 200) 48 | algo_1_d(&(all->a), &(all->b), all->len); 49 | else 50 | algo_d(&(all->a), &(all->b), all->len); 51 | free_stack(&new); 52 | free_stack(&all->a); 53 | free_arg(&all->line); 54 | if (ac == 3) 55 | ft_free_split(all->split); 56 | } 57 | -------------------------------------------------------------------------------- /code/srcs/shared/moves.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* moves.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zdnaya +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/02 13:59:39 by zdnaya #+# #+# */ 9 | /* Updated: 2021/04/20 16:11:18 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void swap_stack(t_stack **head) 16 | { 17 | int k; 18 | int l; 19 | t_stack *tmp; 20 | 21 | tmp = NULL; 22 | if ((*head) && (*head)->next) 23 | { 24 | tmp = *head; 25 | k = (*head)->number; 26 | l = (*head)->next->number; 27 | tmp = *head; 28 | (*head)->number = l; 29 | tmp = *head; 30 | (*head)->next->number = k; 31 | (*head) = tmp; 32 | } 33 | } 34 | 35 | void r_stack(t_stack **head) 36 | { 37 | if (*head) 38 | { 39 | *head = (*head)->next; 40 | add_back1(head); 41 | } 42 | } 43 | 44 | t_stack *rr_part1(t_stack *head, t_stack *tmp) 45 | { 46 | while (head->next) 47 | head = head->next; 48 | tmp->number = head->number; 49 | tmp->next = NULL; 50 | tmp->previous = NULL; 51 | return (tmp); 52 | } 53 | 54 | void botom(t_stack **head) 55 | { 56 | while ((*head)) 57 | { 58 | if ((*head)->next->next == NULL) 59 | break ; 60 | *head = (*head)->next; 61 | } 62 | free((*head)->next); 63 | (*head)->next = NULL; 64 | } 65 | 66 | void rr_stack(t_stack **head, t_stack *tmp) 67 | { 68 | t_stack *a; 69 | 70 | a = NULL; 71 | tmp = NULL; 72 | if (*head) 73 | { 74 | tmp = (t_stack *)malloc(sizeof(t_stack)); 75 | if (tmp == NULL) 76 | return ; 77 | tmp = rr_part1(*head, tmp); 78 | a = *head; 79 | botom(head); 80 | *head = a; 81 | add_front(head, tmp); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /code/srcs/shared/errors.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* errors.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zainabdnayagmail.com 1) 21 | { 22 | ft_putstr_fd("rrb\n", all->fd); 23 | rr_stack(b, tmp); 24 | free(tmp); 25 | } 26 | else if (w == 7 && size_list(*b) > 1) 27 | { 28 | ft_putstr_fd("sb\n", all->fd); 29 | swap_stack(b); 30 | } 31 | else if (w == 8 && size_list(*a) > 1) 32 | { 33 | ft_putstr_fd("sa\n", all->fd); 34 | swap_stack(a); 35 | } 36 | } 37 | 38 | void switch_case_norm(t_stack **a, t_stack **b, int w, t_all *all) 39 | { 40 | t_stack *tmp; 41 | 42 | tmp = NULL; 43 | if (w == 3 && size_list(*a) > 1) 44 | { 45 | ft_putstr_fd("rra\n", all->fd); 46 | rr_stack(a, tmp); 47 | } 48 | else if (w == 4) 49 | { 50 | ft_putstr_fd("pa\n", all->fd); 51 | push_stack(b, a, tmp); 52 | free(tmp); 53 | } 54 | else if (w == 5 && size_list(*b) > 1) 55 | { 56 | ft_putstr_fd("rb\n", all->fd); 57 | r_stack(b); 58 | free(tmp); 59 | } 60 | else 61 | switch_c_norm(a, b, w, all); 62 | } 63 | 64 | void switch_case(t_stack **a, t_stack **b, int w, t_all *all) 65 | { 66 | t_stack *tmp; 67 | 68 | tmp = NULL; 69 | if (w == 1) 70 | { 71 | ft_putstr_fd("pb\n", all->fd); 72 | push_stack(a, b, tmp); 73 | free(tmp); 74 | } 75 | else if (w == 2) 76 | { 77 | ft_putstr_fd("ra\n", all->fd); 78 | r_stack(a); 79 | free(tmp); 80 | } 81 | else 82 | switch_case_norm(a, b, w, all); 83 | } 84 | -------------------------------------------------------------------------------- /code/srcs/shared/checker_rd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* checker_rd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zdnaya +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/13 10:15:44 by zdnaya #+# #+# */ 9 | /* Updated: 2021/04/21 15:51:07 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void free_condition_(char **str) 16 | { 17 | ft_free_split(str); 18 | ft_putstr_fd("Error\n", 2); 19 | exit(1); 20 | } 21 | 22 | int condition_(char *line) 23 | { 24 | char **str; 25 | int i; 26 | 27 | str = ft_split(line, '\n'); 28 | i = 0; 29 | while (str[i]) 30 | { 31 | if (condition(str[i]) == 1) 32 | { 33 | i++; 34 | continue ; 35 | } 36 | else 37 | free_condition_(str); 38 | } 39 | ft_free_split(str); 40 | return (1); 41 | } 42 | 43 | void checker_pars_v(t_stack **a, t_stack **b, char *line) 44 | { 45 | t_stack *tmp; 46 | 47 | tmp = NULL; 48 | if (!ft_strcmp(line, "sa\n") && size_list(*a) > 1) 49 | swap_stack(a); 50 | else if (!ft_strcmp(line, "ra\n") && size_list(*a) > 1) 51 | r_stack(a); 52 | else if (!ft_strcmp(line, "rra\n") && size_list(*a) > 1) 53 | rr_stack(a, tmp); 54 | else if (!ft_strcmp(line, "sb\n") && size_list(*b) > 1) 55 | swap_stack(b); 56 | else if (!ft_strcmp(line, "rb\n") && size_list(*b) > 1) 57 | r_stack(b); 58 | else if (!ft_strcmp(line, "rrb\n") && size_list(*b) > 1) 59 | rr_stack(b, tmp); 60 | else if (!ft_strcmp(line, "pb\n")) 61 | push_stack(a, b, tmp); 62 | else if (!ft_strcmp(line, "pa\n")) 63 | push_stack(b, a, tmp); 64 | else if (!ft_strcmp(line, "ss\n") && size_list(*b) > 1 && size_list(*a) > 1) 65 | ss(a, b); 66 | else if (!ft_strcmp(line, "rr\n") && size_list(*b) > 1 && size_list(*a) > 1) 67 | rr(a, b); 68 | else if (!ft_strcmp(line, "rrr\n") && size_list(*b) > 1 && size_list(*a) > 1) 69 | rrr(a, b); 70 | } 71 | -------------------------------------------------------------------------------- /code/srcs/shared/to_use.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* to_use.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zdnaya +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/02 15:33:49 by zdnaya #+# #+# */ 9 | /* Updated: 2021/04/10 14:46:56 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | int val_aprox(float m) 16 | { 17 | int k; 18 | 19 | k = (int) m; 20 | if ((m - k) >= 0.5) 21 | return ((int) m + 1); 22 | else 23 | return ((int) m); 24 | } 25 | 26 | int ft_strcmp(const char *s1, const char *s2) 27 | { 28 | size_t i; 29 | 30 | i = 0; 31 | if ((!s1 || !s2)) 32 | return (-1); 33 | while (s1[i] != '\0' && s2[i] != '\0') 34 | { 35 | if (s1[i] != s2[i]) 36 | return ((unsigned char)s1[i] - (unsigned char)s2[i]); 37 | i++; 38 | } 39 | if (((s1[i] == '\0' && s2[i] != '\0') || (s2[i] == '\0' && s1[i] != '\0'))) 40 | return ((unsigned char)s1[i] - (unsigned char)s2[i]); 41 | return (0); 42 | } 43 | 44 | int size_list(t_stack *a) 45 | { 46 | int len; 47 | 48 | len = 0; 49 | while (a) 50 | { 51 | len++; 52 | a = a->next; 53 | } 54 | return (len); 55 | } 56 | 57 | static long long operation(const char *str, long lenght, int sign) 58 | { 59 | long long r; 60 | 61 | r = 0; 62 | while (str[lenght] >= 48 && str[lenght] <= 57) 63 | { 64 | r = r * 10 + str[lenght] - '0'; 65 | lenght++; 66 | } 67 | return (sign * r); 68 | } 69 | 70 | long long my_atoi(const char *str) 71 | { 72 | long lenght; 73 | int sign; 74 | 75 | lenght = 0; 76 | sign = 1; 77 | while (str[lenght] == ' ' || str[lenght] == '\n' || str[lenght] == '\t' 78 | || str[lenght] == '\r' || str[lenght] == '\f' || str[lenght] == '\v') 79 | lenght++; 80 | if (str[lenght] == '-' || str[lenght] == '+') 81 | { 82 | if (str[lenght] == '-') 83 | sign *= -1; 84 | lenght++; 85 | } 86 | return (operation(str, lenght, sign)); 87 | } 88 | -------------------------------------------------------------------------------- /code/srcs/shared/print.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* print.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zainabdnayagmail.com 10 && len <= 100) 20 | usleep(100000); 21 | else if (len > 100) 22 | usleep(10000); 23 | } 24 | 25 | void display(t_stack *a, t_stack *b) 26 | { 27 | system("clear"); 28 | ft_putstr_fd("\n\t-------------------------------\n", 1); 29 | ft_putstr_fd(" \tA \tB ", 1); 30 | ft_putstr_fd("\n\t-------------------------------\n", 1); 31 | while ((a) || (b)) 32 | { 33 | ft_putstr_fd("\t ", 1); 34 | if ((a)) 35 | ft_putnbr_fd((a)->number, 1); 36 | else 37 | ft_putstr_fd(" ", 1); 38 | ft_putstr_fd("\t ", 1); 39 | if (b) 40 | ft_putnbr_fd((b)->number, 1); 41 | else 42 | ft_putstr_fd(" ", 1); 43 | ft_putstr_fd("\n", 1); 44 | if (a) 45 | (a) = (a)->next; 46 | if (b) 47 | b = (b)->next; 48 | } 49 | display_norm(size_list(a)); 50 | } 51 | 52 | void print_list(t_stack *ptr) 53 | { 54 | t_stack *tmp; 55 | 56 | tmp = ptr; 57 | while (ptr) 58 | { 59 | ft_putstr_fd("{", 1); 60 | ft_putnbr_fd(ptr->number, 1); 61 | ft_putstr_fd("}⇄", 1); 62 | ptr = ptr->next; 63 | } 64 | ft_putstr_fd("\n", 1); 65 | ptr = tmp; 66 | return ; 67 | } 68 | 69 | void print_all(t_stack *a, t_stack *b) 70 | { 71 | if (a) 72 | { 73 | ft_putstr_fd("a:", 1); 74 | print_list(a); 75 | } 76 | else if (a == NULL) 77 | ft_putstr_fd("a is empty \n", 1); 78 | if (b) 79 | { 80 | ft_putstr_fd("b:", 1); 81 | print_list(b); 82 | } 83 | else if (b == NULL) 84 | ft_putstr_fd("b is empty \n", 1); 85 | } 86 | -------------------------------------------------------------------------------- /code/srcs/shared/list.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* list.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zdnaya +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/02 17:07:59 by zdnaya #+# #+# */ 9 | /* Updated: 2021/04/20 16:08:11 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void add_back(t_stack **head, t_stack *new) 16 | { 17 | t_stack *tmp; 18 | 19 | if (*head == NULL) 20 | *head = new; 21 | else 22 | { 23 | tmp = *head; 24 | while (tmp->next) 25 | tmp = tmp->next; 26 | tmp->next = new; 27 | new->previous = tmp; 28 | } 29 | } 30 | 31 | void add_back1(t_stack **head) 32 | { 33 | t_stack *tmp; 34 | t_stack *tmps; 35 | 36 | tmp = NULL; 37 | tmp = *head; 38 | tmps = (*head)->previous; 39 | while (tmp->next) 40 | tmp = tmp->next; 41 | tmp->next = tmps; 42 | tmp->next->previous = tmp; 43 | tmp->next->next = NULL; 44 | } 45 | 46 | void add_front(t_stack **head, t_stack *new_node) 47 | { 48 | new_node->next = (*head); 49 | new_node->previous = NULL; 50 | if ((*head) != NULL) 51 | (*head)->previous = new_node; 52 | (*head) = new_node; 53 | } 54 | 55 | int node_nbr(t_stack **head, int index) 56 | { 57 | t_stack *tmp; 58 | int count; 59 | 60 | count = 0; 61 | tmp = *head; 62 | while (tmp != NULL) 63 | { 64 | if (count == index) 65 | return (tmp->number); 66 | count++; 67 | tmp = tmp->next; 68 | } 69 | return (1); 70 | } 71 | 72 | t_stack *dup_list(t_stack *head) 73 | { 74 | t_stack *tmp; 75 | t_stack *tmp2; 76 | 77 | tmp2 = malloc(sizeof(t_stack)); 78 | ft_bzero(tmp2, sizeof(t_stack)); 79 | tmp = tmp2; 80 | while (head) 81 | { 82 | tmp2->number = head->number; 83 | if (head->next) 84 | { 85 | tmp2->next = malloc(sizeof(t_stack)); 86 | ft_bzero(tmp2->next, sizeof(t_stack)); 87 | tmp2 = tmp2->next; 88 | } 89 | head = head->next; 90 | } 91 | tmp2 = tmp; 92 | return (tmp2); 93 | } 94 | -------------------------------------------------------------------------------- /code/srcs/shared/use_libft1.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* use_libft1.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zainabdnayagmail.com +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/20 13:41:24 by zdnaya #+# #+# */ 9 | /* Updated: 2021/04/20 16:07:47 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void norm_part1(t_stack **a, t_stack **b, t_all *all, int m) 16 | { 17 | int index; 18 | int proximity; 19 | 20 | index = get_index((*a), m); 21 | proximity = (size_list((*a)) / 2); 22 | if (proximity > index) 23 | switch_case(a, b, 2, all); 24 | else 25 | switch_case(a, b, 3, all); 26 | } 27 | 28 | void norm_part3(t_stack **a, t_stack **b, t_all *all, int max) 29 | { 30 | int index; 31 | int proximity; 32 | 33 | index = get_index_max((*b), max); 34 | proximity = val_aprox((size_list((*b)) / 2)); 35 | if (proximity > index && *b) 36 | switch_case(a, b, 5, all); 37 | else if (*b) 38 | switch_case(a, b, 6, all); 39 | } 40 | 41 | t_stack *bottom(t_stack *a) 42 | { 43 | t_stack *bottom; 44 | 45 | bottom = a; 46 | while ((bottom)->next) 47 | (bottom) = (bottom)->next; 48 | return (bottom); 49 | } 50 | 51 | void norm_part1_c(t_stack **a, t_stack **b, int m, int k) 52 | { 53 | int index; 54 | int proximity; 55 | 56 | index = get_index((*a), m); 57 | proximity = (size_list((*a)) / 2); 58 | if (k == 1) 59 | { 60 | if (proximity > index) 61 | switch_case_color(a, b, 2); 62 | else 63 | switch_case_color(a, b, 3); 64 | } 65 | else 66 | { 67 | if (proximity > index) 68 | s_c_display(a, b, 2); 69 | else 70 | s_c_display(a, b, 3); 71 | } 72 | } 73 | 74 | void norm_part3_c(t_stack **a, t_stack **b, int max, int k) 75 | { 76 | int index; 77 | int proximity; 78 | 79 | index = get_index_max((*b), max); 80 | proximity = val_aprox((size_list((*b)) / 2)); 81 | if (k == 1) 82 | { 83 | if (proximity > index && *b) 84 | switch_case_color(a, b, 5); 85 | else if (*b) 86 | switch_case_color(a, b, 6); 87 | } 88 | else 89 | { 90 | if (proximity > index && *b) 91 | s_c_display(a, b, 5); 92 | else if (*b) 93 | s_c_display(a, b, 6); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /code/srcs/shared/algo_min_d.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* algo_min_d.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zainabdnayagmail.com number < (*a)->next->number && bottom1->number == min) 31 | s_c_display(a, b, 3); 32 | else if ((*a)->number > (*a)->next->number && bottom1->number == min) 33 | sort_norm_d(a, b); 34 | else if ((*a)->next->number == min) 35 | { 36 | if ((*a)->number < bottom1->number) 37 | s_c_display(a, b, 8); 38 | else if ((*a)->number > bottom1->number) 39 | s_c_display(a, b, 2); 40 | } 41 | else if ((*a)->number == min && (*a)->next->number > bottom1->number) 42 | { 43 | s_c_display(a, b, 8); 44 | s_c_display(a, b, 2); 45 | } 46 | } 47 | } 48 | 49 | void sort_min1_d(t_stack **a, t_stack **b, int len) 50 | { 51 | int size; 52 | int m; 53 | t_stack *ss; 54 | t_stack *tmp; 55 | 56 | size = len; 57 | while (size > 3) 58 | { 59 | m = get_min(*a); 60 | tmp = (*a)->next; 61 | if ((*a)->number != m && tmp && tmp->number == m) 62 | s_c_display(a, b, 8); 63 | while (m != (*a)->number) 64 | norm_part1_c(a, b, m, 0); 65 | if (m == (*a)->number) 66 | { 67 | ss = *a; 68 | s_c_display(a, b, 1); 69 | free(ss); 70 | size--; 71 | } 72 | } 73 | } 74 | 75 | void sort_min_d(t_stack **a, t_stack **b, int len) 76 | { 77 | t_stack *ss; 78 | 79 | ss = NULL; 80 | if (len == 3) 81 | sort_a_3_d(a, b, len); 82 | else if (len > 3) 83 | { 84 | sort_min1_d(a, b, len); 85 | sort_a_3_d(a, b, 3); 86 | while ((*b)) 87 | { 88 | ss = *b; 89 | s_c_display(a, b, 4); 90 | free(ss); 91 | } 92 | free_stack(b); 93 | } 94 | free_stack(a); 95 | } 96 | -------------------------------------------------------------------------------- /code/srcs/shared/gen_sort.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* gen_sort.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zdnaya +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/05 13:45:52 by zdnaya #+# #+# */ 9 | /* Updated: 2021/04/21 17:42:30 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void swap(t_stack *one, t_stack *two) 16 | { 17 | int k; 18 | 19 | k = one->number; 20 | one->number = two->number; 21 | two->number = k; 22 | } 23 | 24 | t_stack *sort(t_stack *head) 25 | { 26 | t_stack *start; 27 | t_stack *tmp2; 28 | t_stack *min; 29 | 30 | start = NULL; 31 | start = head; 32 | tmp2 = NULL; 33 | min = NULL; 34 | while (start->next) 35 | { 36 | min = start; 37 | tmp2 = start->next; 38 | while (tmp2) 39 | { 40 | if (min->number > tmp2->number) 41 | min = tmp2; 42 | tmp2 = tmp2->next; 43 | } 44 | swap(start, min); 45 | start = start->next; 46 | } 47 | return (head); 48 | } 49 | 50 | void checker_pars_n(t_stack **a, t_stack **b, char *line, t_stack *tmp) 51 | { 52 | if (!ft_strcmp(line, "pb") && size_list(*a) > 0) 53 | push_stack(a, b, tmp); 54 | else if (!ft_strcmp(line, "pa") && size_list(*b) > 0) 55 | push_stack(b, a, tmp); 56 | else if (!ft_strcmp(line, "ss") && size_list(*b) > 1 && size_list(*a) > 1) 57 | ss(a, b); 58 | else if (!ft_strcmp(line, "rr") && size_list(*b) > 1 && size_list(*a) > 1) 59 | rr(a, b); 60 | else if (!ft_strcmp(line, "rrr") && size_list(*b) > 1 && size_list(*a) > 1) 61 | rrr(a, b); 62 | } 63 | 64 | void checker_pars(t_stack **a, t_stack **b, char *line) 65 | { 66 | t_stack *tmp; 67 | 68 | tmp = NULL; 69 | if (!ft_strcmp(line, "sa") && size_list(*a) > 1) 70 | swap_stack(a); 71 | else if (!ft_strcmp(line, "ra") && size_list(*a) > 1) 72 | r_stack(a); 73 | else if (!ft_strcmp(line, "rra") && size_list(*a) > 1) 74 | rr_stack(a, tmp); 75 | else if (!ft_strcmp(line, "sb") && size_list(*b) > 1) 76 | swap_stack(b); 77 | else if (!ft_strcmp(line, "rb") && size_list(*b) > 1) 78 | r_stack(b); 79 | else if (!ft_strcmp(line, "rrb") && size_list(*b) > 1) 80 | rr_stack(b, tmp); 81 | else 82 | checker_pars_n(a, b, line, tmp); 83 | free_stack(&tmp); 84 | } 85 | -------------------------------------------------------------------------------- /code/srcs/shared/algo_min_c.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* algo_min_c.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zainabdnayagmail.com number < (*a)->next->number && bottom1->number == min) 31 | switch_case_color(a, b, 3); 32 | else if ((*a)->number > (*a)->next->number && bottom1->number == min) 33 | sort_norm_c(a, b); 34 | else if ((*a)->next->number == min) 35 | { 36 | if ((*a)->number < bottom1->number) 37 | switch_case_color(a, b, 8); 38 | else if ((*a)->number > bottom1->number) 39 | switch_case_color(a, b, 2); 40 | } 41 | else if ((*a)->number == min && (*a)->next->number > bottom1->number) 42 | { 43 | switch_case_color(a, b, 8); 44 | switch_case_color(a, b, 2); 45 | } 46 | } 47 | } 48 | 49 | void sort_min1_c(t_stack **a, t_stack **b, int len) 50 | { 51 | int size; 52 | int m; 53 | t_stack *ss; 54 | t_stack *tmp; 55 | 56 | size = len; 57 | while (size > 3) 58 | { 59 | m = get_min(*a); 60 | tmp = (*a)->next; 61 | if ((*a)->number != m && tmp && tmp->number == m) 62 | switch_case_color(a, b, 8); 63 | while (m != (*a)->number) 64 | norm_part1_c(a, b, m, 1); 65 | if (m == (*a)->number) 66 | { 67 | ss = *a; 68 | switch_case_color(a, b, 1); 69 | free(ss); 70 | size--; 71 | } 72 | } 73 | } 74 | 75 | void sort_min_c(t_stack **a, t_stack **b, int len) 76 | { 77 | t_stack *ss; 78 | 79 | ss = NULL; 80 | if (len == 3) 81 | sort_c_3(a, b, len); 82 | else if (len > 3) 83 | { 84 | sort_min1_c(a, b, len); 85 | sort_c_3(a, b, 3); 86 | while ((*b)) 87 | { 88 | ss = *b; 89 | switch_case_color(a, b, 4); 90 | free(ss); 91 | } 92 | free_stack(b); 93 | } 94 | free_stack(a); 95 | } 96 | -------------------------------------------------------------------------------- /code/srcs/shared/list_op.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* list_op.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zainabdnayagmail.com number = my_atoi(split[i]); 26 | if (new->number > 2147483647 || new->number < -2147483647) 27 | { 28 | ft_putstr_fd("Error\n", 2); 29 | exit(1); 30 | } 31 | new->next = NULL; 32 | new->previous = NULL; 33 | add_back(&all->a, new); 34 | new = NULL; 35 | i++; 36 | } 37 | return (all->a); 38 | } 39 | 40 | int get_pivot(t_stack *c) 41 | { 42 | t_stack *result; 43 | int len; 44 | int m; 45 | int k; 46 | 47 | m = 0; 48 | k = 0; 49 | len = 0; 50 | result = NULL; 51 | len = size_list(c); 52 | result = sort(c); 53 | if (len <= 12) 54 | m = val_aprox(len / 2); 55 | else if (len > 12 && len < 200) 56 | m = val_aprox(len / 4); 57 | else if (len >= 200) 58 | m = val_aprox(len / 8); 59 | k = node_nbr(&result, m); 60 | return (k); 61 | } 62 | 63 | int get_index(t_stack *a, int pivot) 64 | { 65 | int i; 66 | 67 | i = 1; 68 | while (a) 69 | { 70 | if (a->number <= pivot) 71 | return (i); 72 | a = a->next; 73 | i++; 74 | } 75 | return (-1); 76 | } 77 | 78 | int get_index_max(t_stack *a, int pivot) 79 | { 80 | int i; 81 | 82 | i = 1; 83 | while (a) 84 | { 85 | if (a->number >= pivot) 86 | return (i); 87 | a = a->next; 88 | i++; 89 | } 90 | return (-1); 91 | } 92 | 93 | int get_max(t_stack *b) 94 | { 95 | int max; 96 | t_stack *tmp; 97 | 98 | max = 0; 99 | tmp = NULL; 100 | if (b) 101 | { 102 | max = b->number; 103 | tmp = b; 104 | while (b) 105 | { 106 | if (b->number >= max) 107 | max = b->number; 108 | b = b->next; 109 | } 110 | b = tmp; 111 | } 112 | return (max); 113 | } 114 | -------------------------------------------------------------------------------- /code/srcs/shared/algo_min.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* algo_min.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zainabdnayagmail.com number < (*a)->next->number && bottom1->number == min) 31 | switch_case(a, b, 3, all); 32 | else if ((*a)->number > (*a)->next->number && bottom1->number == min) 33 | sort_norm_3(a, b, all); 34 | else if ((*a)->next->number == min) 35 | { 36 | if ((*a)->number < bottom1->number) 37 | switch_case(a, b, 8, all); 38 | else if ((*a)->number > bottom1->number) 39 | switch_case(a, b, 2, all); 40 | } 41 | else if ((*a)->number == min && (*a)->next->number > bottom1->number) 42 | { 43 | switch_case(a, b, 8, all); 44 | switch_case(a, b, 2, all); 45 | } 46 | } 47 | } 48 | 49 | void sort_min1(t_stack **a, t_stack **b, int len, t_all *all) 50 | { 51 | int size; 52 | int m; 53 | t_stack *ss; 54 | t_stack *tmp; 55 | 56 | size = len; 57 | while (size > 3) 58 | { 59 | m = get_min(*a); 60 | tmp = (*a)->next; 61 | if ((*a)->number != m && tmp && tmp->number == m) 62 | switch_case(a, b, 8, all); 63 | while (m != (*a)->number) 64 | norm_part1(a, b, all, m); 65 | if (m == (*a)->number) 66 | { 67 | ss = *a; 68 | switch_case(a, b, 1, all); 69 | free(ss); 70 | size--; 71 | } 72 | } 73 | } 74 | 75 | void sort_min(t_stack **a, t_stack **b, int len, t_all *all) 76 | { 77 | t_stack *ss; 78 | 79 | ss = NULL; 80 | if (len == 2 && (*a)->number > (*a)->next->number) 81 | switch_case(a, b, 8, all); 82 | if (len <= 3) 83 | sort_a_3(a, b, len, all); 84 | else if (len > 3) 85 | { 86 | sort_min1(a, b, len, all); 87 | sort_a_3(a, b, 3, all); 88 | while ((*b)) 89 | { 90 | ss = *b; 91 | switch_case(a, b, 4, all); 92 | free(ss); 93 | } 94 | free_stack(b); 95 | } 96 | free_stack(a); 97 | } 98 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # push_swap 2 | sort data on a stack, with a limited set of instructions, using the lowest possible number of actions 3 | 4 | ## Explanation about The algorithm that I use : 5 | algorithm push_swap 6 | ## To Test 7 | Try to use : ./push_swap -h "this command will show what to do " 8 | ## Concept of this Project ## 9 | 10 | The idea is simple, You have two stacks called Stack A and Stack B.
11 | Stack A is given a random list of unorganized numbers.
12 | You must take the random list of numbers in Stack A and sort them so that Stack A is organized from smallest to largest.
13 | There are only a few moves you’re allowed to used to manipulate the stacks that we’re going to call “Actions”.
14 | The main goal of this project is to organize Stack A in as few actions as possible.
15 | 16 | ## MOVES ## 17 | 18 | the moves are named: sa, sb, ss, ra, rb, rr, rra, rrb, rrr, pa, pb.
19 | I use Double linked list to implement these moves :
20 | 21 | ## sa || sb :arrows_clockwise: ## 22 | -swap 2 first element 23 | Screen-Shot-2021-04-03-at-12-44-32-PM 24 | ## :arrow_heading_up: ra || rb :arrow_right_hook: ## 25 | -put the first element last,and all elemnt go UP by one 26 | Screen-Shot-2021-04-03-at-2-51-42-PM 27 | 28 | ## :arrow_heading_down: rra || rrb :arrows_counterclockwise: ## 29 | -put the last element First, so all elements go DOWN by ine element 30 | Screen-Shot-2021-04-03-at-5-26-13-PM 31 | 32 | ## pa || pb :arrow_right: ## 33 | -take the first element at the top of a stack and put it at the top of other_stack. 34 | Screen-Shot-2021-04-03-at-6-41-55-PM 35 | Screen-Shot-2021-04-04-at-3-27-57-PM 36 | 37 | ## Checker: 38 | -Thanks to the checker program, you will be able to check if the 39 | list of instructions you’ll generate with the program push_swap is 40 | actually sorting the stack properly. 41 | 42 | ## This is how much instructions the algorithm Do with 100 random number 43 | Screen-Shot-2021-04-11-at-2-46-15-PM 44 | 45 | Screen-Shot-2021-04-11-at-2-33-56-PM 46 | 47 | 48 | -------------------------------------------------------------------------------- /code/srcs/shared/algo_d.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* algo_d.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zdnaya +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/15 23:42:09 by zainabdnaya #+# #+# */ 9 | /* Updated: 2021/04/20 15:27:38 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void part_1_d(t_stack **a, t_stack **b, int len) 16 | { 17 | int size; 18 | int m; 19 | t_stack *tmp; 20 | t_stack *tmps; 21 | 22 | size = len; 23 | while (size >= val_aprox(len / 8)) 24 | { 25 | tmps = dup_list(*a); 26 | m = get_pivot(tmps); 27 | while (check_under_pivot((*a), m) && (*a)) 28 | { 29 | if ((*a)->number <= m) 30 | { 31 | tmp = *a; 32 | s_c_display(a, b, 1); 33 | size--; 34 | free(tmp); 35 | } 36 | else 37 | norm_part1_c(a, b, m, 0); 38 | } 39 | free_stack(&tmps); 40 | } 41 | } 42 | 43 | void part_2_d(t_stack **a, t_stack **b, int size) 44 | { 45 | int min; 46 | t_stack *tmp; 47 | 48 | tmp = NULL; 49 | while (*a && !check_sort(a, size)) 50 | { 51 | min = get_min(*a); 52 | tmp = (*a)->next; 53 | while ((*a)->number != min && tmp && tmp->number == min) 54 | s_c_display(a, b, 8); 55 | while (size_list(*a) && check_under_pivot((*a), min)) 56 | { 57 | if ((*a)->number == min) 58 | { 59 | tmp = *a; 60 | s_c_display(a, b, 1); 61 | free(tmp); 62 | size--; 63 | } 64 | else 65 | norm_part1_c(a, b, min, 0); 66 | } 67 | } 68 | } 69 | 70 | void part_3_d(t_stack **a, t_stack **b, int max) 71 | { 72 | t_stack *tmp; 73 | 74 | tmp = NULL; 75 | while (*b) 76 | { 77 | max = get_max(*b); 78 | while (check_upper_pivot((*b), max)) 79 | { 80 | max = get_max(*b); 81 | tmp = (*b)->next; 82 | while ((*b)->number != max && tmp && tmp->number == max) 83 | s_c_display(a, b, 7); 84 | while (((*b)->number != max) && *b) 85 | norm_part3_c(a, b, max, 0); 86 | while (*b && (*b)->number == max) 87 | { 88 | tmp = *b; 89 | s_c_display(a, b, 4); 90 | max = get_max(*b); 91 | free(tmp); 92 | } 93 | } 94 | } 95 | } 96 | 97 | void algo_d(t_stack **a, t_stack **b, int len) 98 | { 99 | int m; 100 | 101 | m = 0; 102 | part_3_d(a, b, len); 103 | part_2_d(a, b, len); 104 | part_3_d(a, b, m); 105 | free_stack(a); 106 | free_stack(b); 107 | } 108 | -------------------------------------------------------------------------------- /code/srcs/shared/algo_disp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* algo_disp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zdnaya +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/15 22:01:00 by zainabdnaya #+# #+# */ 9 | /* Updated: 2021/04/20 15:25:07 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void part1_1_d(t_stack **a, t_stack **b, int len) 16 | { 17 | int size; 18 | int m; 19 | t_stack *tmp; 20 | t_stack *tmps; 21 | 22 | size = len; 23 | while (size >= val_aprox(len / 4)) 24 | { 25 | tmps = dup_list(*a); 26 | m = get_pivot(tmps); 27 | while (check_under_pivot((*a), m) && (*a)) 28 | { 29 | if ((*a)->number <= m) 30 | { 31 | tmp = *a; 32 | s_c_display(a, b, 1); 33 | size--; 34 | free(tmp); 35 | } 36 | else 37 | norm_part1_c(a, b, m, 0); 38 | } 39 | free_stack(&tmps); 40 | } 41 | } 42 | 43 | void part1_2_d(t_stack **a, t_stack **b, int size) 44 | { 45 | int min; 46 | t_stack *tmp; 47 | 48 | tmp = NULL; 49 | while (*a && !check_sort(a, size)) 50 | { 51 | min = get_min(*a); 52 | tmp = (*a)->next; 53 | while ((*a)->number != min && tmp && tmp->number == min) 54 | s_c_display(a, b, 8); 55 | while (size_list(*a) && check_under_pivot((*a), min)) 56 | { 57 | if ((*a)->number == min) 58 | { 59 | tmp = *a; 60 | s_c_display(a, b, 1); 61 | free(tmp); 62 | size--; 63 | } 64 | else 65 | norm_part1_c(a, b, min, 0); 66 | } 67 | } 68 | } 69 | 70 | void part1_3_d(t_stack **a, t_stack **b, int max) 71 | { 72 | t_stack *tmp; 73 | 74 | tmp = NULL; 75 | while (*b) 76 | { 77 | max = get_max(*b); 78 | while (check_upper_pivot((*b), max)) 79 | { 80 | max = get_max(*b); 81 | tmp = (*b)->next; 82 | while ((*b)->number != max && tmp && tmp->number == max) 83 | s_c_display(a, b, 7); 84 | while (((*b)->number != max) && *b) 85 | norm_part3_c(a, b, max, 0); 86 | while (*b && (*b)->number == max) 87 | { 88 | tmp = *b; 89 | s_c_display(a, b, 4); 90 | max = get_max(*b); 91 | free(tmp); 92 | } 93 | } 94 | } 95 | } 96 | 97 | void algo_1_d(t_stack **a, t_stack **b, int len) 98 | { 99 | int m; 100 | 101 | m = 0; 102 | part1_3_d(a, b, len); 103 | part1_2_d(a, b, len); 104 | part1_3_d(a, b, m); 105 | free_stack(a); 106 | free_stack(b); 107 | } 108 | -------------------------------------------------------------------------------- /code/srcs/shared/swap_disp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* swap_disp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zainabdnayagmail.com 1) 18 | { 19 | ft_putendl_fd(MAG, 1); 20 | r_stack(b); 21 | free(tmp); 22 | tmp = NULL; 23 | } 24 | else if (w == 6 && size_list(*b) > 1) 25 | { 26 | ft_putendl_fd(CYN, 1); 27 | rr_stack(b, tmp); 28 | free(tmp); 29 | tmp = NULL; 30 | } 31 | else if (w == 7 && size_list(*b) > 1) 32 | { 33 | ft_putendl_fd(BHBLK, 1); 34 | swap_stack(b); 35 | } 36 | } 37 | 38 | void switch_norm(t_stack **a, t_stack **b, int w, t_stack *tmp) 39 | { 40 | if (w == 4 && size_list(*b) > 0) 41 | { 42 | ft_putendl_fd(BLU, 1); 43 | push_stack(b, a, tmp); 44 | free(tmp); 45 | tmp = NULL; 46 | } 47 | else if (w == 3 && size_list(*a) > 1) 48 | { 49 | ft_putendl_fd(YEL, 1); 50 | rr_stack(a, tmp); 51 | } 52 | else if (w == 8 && size_list(*a) > 1) 53 | { 54 | ft_putendl_fd(RED, 1); 55 | swap_stack(a); 56 | } 57 | else 58 | switch_norm1(b, w, tmp); 59 | } 60 | 61 | void switch_case_color(t_stack **a, t_stack **b, int w) 62 | { 63 | t_stack *tmp; 64 | 65 | tmp = NULL; 66 | if (w == 1 && size_list(*a) > 0) 67 | { 68 | ft_putendl_fd(WHT, 1); 69 | push_stack(a, b, tmp); 70 | } 71 | else if (w == 2) 72 | { 73 | ft_putendl_fd(GRN, 1); 74 | r_stack(a); 75 | } 76 | else 77 | switch_norm(a, b, w, tmp); 78 | free_stack(&tmp); 79 | } 80 | 81 | void s_c_display(t_stack **a, t_stack **b, int w) 82 | { 83 | t_stack *tmp; 84 | 85 | tmp = NULL; 86 | if (w == 1) 87 | push_stack(a, b, tmp); 88 | else if (w == 2 && size_list(*a) > 1) 89 | r_stack(a); 90 | else if (w == 3 && size_list(*a) > 1) 91 | rr_stack(a, tmp); 92 | else if (w == 4) 93 | push_stack(b, a, tmp); 94 | else if (w == 5 && size_list(*b) > 1) 95 | r_stack(b); 96 | else if (w == 6 && size_list(*b) > 1) 97 | rr_stack(b, tmp); 98 | else if (w == 7 && size_list(*b) > 1) 99 | swap_stack(b); 100 | else if (w == 8 && size_list(*a) > 1) 101 | swap_stack(a); 102 | free_stack(&tmp); 103 | display(*a, *b); 104 | } 105 | -------------------------------------------------------------------------------- /code/srcs/shared/algo_c.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* algo_c.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zdnaya +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/15 01:01:02 by zainabdnaya #+# #+# */ 9 | /* Updated: 2021/04/20 17:10:29 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void part1_1_c(t_stack **a, t_stack **b, int len) 16 | { 17 | int size; 18 | int m; 19 | t_stack *tmp; 20 | t_stack *tmps; 21 | 22 | size = len; 23 | while (size >= val_aprox(len / 4)) 24 | { 25 | tmps = dup_list(*a); 26 | m = get_pivot(tmps); 27 | while (check_under_pivot((*a), m) && (*a)) 28 | { 29 | if ((*a)->number <= m) 30 | { 31 | tmp = *a; 32 | switch_case_color(a, b, 1); 33 | size--; 34 | free(tmp); 35 | } 36 | else 37 | norm_part1_c(a, b, m, 1); 38 | } 39 | free_stack(&tmps); 40 | } 41 | } 42 | 43 | void part1_2_c(t_stack **a, t_stack **b, int size) 44 | { 45 | int min; 46 | t_stack *tmp; 47 | 48 | tmp = NULL; 49 | while (*a && !check_sort(a, size)) 50 | { 51 | min = get_min(*a); 52 | tmp = (*a)->next; 53 | while ((*a)->number != min && tmp && tmp->number == min) 54 | switch_case_color(a, b, 8); 55 | while (size_list(*a) && check_under_pivot((*a), min)) 56 | { 57 | if ((*a)->number == min) 58 | { 59 | tmp = *a; 60 | switch_case_color(a, b, 1); 61 | free(tmp); 62 | size--; 63 | } 64 | else 65 | norm_part1_c(a, b, min, 1); 66 | } 67 | } 68 | } 69 | 70 | void part1_3_c(t_stack **a, t_stack **b, int max) 71 | { 72 | t_stack *tmp; 73 | 74 | tmp = NULL; 75 | while (*b) 76 | { 77 | max = get_max(*b); 78 | while (check_upper_pivot((*b), max)) 79 | { 80 | max = get_max(*b); 81 | tmp = (*b)->next; 82 | while ((*b)->number != max && tmp && tmp->number == max) 83 | switch_case_color(a, b, 7); 84 | while (((*b)->number != max) && *b) 85 | norm_part3_c(a, b, max, 1); 86 | while (*b && (*b)->number == max) 87 | { 88 | tmp = *b; 89 | switch_case_color(a, b, 4); 90 | max = get_max(*b); 91 | free(tmp); 92 | } 93 | } 94 | } 95 | } 96 | 97 | void algo_1_c(t_stack **a, t_stack **b, int len) 98 | { 99 | int m; 100 | 101 | m = 0; 102 | part1_3_c(a, b, len); 103 | part1_2_c(a, b, len); 104 | part1_3_c(a, b, m); 105 | free_stack(a); 106 | free_stack(b); 107 | } 108 | -------------------------------------------------------------------------------- /code/srcs/shared/algo_500_c.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* algo_500_c.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zdnaya +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/15 01:15:40 by zainabdnaya #+# #+# */ 9 | /* Updated: 2021/04/20 15:23:06 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void part_1_c(t_stack **a, t_stack **b, int len) 16 | { 17 | int size; 18 | int m; 19 | t_stack *tmp; 20 | t_stack *tmps; 21 | 22 | size = len; 23 | while (size >= val_aprox(len / 8)) 24 | { 25 | tmps = dup_list(*a); 26 | m = get_pivot(tmps); 27 | while (check_under_pivot((*a), m) && (*a)) 28 | { 29 | if ((*a)->number <= m) 30 | { 31 | tmp = *a; 32 | switch_case_color(a, b, 1); 33 | size--; 34 | free(tmp); 35 | } 36 | else 37 | norm_part1_c(a, b, m, 1); 38 | } 39 | free_stack(&tmps); 40 | } 41 | } 42 | 43 | void part_2_c(t_stack **a, t_stack **b, int size) 44 | { 45 | t_stack *tmp; 46 | t_stack *tmps; 47 | int min; 48 | 49 | while (*a && !check_sort(a, size)) 50 | { 51 | min = get_min(*a); 52 | tmp = (*a)->next; 53 | while ((*a)->number != min && tmp && tmp->number == min) 54 | switch_case_color(a, b, 8); 55 | while (size_list(*a) && check_under_pivot((*a), min)) 56 | { 57 | if ((*a)->number == min) 58 | { 59 | tmps = *a; 60 | switch_case_color(a, b, 1); 61 | free(tmps); 62 | size--; 63 | } 64 | else 65 | norm_part1_c(a, b, min, 1); 66 | } 67 | } 68 | } 69 | 70 | void part_3_c(t_stack **a, t_stack **b, int max) 71 | { 72 | t_stack *tmp; 73 | t_stack *tmps; 74 | 75 | while (*b) 76 | { 77 | max = get_max(*b); 78 | while (check_upper_pivot((*b), max)) 79 | { 80 | max = get_max(*b); 81 | tmp = (*b)->next; 82 | while ((*b)->number != max && tmp && tmp->number == max) 83 | switch_case_color(a, b, 7); 84 | while (((*b)->number != max) && *b) 85 | norm_part3_c(a, b, max, 1); 86 | while (*b && (*b)->number == max) 87 | { 88 | tmps = *b; 89 | switch_case_color(a, b, 4); 90 | max = get_max(*b); 91 | free(tmps); 92 | } 93 | } 94 | } 95 | } 96 | 97 | void algo_c(t_stack **a, t_stack **b, int len) 98 | { 99 | int m; 100 | 101 | m = 0; 102 | part_1_c(a, b, len); 103 | part_2_c(a, b, len); 104 | part_3_c(a, b, m); 105 | free_stack(a); 106 | free_stack(b); 107 | } 108 | -------------------------------------------------------------------------------- /code/srcs/shared/option_v.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* option_v.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zainabdnayagmail.com split = ft_split(av[2], ' '); 19 | else 20 | all->split = &av[2]; 21 | all = fill_in(all); 22 | while (1) 23 | { 24 | all->line = ft_calloc(BUFFER_SIZE, sizeof(char)); 25 | read(0, all->line, BUFFER_SIZE); 26 | if (all->line[0] == '\0' || all->line[0] == '\n') 27 | { 28 | checker_sort(all); 29 | exit(1); 30 | } 31 | else 32 | checker_pars_v(&all->a, &all->b, all->line); 33 | system("clear"); 34 | print_all(all->a, all->b); 35 | free_arg(&(all->line)); 36 | } 37 | if (all->line[0] == '\0' || all->line[0] == '\n') 38 | { 39 | checker_sort(all); 40 | exit(1); 41 | } 42 | } 43 | 44 | char *ft_strdup(const char *src) 45 | { 46 | char *dup; 47 | int i; 48 | 49 | i = 0; 50 | while (src[i]) 51 | i++; 52 | dup = (char *)malloc(sizeof(char) * i + 1); 53 | if (dup == NULL) 54 | return (NULL); 55 | i = 0; 56 | while (src[i]) 57 | { 58 | dup[i] = src[i]; 59 | i++; 60 | } 61 | dup[i] = '\0'; 62 | return (dup); 63 | } 64 | 65 | void *ft_calloc(size_t count, size_t size) 66 | { 67 | int i; 68 | char *ptr; 69 | 70 | i = 0; 71 | ptr = (void *)malloc(count * size); 72 | if (ptr == NULL) 73 | return (NULL); 74 | i = (size * count) - 1; 75 | while (i >= 0) 76 | { 77 | ptr[i] = '\0'; 78 | i--; 79 | } 80 | return (ptr); 81 | } 82 | 83 | int ft_strlen(char *s1) 84 | { 85 | int k; 86 | 87 | k = 0; 88 | while (s1[k]) 89 | k++; 90 | return (k); 91 | } 92 | 93 | char *ft_strjoin(char const *s1, char const *s2) 94 | { 95 | char *s3; 96 | char *str1; 97 | char *str2; 98 | unsigned int len; 99 | 100 | if (!s1 || !s2) 101 | { 102 | return (NULL); 103 | } 104 | str1 = (char *)s1; 105 | str2 = (char *)s2; 106 | len = 0; 107 | s3 = (char *)malloc(ft_strlen(str1) + ft_strlen(str2) + 1); 108 | if (s3 == NULL) 109 | return (NULL); 110 | while (*str1) 111 | s3[len++] = *(str1++); 112 | while (*str2) 113 | s3[len++] = *(str2++); 114 | s3[len] = '\0'; 115 | return (s3); 116 | } 117 | -------------------------------------------------------------------------------- /code/srcs/shared/algo.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* algo.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zdnaya +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/08 13:06:00 by zdnaya #+# #+# */ 9 | /* Updated: 2021/04/20 14:19:34 by zdnaya ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void part1_1(t_stack **a, t_stack **b, int len, t_all *all) 16 | { 17 | int size; 18 | int m; 19 | t_stack *tmp; 20 | t_stack *tmps; 21 | 22 | size = len; 23 | while (size >= val_aprox(len / 4)) 24 | { 25 | tmps = dup_list(*a); 26 | m = get_pivot(tmps); 27 | while (check_under_pivot((*a), m) && (*a)) 28 | { 29 | if ((*a)->number <= m) 30 | { 31 | tmp = *a; 32 | switch_case(a, b, 1, all); 33 | size--; 34 | free(tmp); 35 | } 36 | else 37 | norm_part1(a, b, all, m); 38 | } 39 | free_stack(&tmps); 40 | } 41 | } 42 | 43 | void part1_2(t_stack **a, t_stack **b, t_all *all, int size) 44 | { 45 | int min; 46 | t_stack *tmp; 47 | 48 | tmp = NULL; 49 | while (*a && !check_sort(a, size)) 50 | { 51 | min = get_min(*a); 52 | tmp = (*a)->next; 53 | while ((*a)->number != min && tmp && tmp->number == min) 54 | switch_case(a, b, 8, all); 55 | while (size_list(*a) && check_under_pivot((*a), min)) 56 | { 57 | if ((*a)->number == min) 58 | { 59 | tmp = *a; 60 | switch_case(a, b, 1, all); 61 | free(tmp); 62 | size--; 63 | } 64 | else 65 | norm_part1(a, b, all, min); 66 | } 67 | } 68 | } 69 | 70 | void part1_3(t_stack **a, t_stack **b, t_all *all, int max) 71 | { 72 | t_stack *tmp; 73 | 74 | tmp = NULL; 75 | while (*b) 76 | { 77 | max = get_max(*b); 78 | while (check_upper_pivot((*b), max)) 79 | { 80 | max = get_max(*b); 81 | tmp = (*b)->next; 82 | while ((*b)->number != max && tmp && tmp->number == max) 83 | switch_case(a, b, 7, all); 84 | while (((*b)->number != max) && *b) 85 | norm_part3(a, b, all, max); 86 | while (*b && (*b)->number == max) 87 | { 88 | tmp = *b; 89 | switch_case(a, b, 4, all); 90 | max = get_max(*b); 91 | free(tmp); 92 | } 93 | } 94 | } 95 | } 96 | 97 | void algo_1(t_stack **a, t_stack **b, int len, t_all *all) 98 | { 99 | int m; 100 | 101 | m = 0; 102 | part1_1(a, b, len, all); 103 | part1_2(a, b, all, len); 104 | part1_3(a, b, all, m); 105 | free_stack(a); 106 | free_stack(b); 107 | } 108 | -------------------------------------------------------------------------------- /code/srcs/shared/algo_500.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* algo_500.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zainabdnayagmail.com = val_aprox(len / 8)) 24 | { 25 | tmps = dup_list(*a); 26 | m = get_pivot(tmps); 27 | while (check_under_pivot((*a), m) && (*a)) 28 | { 29 | if ((*a)->number <= m) 30 | { 31 | tmp = *a; 32 | switch_case(a, b, 1, all); 33 | size--; 34 | free(tmp); 35 | } 36 | else 37 | norm_part1(a, b, all, m); 38 | } 39 | free_stack(&tmps); 40 | } 41 | } 42 | 43 | void part_2(t_stack **a, t_stack **b, t_all *all, int size) 44 | { 45 | t_stack *tmp; 46 | t_stack *tmps; 47 | int min; 48 | 49 | while (*a && !check_sort(a, size)) 50 | { 51 | min = get_min(*a); 52 | tmp = (*a)->next; 53 | while ((*a)->number != min && tmp && tmp->number == min) 54 | switch_case(a, b, 8, all); 55 | while (size_list(*a) && check_under_pivot((*a), min)) 56 | { 57 | if ((*a)->number == min) 58 | { 59 | tmps = *a; 60 | switch_case(a, b, 1, all); 61 | free(tmps); 62 | size--; 63 | } 64 | else 65 | norm_part1(a, b, all, min); 66 | } 67 | } 68 | } 69 | 70 | void part_3(t_stack **a, t_stack **b, t_all *all, int max) 71 | { 72 | t_stack *tmp; 73 | t_stack *tmps; 74 | 75 | while (*b) 76 | { 77 | max = get_max(*b); 78 | while (check_upper_pivot((*b), max)) 79 | { 80 | max = get_max(*b); 81 | tmp = (*b)->next; 82 | while ((*b)->number != max && tmp && tmp->number == max) 83 | switch_case(a, b, 7, all); 84 | while (((*b)->number != max) && *b) 85 | norm_part3(a, b, all, max); 86 | while (*b && (*b)->number == max) 87 | { 88 | tmps = *b; 89 | switch_case(a, b, 4, all); 90 | max = get_max(*b); 91 | free(tmps); 92 | } 93 | } 94 | } 95 | } 96 | 97 | void algo(t_stack **a, t_stack **b, int len, t_all *all) 98 | { 99 | int m; 100 | 101 | m = 0; 102 | part_1(a, b, len, all); 103 | part_2(a, b, all, len); 104 | part_3(a, b, all, m); 105 | free_stack(a); 106 | free_stack(b); 107 | } 108 | -------------------------------------------------------------------------------- /code/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: zdnaya +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2021/04/02 08:58:20 by zainabdnaya #+# #+# # 9 | # Updated: 2021/04/21 17:34:34 by zdnaya ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME1= checker 14 | NAME2= push_swap 15 | SRC_PATH= srcs 16 | HDR_PATH= includes 17 | OBJ_PATH= obj 18 | SHARED_PATH= $(OBJ_PATH)/shared 19 | 20 | 21 | SRC_SHARED= shared/errors.c\ 22 | shared/simple_free.c\ 23 | shared/use_libft1.c\ 24 | shared/use_libft.c\ 25 | shared/list_op.c\ 26 | shared/to_use.c\ 27 | shared/initial.c\ 28 | shared/gen_sort.c\ 29 | shared/list.c\ 30 | shared/moves.c\ 31 | shared/ok_ko.c\ 32 | shared/algo_d.c\ 33 | shared/algo_disp.c\ 34 | shared/algo_min_d.c\ 35 | shared/print.c\ 36 | shared/swap_disp.c\ 37 | shared/algo_500_c.c\ 38 | shared/algo_min_c.c\ 39 | shared/algo_c.c\ 40 | shared/orderby.c\ 41 | shared/checker_rd.c\ 42 | shared/algo.c\ 43 | shared/algo_use.c\ 44 | shared/algo_500.c\ 45 | shared/algo_min.c\ 46 | shared/swap_case.c\ 47 | shared/swap_op.c\ 48 | shared/option_v.c\ 49 | shared/algo_norm.c\ 50 | shared/moves_nor.c\ 51 | 52 | SRC_NAME1= checker.c $(SRC_SHARED) 53 | SRC_NAME2= swap.c $(SRC_SHARED) 54 | HDR_NAME= push_swap.h 55 | 56 | OBJ_NAME1= $(SRC_NAME1:.c=.o) 57 | OBJ1= $(addprefix $(OBJ_PATH)/,$(OBJ_NAME1)) 58 | SRC1= $(addprefix $(SRC_PATH)/,$(SRC_NAME1)) 59 | 60 | OBJ_NAME2= $(SRC_NAME2:.c=.o) 61 | OBJ2= $(addprefix $(OBJ_PATH)/,$(OBJ_NAME2)) 62 | SRC2= $(addprefix $(SRC_PATH)/,$(SRC_NAME2)) 63 | 64 | HDR= $(addprefix $(HDR_PATH)/,$(HDR_NAME)) 65 | FLAGS= -Wall -Wextra -Werror 66 | H_FLAG= -I $(HDR_PATH) 67 | 68 | COMP= gcc -g 69 | 70 | all: $(NAME1) $(NAME2) 71 | 72 | $(NAME1) : $(OBJ1) 73 | @rm -rf $(NAME1) 74 | @$(COMP) $(H_FLAG) $(OBJ1) -o $@ 75 | @echo " Compilation of $(NAME1): \033[1;32mOK\033[m" 76 | 77 | $(NAME2) : $(OBJ2) 78 | @rm -rf $(NAME2) 79 | @$(COMP) $(H_FLAG) $(OBJ2) -o $@ 80 | @echo " Compilation of $(NAME2): \033[1;32mOK\033[m" 81 | 82 | $(OBJ_PATH)/%.o: $(SRC_PATH)/%.c $(HDR) 83 | @mkdir -p $(OBJ_PATH) $(SHARED_PATH) 84 | @$(COMP) $(FLAGS) $(H_FLAG) -o $@ -c $< 85 | 86 | clean: 87 | @rm -rf $(OBJ_PATH) 88 | @echo "\033[1;33m>> all .o files are deleted.\033[0m" 89 | 90 | fclean: clean 91 | @rm -rf $(NAME1) 92 | @echo "\033[0;31m>> $(NAME1)all obbjects are deleted.\033[0m" 93 | @rm -rf $(NAME2) 94 | @echo "\033[0;31m>> $(NAME2) all obbjects are deleted.\033[0m" 95 | 96 | re : fclean all -------------------------------------------------------------------------------- /code/srcs/checker.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* checker.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zainabdnayagmail.com line = malloc(sizeof(char) * BUFFER_SIZE); 21 | ft_bzero(all->line, BUFFER_SIZE); 22 | while (read(0, all->line, BUFFER_SIZE)) 23 | { 24 | tmp = str; 25 | if (str == NULL) 26 | str = ft_strdup(all->line); 27 | else 28 | str = ft_strjoin(str, all->line); 29 | if (all->ac >= 2 && !ft_strchr(all->av[1], ' ')) 30 | condition_(all->line); 31 | if (tmp) 32 | { 33 | free(tmp); 34 | tmp = NULL; 35 | } 36 | free_arg(&all->line); 37 | all->line = malloc(sizeof(char) * BUFFER_SIZE); 38 | ft_bzero(all->line, BUFFER_SIZE); 39 | } 40 | return (str); 41 | } 42 | 43 | char **checker_norm(t_all *all, char **line, char *tmp) 44 | { 45 | char *l; 46 | 47 | l = string(all, tmp); 48 | if (l) 49 | { 50 | line = ft_split(l, '\n'); 51 | free_arg(&l); 52 | } 53 | else 54 | checker_sort(all); 55 | return (line); 56 | } 57 | 58 | void the_end(t_all *all, char **line, int i) 59 | { 60 | while (line[i]) 61 | { 62 | if (all->ac <= 2) 63 | { 64 | checker_pars(&all->a, &all->b, line[i]); 65 | i++; 66 | } 67 | else 68 | { 69 | if (condition(line[i]) == 1) 70 | { 71 | checker_pars(&all->a, &all->b, line[i]); 72 | i++; 73 | } 74 | else 75 | { 76 | ft_free_split(line); 77 | free_stack(&all->a); 78 | free_stack(&all->b); 79 | free(all); 80 | ft_putstr_fd("Error\n", 2); 81 | exit(1); 82 | } 83 | } 84 | } 85 | } 86 | 87 | void checker(t_all *all, char **line) 88 | { 89 | char *tmp; 90 | int i; 91 | 92 | i = 0; 93 | tmp = NULL; 94 | line = checker_norm(all, line, tmp); 95 | the_end(all, line, i); 96 | ft_free_split(line); 97 | checker_sort(all); 98 | } 99 | 100 | int main(int ac, char **av) 101 | { 102 | t_all *all; 103 | char **line; 104 | 105 | line = NULL; 106 | all = NULL; 107 | if (ac < 2) 108 | exit(1); 109 | else 110 | { 111 | all = initial(all); 112 | all->ac = ac; 113 | all->av = av; 114 | if (!ft_strcmp(av[1], "-v")) 115 | option_v(all, ac, av); 116 | else 117 | { 118 | all->split = normal(all, ac, av); 119 | all = fill_in(all); 120 | checker(all, line); 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /code/srcs/swap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* swap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zainabdnayagmail.com fd = open("swap.log", O_CREAT | O_RDWR | O_TRUNC, 0666); 20 | if (all->fd == -1) 21 | ft_putstr_fd("Error! opening file", 1); 22 | if (ac == 3) 23 | all->split = ft_split(av[2], ' '); 24 | else 25 | all->split = &av[2]; 26 | } 27 | else 28 | { 29 | all->fd = 1; 30 | if (ac == 2) 31 | all->split = ft_split(av[1], ' '); 32 | else 33 | all->split = &av[1]; 34 | } 35 | return (all->split); 36 | } 37 | 38 | void swap_(t_all *all, int ac, char **av) 39 | { 40 | t_stack *new; 41 | 42 | new = NULL; 43 | all->split = fill_ps(all, ac, av); 44 | check_replicat(all->split); 45 | check_ascii(all->split); 46 | all->a = put_in_list(all, all->split, new); 47 | if (check_sort(&all->a, size_list(all->a)) == 1) 48 | exit(0); 49 | all->len = size_list(all->a); 50 | free_stack(&new); 51 | if (all->len <= 10) 52 | sort_min(&(all->a), &(all->b), all->len, all); 53 | else if (all->len > 10 && all->len < 200) 54 | algo_1(&(all->a), &(all->b), all->len, all); 55 | else 56 | algo(&(all->a), &(all->b), all->len, all); 57 | if (ac == 2 || (ac == 3 && all->fd != 1)) 58 | ft_free_split(all->split); 59 | close(all->fd); 60 | } 61 | 62 | void text(void) 63 | { 64 | char *str; 65 | 66 | str = "Run this command it should give OK \xE2\x9C\x85 if no Errors exist\n"; 67 | ft_putstr_fd("\t\033[94m\xE2\x9C\xA8Welcome to PUSH_SWAP\xE2\x9C\xA8 \033[94m\t\n", 1); 68 | ft_putstr_fd("\033[36mTo run Push_swap\xE2\x9D\x97:\n", 1); 69 | ft_putstr_fd("\033[35m ./push_swap list_of_numbers_given_as_parameters\n", 1); 70 | ft_putstr_fd("\033[92m exemple (./push_swap 4 3 7)\n", 1); 71 | ft_putstr_fd("\033[36mTo run Checker\xE2\x9D\x97:\n", 1); 72 | ft_putstr_fd("\033[35m ./checker number_list\n", 1); 73 | ft_putstr_fd("\033[92m exemple (./checker 4 3 7)\n", 1); 74 | ft_putstr_fd("\033[94mTo Check if push_swap work well\xF0\x9F\x91\x8C:\n", 1); 75 | ft_putstr_fd("\033[94m\xF0\x9F\x8F\x83", 1); 76 | ft_putstr_fd(str, 1); 77 | ft_putstr_fd("\033[35m ./push_swap number_list | ./checker number_list\n", 1); 78 | ft_putstr_fd("\033[92m exemple (./push_swap 4 3 7 | ./checker 4 3 7 )\n", 1); 79 | ft_putstr_fd("\t\t\033[30m\033[41m Output: OK!\n", 1); 80 | exit(0); 81 | } 82 | 83 | int main(int ac, char **av) 84 | { 85 | t_all *all; 86 | 87 | all = NULL; 88 | if (ac < 2) 89 | exit(1); 90 | else 91 | { 92 | all = initial(all); 93 | if (!ft_strcmp(av[1], "-h")) 94 | text(); 95 | if (!ft_strcmp(av[1], "-c")) 96 | swap_c(all, ac, av); 97 | else if (!ft_strcmp(av[1], "-sh")) 98 | swap_sh(all, ac, av); 99 | else 100 | swap_(all, ac, av); 101 | free(all); 102 | all = NULL; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /code/includes/push_swap.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* push_swap.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zainabdnayagmail.com 16 | # include 17 | # include 18 | # include 19 | # define BUFFER_SIZE 100000 20 | # define RED "\e[0;31msa" 21 | # define GRN "\e[0;32mra" 22 | # define YEL "\e[0;33mrra" 23 | # define BLU "\e[0;34mpa" 24 | # define MAG "\e[0;35mrb" 25 | # define CYN "\e[0;36mrrb" 26 | # define WHT "\e[0;37mpb" 27 | # define BHBLK "\e[1;90msb" 28 | # define YELHB "\e[0;103mss" 29 | # define CYNHB "\e[0;106mrr" 30 | # define GRNHB "\e[0;102mrrr" 31 | 32 | typedef struct s_stack 33 | { 34 | int number; 35 | struct s_stack *next; 36 | struct s_stack *previous; 37 | } t_stack; 38 | 39 | typedef struct s_all 40 | { 41 | char *line; 42 | char **split; 43 | char **av; 44 | int len; 45 | int print; 46 | int ac; 47 | int color; 48 | int fd; 49 | t_stack *a; 50 | t_stack *b; 51 | } t_all; 52 | void add_back(t_stack **head, t_stack *new); 53 | void add_back1(t_stack **head); 54 | void add_front(t_stack **head, t_stack *new_node); 55 | void checker_sort(t_all *all); 56 | void check_replicat(char **av); 57 | void swap_stack(t_stack **head); 58 | void r_stack(t_stack **head); 59 | void rr_stack(t_stack **head, t_stack *tmp); 60 | void push_stack(t_stack **a, t_stack **b, t_stack *new); 61 | void ss(t_stack **a, t_stack **b); 62 | void rrr(t_stack **a, t_stack **b); 63 | void rr(t_stack **a, t_stack **b); 64 | void sort_result(t_stack *a, int len); 65 | void algo(t_stack **a, t_stack **b, int len, t_all *all); 66 | void switch_case(t_stack **a, t_stack **b, int w, t_all *all); 67 | void algo_1(t_stack **a, t_stack **b, int len, t_all *all); 68 | void algo_min(t_stack **a, t_stack **b, int len, int k); 69 | void checker_pars(t_stack **a, t_stack **b, char *line); 70 | void sort_a_3(t_stack **a, t_stack **b, int len, t_all *all); 71 | void sort_min(t_stack **a, t_stack **b, int len, t_all *all); 72 | void le_vide_vider(t_stack **a, t_stack **b); 73 | void print_all(t_stack *a, t_stack *b); 74 | void print_list(t_stack *ptr); 75 | void switch_case_color(t_stack **a, t_stack **b, int w); 76 | void sort_a_3_c(t_stack **a, t_stack **b, int len); 77 | void sort_min_c(t_stack **a, t_stack **b, int len); 78 | void algo_1_c(t_stack **a, t_stack **b, int len); 79 | void algo_c(t_stack **a, t_stack **b, int len); 80 | void s_c_display(t_stack **a, t_stack **b, int w); 81 | void algo_1_d(t_stack **a, t_stack **b, int len); 82 | void sort_min_d(t_stack **a, t_stack **b, int len); 83 | void algo_d(t_stack **a, t_stack **b, int len); 84 | void swap_(t_all *all, int ac, char **av); 85 | void swap_c(t_all *all, int ac, char **av); 86 | void swap_sh(t_all *all, int ac, char **av); 87 | void ft_putnbr_fd(int n, int fd); 88 | void ft_putstr_fd(char *s, int fd); 89 | void ft_putchar_fd(char c, int fd); 90 | void ft_putendl_fd(char *s, int fd); 91 | void *ft_calloc(size_t count, size_t size); 92 | void checker_pars_v(t_stack **a, t_stack **b, char *line); 93 | void display(t_stack *a, t_stack *b); 94 | void option_v(t_all *all, int ac, char **av); 95 | void free_arg(char **arg); 96 | void free_stack(t_stack **stack); 97 | void free_single_stack(t_stack **stack); 98 | void ft_free_split(char **split); 99 | void norm_part1(t_stack **a, t_stack **b, t_all *all, int m); 100 | void norm_part3(t_stack **a, t_stack **b, t_all *all, int max); 101 | void norm_part3_c(t_stack **a, t_stack **b, int max, int k); 102 | void norm_part1_c(t_stack **a, t_stack **b, int m, int k); 103 | void ft_bzero(void *s, size_t n); 104 | char **normal(t_all *all, int ac, char **av); 105 | char **ft_split(char const *s, char c); 106 | char *ft_strdup(const char *src); 107 | char *ft_strjoin(char const *s1, char const *s2); 108 | char **fill_ps(t_all *all, int ac, char **av); 109 | char *ft_strchr(const char *s, int c); 110 | int condition(char *line); 111 | int ft_isalpha(int c); 112 | int check_sort(t_stack **a, int len); 113 | int get_pivot(t_stack *a); 114 | int node_nbr(t_stack **head, int index); 115 | int get_index(t_stack *a, int pivot); 116 | int get_index_max(t_stack *a, int pivot); 117 | int get_max(t_stack *b); 118 | int check_ascii(char **av); 119 | int node_nbr(t_stack **head, int index); 120 | int ft_strcmp(const char *s1, const char *s2); 121 | int size_list(t_stack *a); 122 | int val_aprox(float m); 123 | int check_under_pivot(t_stack *a, int pivot); 124 | int check_upper_pivot(t_stack *a, int pivot); 125 | int get_min(t_stack *a); 126 | int condition_(char *line); 127 | t_stack *bottom(t_stack *a); 128 | t_stack *sort(t_stack *head); 129 | t_stack *put_in_list(t_all *all, char **av, t_stack *new); 130 | t_stack *creat_stack(char *av, t_stack *a); 131 | t_stack *dup_list(t_stack *head); 132 | t_all *fill_in(t_all *all); 133 | t_all *initial(t_all *all); 134 | long long my_atoi(const char *str); 135 | 136 | #endif 137 | --------------------------------------------------------------------------------