├── .DS_Store ├── checker.gif ├── push_swap100.gif ├── push_swap5.gif ├── bns ├── rr_bonus.c ├── rrr_bonus.c ├── ss_bonus.c ├── sa_bonus.c ├── sb_bonus.c ├── swap_bonus.c ├── ra_bonus.c ├── rb_bonus.c ├── rra_bonus.c ├── rrb_bonus.c ├── check_args_bonus.c ├── pa_bonus.c ├── pb_bonus.c ├── check_same_nums_bonus.c ├── get_next_line_bonus.c ├── utils_bonus.c ├── checker_bonus.c └── checker_lines_bonus.c ├── src ├── ft_isdigit.c ├── ft_strlen.c ├── swap.c ├── sa.c ├── sb.c ├── ra.c ├── rb.c ├── rra.c ├── rrb.c ├── check_args.c ├── size500.c ├── changenum.c ├── check_order.c ├── ft_free.c ├── rr.c ├── rrr.c ├── size100.c ├── sortnum.c ├── ft_free2.c ├── pa.c ├── pb.c ├── check_num.c ├── check_same_nums.c ├── ft_substr.c ├── size3.c ├── ft_atoi.c ├── ordernums_peq.c ├── size5.c ├── pushswap.c └── ordernums.c ├── inc ├── pushswap_bonus.h └── pushswap.h ├── Makefile ├── README.md └── pyviz.py /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gemartin99/Push_swap/HEAD/.DS_Store -------------------------------------------------------------------------------- /checker.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gemartin99/Push_swap/HEAD/checker.gif -------------------------------------------------------------------------------- /push_swap100.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gemartin99/Push_swap/HEAD/push_swap100.gif -------------------------------------------------------------------------------- /push_swap5.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gemartin99/Push_swap/HEAD/push_swap5.gif -------------------------------------------------------------------------------- /bns/rr_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* rr_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:49:38 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:09:37 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | void rr(t_list *d) 16 | { 17 | ra(d); 18 | rb(d); 19 | } 20 | -------------------------------------------------------------------------------- /bns/rrr_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* rrr_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:57:29 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:10:38 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | void rrr(t_list *d) 16 | { 17 | rra(d); 18 | rrb(d); 19 | } 20 | -------------------------------------------------------------------------------- /bns/ss_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ss_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:08:22 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:10:55 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | void ss(t_list *d) 16 | { 17 | sa(d->sa); 18 | sb(d->sb); 19 | } 20 | -------------------------------------------------------------------------------- /src/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/01/10 18:03:44 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/13 07:36:14 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | int ft_isdigit(int i) 16 | { 17 | if (i < 48 || i > 57) 18 | return (0); 19 | return (1); 20 | } 21 | -------------------------------------------------------------------------------- /bns/sa_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* sa_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:08:22 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:10:55 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | void sa(int *sa) 16 | { 17 | int temp; 18 | 19 | temp = sa[0]; 20 | sa[0] = sa[1]; 21 | sa[1] = temp; 22 | } 23 | -------------------------------------------------------------------------------- /bns/sb_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* sb_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:17:17 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:11:08 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | void sb(int *sb) 16 | { 17 | int temp; 18 | 19 | temp = sb[0]; 20 | sb[0] = sb[1]; 21 | sb[1] = temp; 22 | } 23 | -------------------------------------------------------------------------------- /src/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 01:31:06 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/13 07:40:17 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | int ft_strlen(char *s) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (s[i]) 21 | i++; 22 | return (i); 23 | } 24 | -------------------------------------------------------------------------------- /src/swap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* swap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 01:33:01 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 20:26:36 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void swap(int *argv1, int *argv2) 16 | { 17 | int temp; 18 | 19 | temp = *argv1; 20 | *argv1 = *argv2; 21 | *argv2 = temp; 22 | } 23 | -------------------------------------------------------------------------------- /src/sa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* sa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:08:22 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:10:55 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void sa(int *sa) 16 | { 17 | int temp; 18 | 19 | temp = sa[0]; 20 | sa[0] = sa[1]; 21 | sa[1] = temp; 22 | write(1, "sa\n", 3); 23 | } 24 | -------------------------------------------------------------------------------- /src/sb.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* sb.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:17:17 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:11:08 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void sb(int *sb) 16 | { 17 | int temp; 18 | 19 | temp = sb[0]; 20 | sb[0] = sb[1]; 21 | sb[1] = temp; 22 | write(1, "sb\n", 3); 23 | } 24 | -------------------------------------------------------------------------------- /bns/swap_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* swap_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 01:33:01 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 20:26:36 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | void swap(int *argv1, int *argv2) 16 | { 17 | int temp; 18 | 19 | temp = *argv1; 20 | *argv1 = *argv2; 21 | *argv2 = temp; 22 | } 23 | -------------------------------------------------------------------------------- /src/ra.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ra.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:43:27 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:08:29 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void ra(t_list *d) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (i + 1 < d->sia) 21 | { 22 | swap(&d->sa[i], &d->sa[i + 1]); 23 | i++; 24 | } 25 | write(1, "ra\n", 3); 26 | } 27 | -------------------------------------------------------------------------------- /src/rb.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* rb.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:48:48 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:09:06 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void rb(t_list *d) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (i + 1 < d->sib) 21 | { 22 | swap(&d->sb[i], &d->sb[i + 1]); 23 | i++; 24 | } 25 | write(1, "rb\n", 3); 26 | } 27 | -------------------------------------------------------------------------------- /src/rra.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* rra.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:52:44 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:10:05 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void rra(t_list *d) 16 | { 17 | int i; 18 | 19 | i = d->sia - 1; 20 | while (i > 0) 21 | { 22 | swap(&d->sa[i], &d->sa[i - 1]); 23 | i--; 24 | } 25 | write(1, "rra\n", 4); 26 | } 27 | -------------------------------------------------------------------------------- /bns/ra_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ra_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:43:27 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:08:29 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | void ra(t_list *d) 16 | { 17 | int temp; 18 | int i; 19 | 20 | temp = d->sa[0]; 21 | i = -1; 22 | while (++i < (d->sia - 1)) 23 | d->sa[i] = d->sa[i + 1]; 24 | d->sa[d->sia - 1] = temp; 25 | } 26 | -------------------------------------------------------------------------------- /bns/rb_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* rb_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:48:48 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:09:06 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | void rb(t_list *d) 16 | { 17 | int temp; 18 | int i; 19 | 20 | temp = d->sb[0]; 21 | i = -1; 22 | while (++i < (d->sib - 1)) 23 | d->sb[i] = d->sb[i + 1]; 24 | d->sb[d->sib - 1] = temp; 25 | } 26 | -------------------------------------------------------------------------------- /bns/rra_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* rra_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:52:44 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:10:05 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | void rra(t_list *d) 16 | { 17 | int temp; 18 | int i; 19 | 20 | temp = d->sa[d->sia - 1]; 21 | i = d->sia - 1; 22 | while (--i >= 0) 23 | d->sa[i + 1] = d->sa[i]; 24 | d->sa[0] = temp; 25 | } 26 | -------------------------------------------------------------------------------- /bns/rrb_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* rrb_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:55:25 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/11 15:15:00 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | void rrb(t_list *d) 16 | { 17 | int temp; 18 | int i; 19 | 20 | temp = d->sb[d->sib - 1]; 21 | i = d->sib - 1; 22 | while (--i >= 0) 23 | d->sb[i + 1] = d->sb[i]; 24 | d->sb[0] = temp; 25 | } 26 | -------------------------------------------------------------------------------- /src/rrb.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* rrb.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:55:25 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/11 15:15:00 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void rrb(t_list *d) 16 | { 17 | int i; 18 | int j; 19 | 20 | i = d->sib - 1; 21 | j = 0; 22 | while (i - 1 >= 0) 23 | { 24 | swap(&d->sb[i], &d->sb[i - 1]); 25 | i--; 26 | } 27 | write(1, "rrb\n", 4); 28 | } 29 | -------------------------------------------------------------------------------- /bns/check_args_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* check_args_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 01:40:05 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/13 11:37:17 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | int check_args(char **args, int argc) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | while (++i < argc) 21 | { 22 | if (check_num(args[i + 1]) == -1 || *args[i + 1] == '\0') 23 | return (-1); 24 | } 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /src/check_args.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* check_args.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 01:40:05 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/13 11:37:17 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | int check_args(char **args, int argc) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | while (++i < argc) 21 | { 22 | if (check_num(args[i + 1]) == -1 || *args[i + 1] == '\0') 23 | { 24 | return (-1); 25 | } 26 | } 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /src/size500.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* size500.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: marvin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/10 21:42:25 by marvin #+# #+# */ 9 | /* Updated: 2022/08/09 11:26:48 by gemartin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void size500(t_list *d) 16 | { 17 | d->lsp = d->argc -1; 18 | d->control = 0; 19 | d->percentage = 1; 20 | d->cont = 7; 21 | d->size_block = (d->argc / d->cont) / 2; 22 | d->argmax = d->argc; 23 | sortnum(d); 24 | changenum(d); 25 | ordernums(d); 26 | } 27 | -------------------------------------------------------------------------------- /src/changenum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* changenum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 01:40:05 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/13 11:37:17 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void changenum(t_list *d) 16 | { 17 | int j; 18 | int i; 19 | 20 | j = 0; 21 | i = 0; 22 | while (j < d->argc) 23 | { 24 | if (d->sa[j] != d->sp[i]) 25 | i++; 26 | else 27 | { 28 | d->sa[j] = i; 29 | j++; 30 | i = 0; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/check_order.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* check_order.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: marvin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/11 09:38:56 by marvin #+# #+# */ 9 | /* Updated: 2022/04/13 11:37:56 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | int check_order(t_list *d) 16 | { 17 | int i; 18 | int j; 19 | 20 | i = 0; 21 | j = 0; 22 | while (j < d->argc - 1) 23 | { 24 | if (d->sa[j] < d->sa[j + 1]) 25 | i++; 26 | j++; 27 | } 28 | if (i == j) 29 | return (-1); 30 | else 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /src/ft_free.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_free.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: marvin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/10 23:01:26 by marvin #+# #+# */ 9 | /* Updated: 2022/07/08 13:45:55 by gemartin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void ft_free(t_list *d) 16 | { 17 | if (d->sa) 18 | { 19 | free(d->sa); 20 | } 21 | if (d->sb) 22 | { 23 | free(d->sb); 24 | } 25 | if (d->sp) 26 | { 27 | free(d->sp); 28 | } 29 | if (d) 30 | { 31 | free(d); 32 | } 33 | write(2, "Error\n", 6); 34 | exit(0); 35 | } 36 | -------------------------------------------------------------------------------- /src/rr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* rr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:49:38 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:09:37 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void rr(t_list *d) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (i + 1 < d->sia) 21 | { 22 | swap(&d->sa[i], &d->sa[i + 1]); 23 | i++; 24 | } 25 | i = 0; 26 | while (i + 1 < d->sib) 27 | { 28 | swap(&d->sb[i], &d->sb[i + 1]); 29 | i++; 30 | } 31 | write(1, "rr\n", 3); 32 | } 33 | -------------------------------------------------------------------------------- /src/rrr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* rrr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:57:29 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:10:38 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void rrr(t_list *d) 16 | { 17 | int i; 18 | 19 | i = d->sia - 1; 20 | while (i > 0) 21 | { 22 | swap(&d->sa[i], &d->sa[i - 1]); 23 | i--; 24 | } 25 | i = d->sib - 1; 26 | while (i - 1 >= 0) 27 | { 28 | swap(&d->sb[i], &d->sb[i - 1]); 29 | i--; 30 | } 31 | write(1, "rrr\n", 4); 32 | } 33 | -------------------------------------------------------------------------------- /bns/pa_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:25:14 by gemartin #+# #+# */ 9 | /* Updated: 2022/07/08 13:46:30 by gemartin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | void pa(t_list *d) 16 | { 17 | int temp; 18 | int i; 19 | 20 | temp = d->sb[0]; 21 | i = -1; 22 | while (++i < (d->sib - 1)) 23 | d->sb[i] = d->sb[i + 1]; 24 | d->sib = d->sib - 1; 25 | i = d->sia; 26 | d->sia = d->sia + 1; 27 | while (--i >= 0) 28 | d->sa[i + 1] = d->sa[i]; 29 | d->sa[0] = temp; 30 | } 31 | -------------------------------------------------------------------------------- /bns/pb_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pb.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 18:15:54 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/13 17:00:52 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | void pb(t_list *d) 16 | { 17 | int temp; 18 | int i; 19 | 20 | temp = d->sa[0]; 21 | i = -1; 22 | while (++i < (d->sia - 1)) 23 | d->sa[i] = d->sa[i + 1]; 24 | d->sia = d->sia - 1; 25 | i = d->sib; 26 | d->sib = d->sib + 1; 27 | while (--i >= 0) 28 | d->sb[i + 1] = d->sb[i]; 29 | d->sb[0] = temp; 30 | } 31 | -------------------------------------------------------------------------------- /src/size100.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* size100.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/03 01:09:32 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/13 08:21:21 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void size100(t_list *d) 16 | { 17 | d->lsp = d->argc - 1; 18 | d->control = 0; 19 | d->percentage = 1; 20 | d->cont = 4; 21 | d->size_block = (d->argc / d->cont) / 2; 22 | d->argmax = d->argc; 23 | sortnum(d); 24 | changenum(d); 25 | if (d->argc >= 20) 26 | ordernums(d); 27 | else 28 | ordernums_peq(d); 29 | } 30 | -------------------------------------------------------------------------------- /src/sortnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* sortnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 01:33:50 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/12 18:38:41 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void sortnum(t_list *d) 16 | { 17 | int i; 18 | int c; 19 | int j; 20 | 21 | i = 0; 22 | c = 0; 23 | j = d->argc; 24 | while (c < j) 25 | { 26 | while (i < j - 1) 27 | { 28 | if (d->sp[i] > d->sp[i + 1]) 29 | swap(&d->sp[i], &d->sp[i + 1]); 30 | i++; 31 | } 32 | i = 0; 33 | c++; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/ft_free2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_free2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: marvin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/10 23:01:26 by marvin #+# #+# */ 9 | /* Updated: 2022/07/08 13:45:55 by gemartin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void ft_free2(t_list *d) 16 | { 17 | if (d->sa) 18 | { 19 | free(d->sa); 20 | d->sa = NULL; 21 | } 22 | if (d->sb) 23 | { 24 | free(d->sb); 25 | d->sb = NULL; 26 | } 27 | if (d->sp) 28 | { 29 | free(d->sp); 30 | d->sp = NULL; 31 | } 32 | if (d) 33 | { 34 | free(d); 35 | d = NULL; 36 | } 37 | exit(0); 38 | } 39 | -------------------------------------------------------------------------------- /src/pa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:25:14 by gemartin #+# #+# */ 9 | /* Updated: 2022/07/08 13:46:30 by gemartin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void pa(t_list *d) 16 | { 17 | int i; 18 | 19 | i = d->sia; 20 | d->sib = d->sib - 1; 21 | d->sia = d->sia + 1; 22 | while (i > 0) 23 | { 24 | swap(&d->sa[i], &d->sa[i - 1]); 25 | i--; 26 | } 27 | d->sa[0] = d->sb[0]; 28 | i = 0; 29 | while (i <= d->sib) 30 | { 31 | d->sb[i] = d->sb[i + 1]; 32 | i++; 33 | } 34 | write(1, "pa\n", 3); 35 | } 36 | -------------------------------------------------------------------------------- /src/pb.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pb.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 18:15:54 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/13 17:00:52 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void pb(t_list *d) 16 | { 17 | int i; 18 | 19 | i = d->sib; 20 | d->sib = d->sib + 1; 21 | d->sia = d->sia - 1; 22 | while (i > 0) 23 | { 24 | swap(&d->sb[i], &d->sb[i - 1]); 25 | i--; 26 | } 27 | d->sb[0] = d->sa[0]; 28 | i = 0; 29 | while (i <= d->sia - 1) 30 | { 31 | d->sa[i] = d->sa[i + 1]; 32 | i++; 33 | } 34 | write(1, "pb\n", 3); 35 | } 36 | -------------------------------------------------------------------------------- /src/check_num.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* check_num.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: marvin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/13 10:31:33 by marvin #+# #+# */ 9 | /* Updated: 2022/04/13 10:32:38 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | int check_num(char *s) 16 | { 17 | int i; 18 | int j; 19 | 20 | i = 0; 21 | j = 0; 22 | if ( 23 | (s[j] == '-' && (s[j + 1] >= '0' && s[j + 1] <= '9')) 24 | || (s[j] >= '0' && s[j] <= '9')) 25 | { 26 | i++; 27 | j++; 28 | while (s[j]) 29 | { 30 | if (s[j] >= '0' && s[j] <= '9') 31 | i++; 32 | j++; 33 | } 34 | } 35 | if (i == ft_strlen(s)) 36 | return (0); 37 | return (-1); 38 | } 39 | -------------------------------------------------------------------------------- /src/check_same_nums.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* check_same_nums.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: marvin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/11 03:16:39 by marvin #+# #+# */ 9 | /* Updated: 2022/04/13 10:03:58 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | static int ft_check(t_list *d, int num, int j) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (i < j) 21 | { 22 | if (d->sa[i] == num) 23 | return (0); 24 | i++; 25 | } 26 | return (1); 27 | } 28 | 29 | int check_same_nums(t_list *d) 30 | { 31 | int i; 32 | 33 | i = 0; 34 | while (i <= d->sia - 1) 35 | { 36 | if (ft_check(d, d->sa[i], i) == 1) 37 | i++; 38 | else 39 | return (-1); 40 | } 41 | return (0); 42 | } 43 | -------------------------------------------------------------------------------- /bns/check_same_nums_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* check_same_nums_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: marvin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/11 03:16:39 by marvin #+# #+# */ 9 | /* Updated: 2022/04/13 10:03:58 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | static int ft_check(t_list *d, int num, int j) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (i < j) 21 | { 22 | if (d->sa[i] == num) 23 | return (0); 24 | i++; 25 | } 26 | return (1); 27 | } 28 | 29 | int check_same_nums(t_list *d) 30 | { 31 | int i; 32 | 33 | i = 0; 34 | while (i <= d->sia - 1) 35 | { 36 | if (ft_check(d, d->sa[i], i) == 1) 37 | i++; 38 | else 39 | return (-1); 40 | } 41 | return (0); 42 | } 43 | -------------------------------------------------------------------------------- /src/ft_substr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_substr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/01/20 14:45:18 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/13 18:18:34 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | char *ft_substr(char *s, int start, int len) 16 | { 17 | int i; 18 | char *copia; 19 | 20 | i = 0; 21 | if (!s) 22 | return (0); 23 | if (len > ft_strlen(s)) 24 | copia = malloc(sizeof(char) * (ft_strlen(s) + 1)); 25 | else 26 | copia = malloc(sizeof(char) * (len + 1)); 27 | if (!copia) 28 | return (0); 29 | while (start < ft_strlen(s) && i < len && s[start]) 30 | { 31 | copia[i] = s[start]; 32 | i++; 33 | start++; 34 | } 35 | copia[i] = '\0'; 36 | return (copia); 37 | } 38 | -------------------------------------------------------------------------------- /src/size3.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* size3.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/03 01:02:43 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/13 08:23:27 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void size3(int *s, t_list *d) 16 | { 17 | if (d->argc == 2) 18 | { 19 | if (s[0] > s[1]) 20 | sa(s); 21 | } 22 | else if (s[0] < s[1] && s[0] < s[2] && s[1] < s[2]) 23 | ; 24 | else if (s[0] < s[1] && s[0] < s[2] && s[1] > s[2]) 25 | { 26 | rra(d); 27 | sa(s); 28 | } 29 | else if (s[0] > s[1] && s[0] < s[2] && s[1] < s[2]) 30 | sa(s); 31 | else if (s[0] < s[1] && s[0] > s[2] && s[1] > s[2]) 32 | rra(d); 33 | else if (s[1] < s[2] && s[0] > s[1] && s[0] > s[2]) 34 | ra(d); 35 | else if (s[1] > s[2] && s[0] > s[1] && s[0] > s[2]) 36 | { 37 | ra(d); 38 | sa(s); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 01:32:11 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/13 10:30:03 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | int ft_error(t_list *d, int nbr); 16 | 17 | int ft_atoi(const char *str, t_list *d) 18 | { 19 | int i; 20 | int valorfinal; 21 | long long int nbr; 22 | 23 | nbr = 0; 24 | i = 0; 25 | valorfinal = 1; 26 | if (str[i] == '-' || str[i] == '+') 27 | { 28 | if (str[i] == '-') 29 | valorfinal = -1; 30 | i++; 31 | } 32 | while (str[i] >= '0' && str[i] <= '9') 33 | { 34 | nbr = nbr * 10 + str[i] - '0'; 35 | i++; 36 | if (nbr > ((long long int)INT_MAX + 1) && valorfinal == -1) 37 | return (ft_error(d, nbr)); 38 | if (nbr > INT_MAX && valorfinal == 1) 39 | return (ft_error(d, nbr)); 40 | } 41 | return (nbr * valorfinal); 42 | } 43 | 44 | int ft_error(t_list *d, int nbr) 45 | { 46 | nbr = 0; 47 | d->atoierror = 1; 48 | return (nbr); 49 | } 50 | -------------------------------------------------------------------------------- /inc/pushswap_bonus.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pushswap_bonus.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 01:26:42 by gemartin #+# #+# */ 9 | /* Updated: 2022/08/09 11:26:07 by gemartin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | #ifndef PUSHSWAP_BONUS_H 13 | # define PUSHSWAP_BONUS_H 14 | 15 | # include 16 | # include 17 | # include 18 | 19 | typedef struct s_list 20 | { 21 | int *sa; 22 | int *sb; 23 | int sia; 24 | int sib; 25 | int argc; 26 | int error; 27 | int atoierror; 28 | int jatoi; 29 | } t_list; 30 | 31 | char *ft_substr(char *s, int start, int len); 32 | char *get_next_line(int fd); 33 | int checker_bonus(int argc, char **argv); 34 | int ft_strlen(char *s); 35 | int ft_atoi(const char *str, t_list *d); 36 | int check_same_nums(t_list *d); 37 | int check_args(char **args, int argc); 38 | int check_num(char *s); 39 | void ss(t_list *d); 40 | void sa(int *sa); 41 | void rra(t_list *d); 42 | void rrb(t_list *d); 43 | void ra(t_list *d); 44 | void pa(t_list *d); 45 | void pb(t_list *d); 46 | void rb(t_list *d); 47 | void rr(t_list *d); 48 | void rrr(t_list *d); 49 | void sb(int *sb); 50 | void swap(int *argv1, int *argv2); 51 | void ft_free(t_list *d, int print); 52 | void ft_checker_lines(t_list *d); 53 | #endif 54 | -------------------------------------------------------------------------------- /src/ordernums_peq.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ordernums_peq.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/03 15:49:23 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/13 11:41:34 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void ordernums_peq_sb(t_list *d) 16 | { 17 | while (d->sib > 0) 18 | { 19 | if (d->sb[0] == d->lsp - 1) 20 | { 21 | pa(d); 22 | d->control = 1; 23 | } 24 | if (d->sb[0] == d->lsp) 25 | { 26 | pa(d); 27 | d->lsp--; 28 | if (d->control == 1) 29 | { 30 | d->lsp--; 31 | d->control = 0; 32 | sa(d->sa); 33 | } 34 | } 35 | else 36 | rb(d); 37 | } 38 | } 39 | 40 | void ordernums_peq3(t_list *d, int i) 41 | { 42 | while (i >= 0) 43 | { 44 | if (d->sa[0] == i) 45 | { 46 | pb(d); 47 | i--; 48 | } 49 | else 50 | ra(d); 51 | } 52 | ordernums_peq_sb(d); 53 | } 54 | 55 | void ordernums_peq2(t_list *d) 56 | { 57 | int i; 58 | int j; 59 | 60 | j = 0; 61 | i = d->lsp; 62 | while (d->sib < 1) 63 | { 64 | if (d->sa[0] == i) 65 | pb(d); 66 | else 67 | ra(d); 68 | } 69 | } 70 | 71 | void ordernums_peq(t_list *d) 72 | { 73 | int i; 74 | int j; 75 | 76 | j = 0; 77 | i = d->lsp; 78 | while (d->sib <= (d->argc / 2)) 79 | { 80 | if (d->sa[0] == i) 81 | { 82 | pb(d); 83 | i--; 84 | } 85 | else 86 | ra(d); 87 | } 88 | ordernums_peq_sb(d); 89 | ordernums_peq3(d, i); 90 | } 91 | -------------------------------------------------------------------------------- /inc/pushswap.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pushswap.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 01:26:42 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/13 18:19:44 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | #ifndef PUSHSWAP_H 13 | # define PUSHSWAP_H 14 | 15 | # include 16 | # include 17 | # include 18 | 19 | typedef struct s_list 20 | { 21 | int *sa; 22 | int *sb; 23 | int *sp; 24 | int sia; 25 | int sib; 26 | int sip; 27 | int argc; 28 | int percentage; 29 | int lsp; 30 | int control; 31 | int cont; 32 | int error; 33 | int atoierror; 34 | int jatoi; 35 | int size_block; 36 | int argmax; 37 | int arguments; 38 | } t_list; 39 | 40 | char *ft_substr(char *s, int start, int len); 41 | int main(int argc, char **argv); 42 | int ft_strlen(char *s); 43 | int ft_atoi(const char *str, t_list *d); 44 | int check_same_nums(t_list *d); 45 | int check_order(t_list *d); 46 | int ft_isdigit(int i); 47 | int ft_space(char *s); 48 | int check_num(char *s); 49 | int ft_arg2_to_int(char *s, t_list *d); 50 | int check_args(char **args, int argc); 51 | void swap(int *argv1, int *argv2); 52 | void sortnum(t_list *d); 53 | void changenum(t_list *d); 54 | void ordernums(t_list *d); 55 | void sa(int *sa); 56 | void rra(t_list *d); 57 | void rrb(t_list *d); 58 | void ra(t_list *d); 59 | void size3(int *s, t_list *d); 60 | void size5(int *s, t_list *d); 61 | void size100(t_list *d); 62 | void size500(t_list *d); 63 | void pa(t_list *d); 64 | void pb(t_list *d); 65 | void ordernums_peq(t_list *d); 66 | void rb(t_list *d); 67 | void rr(t_list *d); 68 | void rrr(t_list *d); 69 | void ft_free(t_list *d); 70 | void ft_free2(t_list *d); 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /bns/get_next_line_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:52:44 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:10:05 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | static char *ft_strjoin(char *s1, char *s2) 16 | { 17 | char *str; 18 | size_t i; 19 | size_t c; 20 | 21 | if (!s1) 22 | { 23 | s1 = malloc(sizeof(char) + 1); 24 | s1[0] = 0; 25 | } 26 | str = (char *)malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1)); 27 | if (!str) 28 | return (0); 29 | i = -1; 30 | while (s1[++i]) 31 | str[i] = s1[i]; 32 | c = 0; 33 | while (s2[c]) 34 | { 35 | str[i + c] = s2[c]; 36 | c++; 37 | } 38 | str[i + c] = '\0'; 39 | free(s1); 40 | return (str); 41 | } 42 | 43 | static char *ft_strchr(const char *s, int c) 44 | { 45 | int i; 46 | 47 | i = 0; 48 | while (s[i] != '\0') 49 | { 50 | if (s[i] == (char)c) 51 | return (&((char *)s)[i]); 52 | i++; 53 | } 54 | if ((char)c == '\0') 55 | return (&((char *)s)[i]); 56 | return (0); 57 | } 58 | 59 | char *readbuf(int fd, char *line) 60 | { 61 | int r; 62 | char *buffer; 63 | 64 | r = 1; 65 | buffer = malloc(sizeof(char) * (1 + 1)); 66 | if (!buffer) 67 | return (NULL); 68 | buffer[0] = '\0'; 69 | while (r > 0 && !ft_strchr(buffer, '\n')) 70 | { 71 | r = read (fd, buffer, 1); 72 | if (r == -1) 73 | { 74 | free(buffer); 75 | return (NULL); 76 | } 77 | if (r > 0) 78 | { 79 | buffer[r] = '\0'; 80 | line = ft_strjoin(line, buffer); 81 | } 82 | } 83 | free(buffer); 84 | return (line); 85 | } 86 | 87 | char *get_next_line(int fd) 88 | { 89 | char *line; 90 | char *line2; 91 | 92 | line2 = NULL; 93 | if (fd < 0) 94 | return (NULL); 95 | line = readbuf (fd, line2); 96 | return (line); 97 | } 98 | -------------------------------------------------------------------------------- /src/size5.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* size5.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: marvin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/11 06:31:10 by marvin #+# #+# */ 9 | /* Updated: 2022/04/13 09:03:41 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void size4(int *s, t_list *d) 16 | { 17 | if (d->sia == 4) 18 | { 19 | if (s[0] < s[1] && s[0] < s[2] && s[0] < s[3] && d->sia == 4) 20 | pb(d); 21 | if (s[0] > s[1] && s[1] < s[2] && s[1] < s[3] && d->sia == 4) 22 | { 23 | sa(d->sa); 24 | pb(d); 25 | } 26 | if (s[0] > s[2] && s[1] > s[2] && s[2] < s[3] && d->sia == 4) 27 | { 28 | ra(d); 29 | sa(d->sa); 30 | pb(d); 31 | } 32 | if (s[0] > s[3] && s[1] > s[3] && s[2] > s[3] && d->sia == 4) 33 | { 34 | rra(d); 35 | pb(d); 36 | } 37 | } 38 | if (d->sia == 3) 39 | size3(d->sa, d); 40 | pa(d); 41 | } 42 | 43 | int size5_2(int *s, t_list *d, int i) 44 | { 45 | if (s[0] > s[2] && s[1] > s[2] && s[2] < s[3] && s[2] < s[4] && i == 0) 46 | { 47 | ra(d); 48 | sa(d->sa); 49 | pb(d); 50 | i++; 51 | } 52 | if (s[0] > s[3] && s[1] > s[3] && s[2] > s[3] && s[3] < s[4] && i == 0) 53 | { 54 | rra(d); 55 | rra(d); 56 | pb(d); 57 | i++; 58 | } 59 | if (s[0] > s[4] && s[1] > s[4] && s[2] > s[4] && s[3] > s[4] && i == 0) 60 | { 61 | rra(d); 62 | pb(d); 63 | i++; 64 | } 65 | return (i); 66 | } 67 | 68 | void size5(int *s, t_list *d) 69 | { 70 | int i; 71 | 72 | i = 0; 73 | if (d->sia == 5) 74 | { 75 | if (s[0] < s[1] && s[0] < s[2] && s[0] < s[3] && s[0] < s[4] && i == 0) 76 | { 77 | pb(d); 78 | i++; 79 | } 80 | if (s[0] > s[1] && s[1] < s[2] && s[1] < s[3] && s[1] < s[4] && i == 0) 81 | { 82 | sa(d->sa); 83 | pb(d); 84 | i++; 85 | } 86 | i = size5_2(d->sa, d, i); 87 | } 88 | size4(d->sa, d); 89 | if (i == 1) 90 | { 91 | pa(d); 92 | if (s[0] > s[1]) 93 | sa(d->sa); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /bns/utils_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:52:44 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:10:05 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | void ft_free(t_list *d, int print) 16 | { 17 | if (d->sa) 18 | { 19 | free(d->sa); 20 | } 21 | if (d->sb) 22 | { 23 | free(d->sb); 24 | } 25 | if (d) 26 | { 27 | free(d); 28 | } 29 | if (print == 0) 30 | write (2, "Error\n", 6); 31 | exit (0); 32 | } 33 | 34 | static int ft_error(t_list *d, int nbr) 35 | { 36 | nbr = 0; 37 | d->atoierror = 1; 38 | return (nbr); 39 | } 40 | 41 | int ft_atoi(const char *str, t_list *d) 42 | { 43 | int i; 44 | int valorfinal; 45 | long long int nbr; 46 | 47 | nbr = 0; 48 | i = 0; 49 | valorfinal = 1; 50 | if (str[i] == '-' || str[i] == '+') 51 | { 52 | if (str[i] == '-') 53 | valorfinal = -1; 54 | i++; 55 | } 56 | while (str[i] >= '0' && str[i] <= '9') 57 | { 58 | nbr = nbr * 10 + str[i] - '0'; 59 | i++; 60 | if (nbr > ((long long int)INT_MAX + 1) && valorfinal == -1) 61 | return (ft_error(d, nbr)); 62 | if (nbr > INT_MAX && valorfinal == 1) 63 | return (ft_error(d, nbr)); 64 | } 65 | return (nbr * valorfinal); 66 | } 67 | 68 | int ft_strlen(char *s) 69 | { 70 | int i; 71 | 72 | i = 0; 73 | while (s[i]) 74 | i++; 75 | return (i); 76 | } 77 | 78 | char *ft_substr(char *s, int start, int len) 79 | { 80 | int i; 81 | char *res; 82 | 83 | i = 0; 84 | if (!s) 85 | return (0); 86 | if (start > ft_strlen(s)) 87 | { 88 | res = malloc(1); 89 | if (!res) 90 | return (0); 91 | res[0] = '\0'; 92 | return (res); 93 | } 94 | if (ft_strlen(s) - start < len) 95 | len = ft_strlen(s) - start; 96 | res = malloc(sizeof(char) * (len + 1)); 97 | if (!res) 98 | return (0); 99 | while (start < ft_strlen(s) && i < len && s[start]) 100 | res[i++] = s[start++]; 101 | res[i] = '\0'; 102 | return (res); 103 | } 104 | -------------------------------------------------------------------------------- /bns/checker_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* checker_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/03/25 12:25:41 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/13 18:35:36 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | int check_num(char *s) 16 | { 17 | int i; 18 | int j; 19 | 20 | i = 0; 21 | j = 0; 22 | if ( 23 | (s[j] == '-' && (s[j + 1] >= '0' && s[j + 1] <= '9')) 24 | || (s[j] >= '0' && s[j] <= '9')) 25 | { 26 | i++; 27 | j++; 28 | while (s[j]) 29 | { 30 | if (s[j] >= '0' && s[j] <= '9') 31 | i++; 32 | j++; 33 | } 34 | } 35 | if (i == ft_strlen(s)) 36 | return (0); 37 | return (-1); 38 | } 39 | 40 | static int arg_to_int(t_list *d, int arc, char **args) 41 | { 42 | int i; 43 | 44 | i = 0; 45 | d->atoierror = 0; 46 | while (i != arc - 1 && d->atoierror == 0) 47 | { 48 | if (check_num(args[i + 1]) == -1) 49 | return (-1); 50 | d->sa[i] = ft_atoi(args[i + 1], d); 51 | if (d->atoierror == 1) 52 | return (-1); 53 | i++; 54 | } 55 | d->atoierror = 0; 56 | return (0); 57 | } 58 | 59 | static int init_data(t_list *d, int arc) 60 | { 61 | d->argc = arc - 1; 62 | d->error = 0; 63 | d->sa = malloc(sizeof(int) * d->argc); 64 | if (!d->sa) 65 | { 66 | free(d); 67 | return (-1); 68 | } 69 | d->sb = malloc(sizeof(int) * d->argc); 70 | if (!d->sb) 71 | { 72 | free(d->sa); 73 | free(d); 74 | return (-1); 75 | } 76 | d->sia = d->argc; 77 | d->sib = 0; 78 | return (0); 79 | } 80 | 81 | int main(int arc, char **args) 82 | { 83 | t_list *d; 84 | int j; 85 | 86 | j = 0; 87 | if (arc == 1) 88 | return (0); 89 | d = malloc(sizeof(t_list)); 90 | if (!d) 91 | return (0); 92 | if (check_args(args, arc - 1) == -1) 93 | ft_free(d, 0); 94 | if (init_data(d, arc) == -1) 95 | return (0); 96 | if (arg_to_int(d, arc, args) == -1) 97 | { 98 | ft_free(d, 0); 99 | return (0); 100 | } 101 | if (check_same_nums(d) == -1) 102 | ft_free(d, 0); 103 | ft_checker_lines(d); 104 | ft_free(d, 1); 105 | return (0); 106 | } 107 | -------------------------------------------------------------------------------- /bns/checker_lines_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* checker_lines_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 13:52:44 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/07 23:10:05 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap_bonus.h" 14 | 15 | static int ft_strncmp(const char *s1, const char *s2, size_t n) 16 | { 17 | size_t i; 18 | 19 | i = 0; 20 | if (n == 0) 21 | return (0); 22 | while (s1[i] && s2[i] && s1[i] == s2[i] && i + 1 < n) 23 | { 24 | i++; 25 | } 26 | return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]); 27 | } 28 | 29 | void filter_function(char *line, t_list *d) 30 | { 31 | if (ft_strncmp(line, "sa\n", 3) == 0) 32 | sa(d->sa); 33 | else if (ft_strncmp(line, "sb\n", 3) == 0) 34 | sb(d->sb); 35 | else if (ft_strncmp(line, "ss\n", 3) == 0) 36 | ss(d); 37 | else if (ft_strncmp(line, "pa\n", 3) == 0) 38 | pa(d); 39 | else if (ft_strncmp(line, "pb\n", 3) == 0) 40 | pb(d); 41 | else if (ft_strncmp(line, "ra\n", 3) == 0) 42 | ra(d); 43 | else if (ft_strncmp(line, "rb\n", 3) == 0) 44 | rb(d); 45 | else if (ft_strncmp(line, "rr\n", 3) == 0) 46 | rr(d); 47 | else if (ft_strncmp(line, "rra\n", 4) == 0) 48 | rra(d); 49 | else if (ft_strncmp(line, "rrb\n", 4) == 0) 50 | rrb(d); 51 | else if (ft_strncmp(line, "rrr\n", 4) == 0) 52 | rrr(d); 53 | else 54 | ft_free(d, 0); 55 | } 56 | 57 | static int check_order(t_list *d) 58 | { 59 | int i; 60 | 61 | i = -1; 62 | while (++i < d->sia - 1) 63 | { 64 | if (d->sa[i] > d->sa[i + 1]) 65 | return (-1); 66 | } 67 | if (d->sib > 0) 68 | return (-1); 69 | return (1); 70 | } 71 | 72 | static int check_order_sib(t_list *d) 73 | { 74 | if (check_order(d) == 1 && d->sib == 0) 75 | return (0); 76 | else 77 | return (1); 78 | } 79 | 80 | void ft_checker_lines(t_list *d) 81 | { 82 | char *line; 83 | 84 | while (1) 85 | { 86 | line = get_next_line(0); 87 | if (!line) 88 | break ; 89 | filter_function(line, d); 90 | if (line) 91 | free(line); 92 | } 93 | if (line) 94 | free(line); 95 | if (check_order_sib(d) == 0) 96 | write (1, "OK\n", 3); 97 | else 98 | write (1, "KO\n", 3); 99 | } 100 | -------------------------------------------------------------------------------- /src/pushswap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pushswap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/03/25 12:25:41 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/13 18:35:36 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | int arg_to_int(t_list *d, int arc, char **args); 16 | int ft_choose_size(int arc, t_list *d); 17 | int init_data(t_list *d); 18 | 19 | int main(int arc, char **args) 20 | { 21 | t_list *d; 22 | 23 | if (arc == 1) 24 | return (0); 25 | d = malloc(sizeof(t_list)); 26 | if (!d) 27 | return (0); 28 | if (check_args(args, arc - 1) == -1) 29 | ft_free(d); 30 | d->argc = arc - 1; 31 | d->error = 0; 32 | if (init_data(d) == -1) 33 | ft_free(d); 34 | if (arg_to_int(d, arc, args) == -1) 35 | { 36 | ft_free(d); 37 | return (0); 38 | } 39 | if (check_order(d) == -1) 40 | ft_free2(d); 41 | if (ft_choose_size(arc, d) == -1) 42 | ft_free(d); 43 | ft_free2(d); 44 | return (0); 45 | } 46 | 47 | int ft_choose_size(int arc, t_list *d) 48 | { 49 | if (check_same_nums(d) == -1) 50 | return (-1); 51 | else 52 | { 53 | if (arc == 3 || arc == 4) 54 | size3(d->sa, d); 55 | if (arc < 7 && arc > 4) 56 | size5(d->sa, d); 57 | if (arc < 102 && arc > 6) 58 | size100(d); 59 | if (arc > 101) 60 | size500(d); 61 | } 62 | return (0); 63 | } 64 | 65 | int arg_to_int(t_list *d, int arc, char **args) 66 | { 67 | int i; 68 | 69 | i = 0; 70 | while (i != arc - 1) 71 | { 72 | if (check_num(args[i + 1]) == -1) 73 | return (-1); 74 | d->sa[i] = ft_atoi(args[i + 1], d); 75 | if (d->atoierror == 1) 76 | return (-1); 77 | d->sp[i] = ft_atoi(args[i + 1], d); 78 | i++; 79 | } 80 | return (0); 81 | } 82 | 83 | int init_data(t_list *d) 84 | { 85 | d->sa = malloc(sizeof(int) * d->argc); 86 | if (!d->sa) 87 | { 88 | free(d); 89 | return (-1); 90 | } 91 | d->sp = malloc(sizeof(int) * d->argc); 92 | if (!d->sp) 93 | { 94 | free(d->sa); 95 | free(d); 96 | return (-1); 97 | } 98 | d->sb = malloc(sizeof(int) * d->argc); 99 | if (!d->sb) 100 | { 101 | free(d->sp); 102 | free(d->sa); 103 | free(d); 104 | return (-1); 105 | } 106 | d->sia = d->argc; 107 | d->sib = 0; 108 | d->sip = d->argc; 109 | return (0); 110 | } 111 | -------------------------------------------------------------------------------- /src/ordernums.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ordernums.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: gemartin +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/04/02 01:47:42 by gemartin #+# #+# */ 9 | /* Updated: 2022/04/13 11:38:53 by marvin ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../inc/pushswap.h" 14 | 15 | void calc(t_list *d) 16 | { 17 | if (d->sb[0] == d->lsp - 1) 18 | { 19 | pa(d); 20 | d->control = 1; 21 | } 22 | if (d->sb[0] == d->lsp) 23 | { 24 | pa(d); 25 | d->lsp--; 26 | if (d->control == 1) 27 | { 28 | d->lsp--; 29 | d->control = 0; 30 | sa(d->sa); 31 | } 32 | if (d->control == 2) 33 | { 34 | d->lsp = d->lsp - 2; 35 | d->control = 0; 36 | sa(d->sa); 37 | rra(d); 38 | } 39 | } 40 | else 41 | rb(d); 42 | } 43 | 44 | int check_mov(t_list *d) 45 | { 46 | int i; 47 | 48 | i = 0; 49 | while (i <= d->sib - 1) 50 | { 51 | if (d->sb[i] == d->lsp) 52 | break ; 53 | i++; 54 | } 55 | if ((d->sib / 2) - i >= 0) 56 | return (0); 57 | else 58 | return (-1); 59 | } 60 | 61 | void ordernums_sb_rotate(t_list *d) 62 | { 63 | if (d->sb[0] == d->lsp -2 && d->control == 1) 64 | { 65 | pa(d); 66 | ra(d); 67 | d->control = 2; 68 | } 69 | if (d->sb[0] == d->lsp - 1) 70 | { 71 | pa(d); 72 | d->control = 1; 73 | } 74 | if (d->sb[0] == d->lsp) 75 | { 76 | pa(d); 77 | d->lsp--; 78 | if (d->control == 1) 79 | { 80 | d->lsp--; 81 | d->control = 0; 82 | sa(d->sa); 83 | } 84 | } 85 | else 86 | rrb(d); 87 | } 88 | 89 | void ordernums_sb(t_list *d) 90 | { 91 | while (d->sia < d->argc) 92 | { 93 | if (d->sb[0] == d->lsp && d->control == 0) 94 | { 95 | pa(d); 96 | d->lsp--; 97 | } 98 | if (d->sb[0] == d->lsp -2 && d->control == 1) 99 | { 100 | pa(d); 101 | ra(d); 102 | d->control = 2; 103 | } 104 | if (check_mov(d) == -1) 105 | ordernums_sb_rotate(d); 106 | else 107 | calc(d); 108 | } 109 | } 110 | 111 | void ordernums(t_list *d) 112 | { 113 | d->arguments = (d->argc / d->cont) * (d->percentage); 114 | if (d->percentage == d->cont) 115 | d->arguments = d->argmax - 3; 116 | while (d->sib < d->arguments && d->percentage <= d->cont) 117 | { 118 | if (d->sa[0] < d->arguments) 119 | { 120 | pb(d); 121 | if (d->sb[0] > d->arguments - d->size_block && d->sib > 1) 122 | rb(d); 123 | } 124 | else 125 | ra(d); 126 | } 127 | d->percentage++; 128 | if (d->sib == d->argmax - 3) 129 | { 130 | size3(d->sa, d); 131 | d->lsp = d->lsp - 3; 132 | } 133 | if (d->percentage <= d->cont) 134 | ordernums(d); 135 | while (d->percentage-- > 0) 136 | ordernums_sb(d); 137 | } 138 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #******************************************************************************# 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: gemartin +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2022/04/02 02:30:43 by gemartin #+# #+# # 9 | # Updated: 2022/04/13 18:34:38 by marvin ### ########.fr # 10 | # # 11 | #******************************************************************************# 12 | 13 | NAME = push_swap 14 | 15 | NAME_BONUS = checker 16 | 17 | DEL_LINE = \033[2K 18 | ITALIC = \033[3m 19 | BOLD = \033[1m 20 | DEF_COLOR = \033[0;39m 21 | GRAY = \033[0;90m 22 | RED = \033[0;91m 23 | GREEN = \033[0;92m 24 | YELLOW = \033[0;93m 25 | BLUE = \033[0;94m 26 | MAGENTA = \033[0;95m 27 | CYAN = \033[0;96m 28 | WHITE = \033[0;97m 29 | BLACK = \033[0;99m 30 | ORANGE = \033[38;5;209m 31 | BROWN = \033[38;2;184;143;29m 32 | DARK_GRAY = \033[38;5;234m 33 | MID_GRAY = \033[38;5;245m 34 | DARK_GREEN = \033[38;2;75;179;82m 35 | DARK_YELLOW = \033[38;5;143m 36 | 37 | SRCS = ./src/pushswap.c ./src/ft_strlen.c ./src/ft_atoi.c ./src/swap.c ./src/sortnum.c \ 38 | ./src/changenum.c ./src/sa.c ./src/rra.c ./src/ra.c ./src/rrb.c \ 39 | ./src/size3.c ./src/size5.c ./src/size100.c ./src/size500.c \ 40 | ./src/rb.c ./src/rr.c ./src/sb.c ./src/rrr.c ./src/check_order.c \ 41 | ./src/check_same_nums.c ./src/ft_free.c ./src/ordernums_peq.c ./src/ordernums.c \ 42 | ./src/pa.c ./src/pb.c ./src/check_num.c ./src/ft_isdigit.c \ 43 | ./src/ft_substr.c ./src/ft_free2.c ./src/check_args.c 44 | 45 | BONUS_SRC = ./bns/checker_bonus.c ./bns/ss_bonus.c ./bns/sa_bonus.c ./bns/sb_bonus.c ./bns/pa_bonus.c \ 46 | ./bns/rb_bonus.c ./bns/rr_bonus.c ./bns/rra_bonus.c ./bns/rrb_bonus.c ./bns/rrr_bonus.c ./bns/swap_bonus.c \ 47 | ./bns/get_next_line_bonus.c ./bns/checker_lines_bonus.c ./bns/check_same_nums_bonus.c \ 48 | ./bns/utils_bonus.c ./bns/check_args_bonus.c ./bns/pb_bonus.c ./bns/ra_bonus.c 49 | 50 | INCLUDE = ./inc/pushswap.h 51 | 52 | INCLUDE_BONUS = ./inc/pushswap_bonus.h 53 | 54 | CC = gcc 55 | RM = rm -f 56 | CFLAGS = -Wall -Wextra -Werror -MMD 57 | 58 | %.o: %.c 59 | @echo "${BLUE} ◎ $(BROWN)Compiling ${MAGENTA}→ $(CYAN)$< $(DEF_COLOR)" 60 | @${CC} ${CFLAGS} -c $< -o $@ 61 | 62 | OBJS = ${SRCS:.c=.o} 63 | BONUS_OBJS = ${BONUS_SRC:.c=.o} 64 | DEPS = $(addsuffix .d, $(basename $(SRCS))) 65 | DEPS2 = $(addsuffix .d, $(basename $(BONUS_SRC))) 66 | 67 | all: ${NAME} 68 | 69 | -include ${DEPS} 70 | ${NAME}: ${OBJS} $(INCLUDE) 71 | @${CC} ${SRCS} -o ${NAME} 72 | @echo "\n$(GREEN) Created $(NAME) ✓ $(DEF_COLOR)\n" 73 | 74 | -include ${DEPS2} 75 | bonus: ${BONUS_OBJS} 76 | @touch $@ 77 | @${CC} ${BONUS_SRC} -o ${NAME_BONUS} 78 | @echo "\n$(GREEN)Created ${NAME_BONUS} ✓$(DEF_COLOR)\n" 79 | 80 | clean: 81 | @${RM} ${OBJS} ${BONUS_OBJS} 82 | @${RM} ${DEPS} ${DEPS2} 83 | @echo "\n${BLUE} ◎ $(RED)All objects cleaned successfully ${BLUE}◎$(DEF_COLOR)\n" 84 | 85 | fclean: 86 | @${RM} ${OBJS} ${BONUS_OBJS} 87 | @${RM} ${DEPS} ${DEPS2} 88 | @${RM} bonus 89 | @${RM} ${NAME} ${NAME_BONUS} 90 | @echo "\n${BLUE} ◎ $(RED)All objects and executable cleaned successfully${BLUE} ◎$(DEF_COLOR)\n" 91 | 92 | re: fclean all 93 | 94 | .PHONY: all clean fclean re -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Sobre push_swap 🔢 2 | 3 | Push_swap es un proyecto de algoritmia simple, tienes que ordenar 4 | datos. Tienes a tu disposición un conjunto de valores int, dos stacks y un conjunto de 5 | instrucciones para manipular ambos. Deberás utilizar el minimo de instrucciones posible. 6 | 7 | 8 | 9 | ## Operaciones 🔁 10 | 11 | | Operaciones | Explicación | 12 | | :--- | :--- | 13 | | sa | swap a - intercambia los dos primeros elementos encima del stack a. | 14 | | sb | swap b - intercambia los dos primeros elementos encima del stack b. | 15 | | ss | swap a y swap b a la vez. | 16 | | pa | push a - toma el primer elemento del stack b y lo pone encima del stack a. | 17 | | pb | push b - toma el primer elemento del stack a y lo pone encima del stack b. | 18 | | ra | rotate a - desplaza hacia arriba todos los elementos del stack a una posición, el primer elemento se convierte en el último. | 19 | | rb | rotate b - desplaza hacia arriba todos los elementos del stack b una posición, el primer elemento se convierte en el último. | 20 | | rr | ra y rb a la vez. | 21 | | rra | reverse rotate a - desplaza hacia abajo todos los elementos del stack a una posición, el último elemento se convierte en el primero. | 22 | | rrb | reverse rotate b - desplaza hacia abajo todos los elementos del stack b una posición, el último elemento se convierte en el primero. | 23 | | rrr | rra y rrb a la vez. | 24 | 25 | ## Calificación 💯 26 | 27 | ⚠️ Necesitaras un mínimo de 84/100 para aprobar este proyecto ⚠️ 28 | 29 | ### 🔹 3 Numeros 30 | 31 | Max 3 operaciones. 32 | 33 | ### 🔹 5 Numeros 34 | 35 | Max 12 operaciones. 36 | 37 | ![](https://github.com/gemartin99/Push_swap/blob/master/push_swap5.gif) 38 | 39 | ### 🔹 100 Numeros: 40 | 41 | | Operaciones | Puntos | 42 | | :---: | :---: | 43 | | Menos de 700 | 5 | 44 | | Menos de 900 | 4 | 45 | | Menos de 1100 | 3 | 46 | | Menos de 1300 | 2 | 47 | | Menos de 1500 | 1 | 48 | 49 | ![](https://github.com/gemartin99/Push_swap/blob/master/push_swap100.gif) 50 | 51 | ### 🔹 500 Numeros: 52 | 53 | | Operaciones | Puntos | 54 | | :---: | :---: | 55 | | Menos de 5500 | 5 | 56 | | Menos de 7000 | 4 | 57 | | Menos de 8500 | 3 | 58 | | Menos de 10000 | 2 | 59 | | Menos de 11500 | 1 | 60 | 61 | ## BONUS ⭐️ 62 | 63 | Solo se debe hacer el bonus si la parte obligatoria esta al 100%. El bonus consiste en el desarrollo de un programa llamado checker , que verificará 64 | si tu stack A esta ordenado. Para ello deberás mandarle como argumentos los numeros del stack y una vez se ejecute el programa debes mandarle las operaciones que quieres realizar para ordenarlo. Cuando queramos dejar de mandarle operaciones haremos ```Ctrl + D``` y el checker nos dirá "OK" si esta ordenado o "KO" si no lo esta. La controlación de errores del checker debe ser la misma que en push_swap. 65 | 66 | ![](https://github.com/gemartin99/Push_swap/blob/master/checker.gif) 67 | 68 | # TESTER ✅🆗 69 | 70 | Comprueba que todo este correcto antes de validar el proyecto! 71 | 72 | ➡️ [TESTER PROPIO](https://github.com/gemartin99/Push_swap_tester) ⬅️ 73 | 74 | # Autor ✍🏼 75 | 76 | 77 | 78 | 79 | 80 |
100px
gemartin

Intra 42
81 | 82 | # Quizás pueda interesarte! 83 | 84 | ### - Para ver mi progresion en el common core 42 ↙️ 85 | 86 | [AQUÍ](https://github.com/gemartin99/42cursus) 87 | 88 | ### - Mi perfil en la intranet de 42 ↙️ 89 | [AQUÍ](https://profile.intra.42.fr/users/gemartin) 90 | 91 | ### - Contacto 📥 92 | 93 | ◦ Email: gemartin@student.42barcelona.com 94 | 95 | ◦ Linkedin: https://www.linkedin.com/in/gemartin99/ 96 | 97 | -------------------------------------------------------------------------------- /pyviz.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from tkinter import * 4 | import sys 5 | import time 6 | import subprocess 7 | import os 8 | from math import sqrt 9 | 10 | 11 | """ 12 | __project__ = "push_swap visualizer" 13 | __author__ = "Emmanuel Ruaud" 14 | __email__ = "eruaud@student.le-101.fr" 15 | This python script was created to visualize your work with the PUSH_SWAP 16 | 42 Project. 17 | You must put this script in the same path or in a sibling path of your program 18 | Of course you need Python3 with the builtin Tkinter. 19 | You can install it with Brew. 20 | --> Brew install python3 21 | Execute the script with : 22 | --> python3 pyviz.py `ruby -e "puts (-200..200).to_a.shuffle.join(' ')"` 23 | You can change the PUSHS_PATH to get to the relative path of your push_swap 24 | You can decrease or increase the speed with the matching buttons. 25 | """ 26 | 27 | 28 | RELATIVE_PATH = r'push_swap' 29 | 30 | 31 | class PsGui: 32 | def __init__(self, master): 33 | ww = 600 34 | wh = 600 35 | self.i = 0 36 | self.speed = 0 37 | dirname = os.path.dirname(os.path.abspath(__file__)) 38 | PUSHS_PATH = os.path.join(dirname, RELATIVE_PATH) 39 | self.pile_a = [int(num) for num in sys.argv[1:]] 40 | self.first_pile = self.pile_a[:] 41 | self.pile_b = [] 42 | self.cmds = subprocess.check_output([PUSHS_PATH] + sys.argv[1:], stderr=subprocess.STDOUT, 43 | timeout=12).splitlines() 44 | if len(self.pile_a) != 0: 45 | self.prespeed = 1 / len(self.pile_a) 46 | else: 47 | self.prespeed = 0 48 | self.master = master 49 | master.title("Push_swap viewer") 50 | self.mainframe = Frame(master) 51 | self.mainframe.pack(fill=BOTH) 52 | self.can = Canvas(self.mainframe, width=ww, height=wh, bg="black") 53 | self.can.pack(side=LEFT) 54 | self.toolframe = Frame(self.mainframe) 55 | self.toolframe.pack(side=RIGHT, fill=BOTH) 56 | self.butframe = Frame(self.toolframe) 57 | self.butframe.pack(side=TOP, fill=Y) 58 | self.PrevCtl = Button(self.butframe, text="<<", command=self.speed_down) 59 | self.PrevCtl.pack(side=LEFT) 60 | self.PauseCtl = Button(self.butframe, text=">", command=self.pause) 61 | self.PauseCtl.pack(side=LEFT) 62 | self.NextCtl = Button(self.butframe, text=">>", command=self.speed_up) 63 | self.NextCtl.pack(side=LEFT) 64 | self.ResetCtl = Button(self.butframe, text="R", command=self.reset) 65 | self.ResetCtl.pack(side=LEFT) 66 | self.listbox = Listbox(self.toolframe, bg='black', fg='light cyan', 67 | font=("monospace", 12), relief=FLAT) 68 | self.listbox.pack(fill=BOTH, expand=1) 69 | for cmd in self.cmds: 70 | self.listbox.insert(END, cmd) 71 | self.statusframe = Frame(master) 72 | self.statusframe.pack(side=BOTTOM, fill=X) 73 | self.speedmeter = Label(self.statusframe, 74 | text='frame rate = ' + str(self.speed), 75 | font=("monospace", 10)) 76 | self.speedmeter.pack(side=LEFT) 77 | self.totalcount = Label(self.statusframe, 78 | text='- operations = ' + str(len(self.cmds)), 79 | font=("monospace", 10)) 80 | self.totalcount.pack(side=LEFT) 81 | self.draw_rectangles() 82 | self.launch() 83 | 84 | def reset(self): 85 | self.speed = 0 86 | self.i = 0 87 | del self.pile_a[:] 88 | self.pile_a = self.first_pile[:] 89 | del self.pile_b[:] 90 | self.can.delete("all") 91 | self.draw_rectangles() 92 | self.listbox.see(0) 93 | self.PauseCtl.config(text='>') 94 | self.launch() 95 | 96 | def pause(self): 97 | if self.speed != 0: 98 | self.prespeed = self.speed 99 | self.speed = 0 100 | self.speedmeter.config(text='frame rate = 0') 101 | self.PauseCtl.config(text='>') 102 | else: 103 | self.speed = self.prespeed 104 | self.speedmeter.config(text='frame rate = ' \ 105 | + '{:.2e}'.format(self.speed)) 106 | self.PauseCtl.config(text='||') 107 | 108 | def speed_up(self): 109 | if self.speed == 0: 110 | self.PauseCtl.config(text='||') 111 | self.speed = self.prespeed 112 | self.speed = self.speed ** 2 113 | self.speedmeter.config(text='frame rate = ' \ 114 | + '{:.2e}'.format(self.speed)) 115 | 116 | def speed_down(self): 117 | self.speed = sqrt(self.speed) 118 | self.speedmeter.config(text='frame rate = ' \ 119 | + '{:.2e}'.format(self.speed)) 120 | 121 | def launch_cmds(self, cmd): 122 | if cmd == b'sa' and len(self.pile_a) >= 2: 123 | self.pile_a[0], self.pile_a[1] = self.pile_a[1], self.pile_a[0] 124 | if cmd == b'sb' and len(self.pile_b) >= 2: 125 | self.pile_b[0], self.pile_b[1] = self.pile_b[1], self.pile_b[0] 126 | if cmd == b'ss': 127 | if (len(self.pile_a) >= 2): 128 | self.pile_a[0], self.pile_a[1] = self.pile_a[1], self.pile_a[0] 129 | if (len(self.pile_b) >= 2): 130 | self.pile_b[0], self.pile_b[1] = self.pile_b[1], self.pile_b[0] 131 | if cmd == b'ra' and len(self.pile_a) >= 2: 132 | self.pile_a.append(self.pile_a[0]) 133 | del self.pile_a[0] 134 | if cmd == b'rb' and len(self.pile_b) >= 2: 135 | self.pile_b.append(self.pile_b[0]) 136 | del self.pile_b[0] 137 | if cmd == b'rr': 138 | if (len(self.pile_a) >= 2): 139 | self.pile_a.append(self.pile_a[0]) 140 | del self.pile_a[0] 141 | if (len(self.pile_b) >= 2): 142 | self.pile_b.append(self.pile_b[0]) 143 | del self.pile_b[0] 144 | if cmd == b'rra' and len(self.pile_a) >= 2: 145 | self.pile_a = [self.pile_a[-1]] + self.pile_a 146 | del self.pile_a[-1] 147 | if cmd == b'rrb' and len(self.pile_b) >= 2: 148 | self.pile_b = [self.pile_b[-1]] + self.pile_b 149 | del self.pile_b[-1] 150 | if cmd == b'rrr': 151 | if (len(self.pile_a) >= 2): 152 | self.pile_a = [self.pile_a[-1]] + self.pile_a 153 | del self.pile_a[-1] 154 | if (len(self.pile_b) >= 2): 155 | self.pile_b = [self.pile_b[-1]] + self.pile_b 156 | del self.pile_b[-1] 157 | if cmd == b'pa' and len(self.pile_b) >= 1: 158 | self.pile_a = [self.pile_b[0]] + self.pile_a 159 | del self.pile_b[0] 160 | if cmd == b'pb' and len(self.pile_a) >= 1: 161 | self.pile_b = [self.pile_a[0]] + self.pile_b 162 | del self.pile_a[0] 163 | return self.pile_a, self.pile_b 164 | 165 | def set_color(self, index): 166 | col = '#%02x%02x%02x' % (int(255 * (index - 0.3) * (index > 0.3)), 167 | int(255 * index 168 | - ((510 * (index - 0.6)) * (index > 0.6))), 169 | int((255 - 510 * index) * (index < 0.5))) 170 | return col 171 | 172 | def draw_rectangles(self): 173 | vi = 0 174 | ww = 600 175 | wh = 600 176 | hw = ww / 2 177 | hm = len(self.pile_a) + len(self.pile_b) 178 | mx, mn = (0, 0) 179 | if (hm != 0): 180 | mx = max(self.pile_a + self.pile_b) 181 | mn = min(self.pile_a + self.pile_b) 182 | rects = [] 183 | if len(self.pile_a) > 0: 184 | a_val = [(num - mn) / (mx - mn) for num in self.pile_a] 185 | for vala in a_val: 186 | rects.append(self.can.create_rectangle(0, vi, 187 | 10 + vala * (hw - 100), vi + wh / hm, 188 | fill=self.set_color(vala), outline="")) 189 | vi += wh / hm 190 | vi = 0 191 | if len(self.pile_b) > 0: 192 | b_val = [(num - mn) / (mx - mn) for num in self.pile_b] 193 | for valb in b_val: 194 | rects.append(self.can.create_rectangle(hw, vi, 195 | hw + 10 + valb * (hw - 100), vi + wh / hm, 196 | fill=self.set_color(valb), outline="")) 197 | vi += wh / hm 198 | 199 | def launch(self): 200 | while self.i < len(self.cmds): 201 | if self.speed != 0: 202 | while self.i < len(self.cmds): 203 | self.listbox.activate(self.i) 204 | self.can.delete("all") 205 | self.pile_a, self.pile_b = \ 206 | self.launch_cmds(self.cmds[self.i]) 207 | self.draw_rectangles() 208 | time.sleep(2 * self.speed) 209 | self.can.update() 210 | self.listbox.yview_scroll(1, 'units') 211 | self.i += 1 212 | if self.speed == 0: 213 | break 214 | time.sleep(0.25) 215 | self.can.update() 216 | self.PauseCtl.config(text='>') 217 | 218 | 219 | root = Tk() 220 | root.resizable(width=False, height=False) 221 | gui = PsGui(root) 222 | root.mainloop() 223 | --------------------------------------------------------------------------------