├── .gitignore ├── illustrations ├── 42.mp4 ├── 100.mp4 ├── 1000.jpg ├── 500.mp4 ├── rev_b.jpg ├── 1000rev.mp4 ├── rev_mid.jpg ├── rev_third.jpg └── rev_third_2.jpg ├── lib ├── libft │ ├── src │ │ ├── memory │ │ │ ├── ft_bzero.c │ │ │ ├── ft_memset.c │ │ │ ├── ft_calloc.c │ │ │ ├── ft_memchr.c │ │ │ ├── ft_memcpy.c │ │ │ ├── ft_memcmp.c │ │ │ └── ft_memmove.c │ │ ├── string │ │ │ ├── ft_isascii.c │ │ │ ├── ft_isprint.c │ │ │ ├── ft_isdigit.c │ │ │ ├── ft_isalnum.c │ │ │ ├── ft_isalpha.c │ │ │ ├── ft_strlen.c │ │ │ ├── ft_strchr.c │ │ │ ├── ft_strcmp.c │ │ │ ├── ft_striteri.c │ │ │ ├── ft_strrchr.c │ │ │ ├── ft_strdup.c │ │ │ ├── ft_strncmp.c │ │ │ ├── ft_strlcpy.c │ │ │ ├── ft_strjoin.c │ │ │ ├── ft_strmapi.c │ │ │ ├── ft_strlcat.c │ │ │ ├── ft_substr.c │ │ │ ├── ft_strtrim.c │ │ │ ├── ft_strnstr.c │ │ │ └── ft_split.c │ │ ├── output │ │ │ ├── ft_putchar_fd.c │ │ │ ├── ft_putstr_fd.c │ │ │ ├── ft_putendl_fd.c │ │ │ └── ft_putnbr_fd.c │ │ ├── conversion │ │ │ ├── ft_tolower.c │ │ │ ├── ft_toupper.c │ │ │ ├── ft_atoi.c │ │ │ └── ft_itoa.c │ │ └── list │ │ │ ├── ft_lstlast.c │ │ │ ├── ft_lstdelone.c │ │ │ ├── ft_lstadd_front.c │ │ │ ├── ft_lstiter.c │ │ │ ├── ft_lstsize.c │ │ │ ├── ft_lstadd_back.c │ │ │ ├── ft_lstnew.c │ │ │ ├── ft_lstclear.c │ │ │ └── ft_lstmap.c │ ├── Makefile │ └── include │ │ └── libft.h └── ft_printf │ ├── include │ └── ft_printf.h │ ├── Makefile │ └── src │ ├── ft_printf.c │ └── ft_print_nbr.c ├── src ├── push_swap │ ├── main.c │ ├── opti_to_the_top.c │ ├── opti_post_sort.c │ ├── sort.c │ ├── chunk_sort.c │ ├── chunk_utils.c │ ├── opti_sort_asap.c │ ├── move.c │ ├── opti_easy_sort.c │ ├── opti_post_sort_utils.c │ ├── chunk_split.c │ └── opti_sort_three.c ├── stack │ ├── op_push.c │ ├── op_swap.c │ ├── op_rotate.c │ ├── op_reverse_rotate.c │ ├── op_utils.c │ ├── stack_utils.c │ ├── data_mngt.c │ └── stack_init.c └── checker_bonus │ ├── checker_bonus.c │ └── checker_utils_bonus.c ├── include ├── checker_bonus.h ├── stack.h └── push_swap.h ├── README.md ├── Makefile └── push_swap_docs.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | build/ 3 | lib/libft/obj/ 4 | *.a 5 | *.o 6 | push_swap 7 | -------------------------------------------------------------------------------- /illustrations/42.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ulsgks/push_swap/HEAD/illustrations/42.mp4 -------------------------------------------------------------------------------- /illustrations/100.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ulsgks/push_swap/HEAD/illustrations/100.mp4 -------------------------------------------------------------------------------- /illustrations/1000.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ulsgks/push_swap/HEAD/illustrations/1000.jpg -------------------------------------------------------------------------------- /illustrations/500.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ulsgks/push_swap/HEAD/illustrations/500.mp4 -------------------------------------------------------------------------------- /illustrations/rev_b.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ulsgks/push_swap/HEAD/illustrations/rev_b.jpg -------------------------------------------------------------------------------- /illustrations/1000rev.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ulsgks/push_swap/HEAD/illustrations/1000rev.mp4 -------------------------------------------------------------------------------- /illustrations/rev_mid.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ulsgks/push_swap/HEAD/illustrations/rev_mid.jpg -------------------------------------------------------------------------------- /illustrations/rev_third.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ulsgks/push_swap/HEAD/illustrations/rev_third.jpg -------------------------------------------------------------------------------- /illustrations/rev_third_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ulsgks/push_swap/HEAD/illustrations/rev_third_2.jpg -------------------------------------------------------------------------------- /lib/libft/src/memory/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:22:35 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/27 14:58:50 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_bzero(void *s, size_t n) 16 | { 17 | ft_memset(s, 0, n); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:23:01 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/10 23:23:03 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isascii(int c) 16 | { 17 | return (c >= 0 && c <= 127); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:23:10 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/10 23:23:14 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isprint(int c) 16 | { 17 | return (c >= 32 && c <= 126); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/src/output/ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:24:03 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/10 23:39:49 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar_fd(char c, int fd) 16 | { 17 | write(fd, &c, 1); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:23:04 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/19 19:10:33 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isdigit(int c) 16 | { 17 | return (c >= '0' && c <= '9'); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:22:52 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/10 23:39:11 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalnum(int c) 16 | { 17 | return (ft_isalpha(c) || ft_isdigit(c)); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:22:57 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/10 23:39:15 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalpha(int c) 16 | { 17 | return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/src/conversion/ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:26:02 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/10 23:26:04 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_tolower(int c) 16 | { 17 | if (c >= 'A' && c <= 'Z') 18 | return (c + 32); 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /lib/libft/src/conversion/ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:26:07 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/10 23:26:10 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_toupper(int c) 16 | { 17 | if (c >= 'a' && c <= 'z') 18 | return (c - 32); 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /lib/libft/src/output/ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:24:15 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/11 15:47:57 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr_fd(char *s, int fd) 16 | { 17 | if (!s) 18 | return ; 19 | write(fd, s, ft_strlen(s)); 20 | } 21 | -------------------------------------------------------------------------------- /lib/libft/src/list/ft_lstlast.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstlast.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/13 17:55:54 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/13 20:06:30 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstlast(t_list *lst) 16 | { 17 | while (lst && lst->next) 18 | lst = lst->next; 19 | return (lst); 20 | } 21 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:25:20 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/04 19:08:48 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlen(const char *s) 16 | { 17 | size_t len; 18 | 19 | len = 0; 20 | while (s[len]) 21 | len++; 22 | return (len); 23 | } 24 | -------------------------------------------------------------------------------- /lib/libft/src/output/ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:24:07 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/11 15:48:01 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl_fd(char *s, int fd) 16 | { 17 | if (!s) 18 | return ; 19 | write(fd, s, ft_strlen(s)); 20 | write(fd, "\n", 1); 21 | } 22 | -------------------------------------------------------------------------------- /lib/libft/src/list/ft_lstdelone.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/13 18:06:55 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/11 15:46:42 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdelone(t_list *lst, void (*del)(void *)) 16 | { 17 | if (!lst || !del) 18 | return ; 19 | del(lst->content); 20 | free(lst); 21 | } 22 | -------------------------------------------------------------------------------- /lib/libft/src/list/ft_lstadd_front.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_front.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/13 15:52:38 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/22 18:31:36 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_front(t_list **lst, t_list *new) 16 | { 17 | new->next = *lst; 18 | if (*lst != NULL) 19 | (*lst)->prev = new; 20 | *lst = new; 21 | } 22 | -------------------------------------------------------------------------------- /lib/libft/src/list/ft_lstiter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/13 19:36:55 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/12 12:12:38 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstiter(t_list *lst, void (*f)(void *)) 16 | { 17 | if (!f) 18 | return ; 19 | while (lst) 20 | { 21 | f(lst->content); 22 | lst = lst->next; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /lib/libft/src/list/ft_lstsize.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstsize.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/13 15:57:40 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/07 15:18:08 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_lstsize(t_list *lst) 16 | { 17 | int size; 18 | 19 | size = 0; 20 | while (lst) 21 | { 22 | size++; 23 | lst = lst->next; 24 | } 25 | return (size); 26 | } 27 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:24:26 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/03 14:47:58 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strchr(const char *s, int c) 16 | { 17 | while (*s && *s != (char)c) 18 | s++; 19 | if (*s == (char)c || !c) 20 | return ((char *)s); 21 | return (NULL); 22 | } 23 | -------------------------------------------------------------------------------- /lib/libft/src/memory/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:23:59 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/18 19:06:07 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memset(void *b, int c, size_t len) 16 | { 17 | unsigned char *ptr; 18 | 19 | ptr = (unsigned char *)b; 20 | while (len--) 21 | *ptr++ = (unsigned char)c; 22 | return (b); 23 | } 24 | -------------------------------------------------------------------------------- /src/push_swap/main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 21:08:24 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/27 21:08:26 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | int main(int argc, char *argv[]) 16 | { 17 | t_ps data; 18 | 19 | init_data(&data, argc, argv, true); 20 | sort(&data); 21 | print_operations(data.op_list); 22 | free_data(&data); 23 | exit(EXIT_SUCCESS); 24 | } 25 | -------------------------------------------------------------------------------- /lib/libft/src/memory/ft_calloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_calloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:22:38 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/28 17:03:45 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_calloc(size_t count, size_t size) 16 | { 17 | void *ptr; 18 | 19 | ptr = malloc(size * count); 20 | if (!ptr) 21 | return (NULL); 22 | ft_bzero(ptr, (count * size)); 23 | return (ptr); 24 | } 25 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_strcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/24 00:11:21 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/24 00:11:50 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strcmp(const char *s1, const char *s2) 16 | { 17 | while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2) 18 | { 19 | s1++; 20 | s2++; 21 | } 22 | return ((unsigned char)*s1 - (unsigned char)*s2); 23 | } 24 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_striteri.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striteri.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:24:36 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/11 15:45:45 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_striteri(char *s, void (*f)(unsigned int, char *)) 16 | { 17 | int i; 18 | 19 | if (!s || !f) 20 | return ; 21 | i = 0; 22 | while (s[i]) 23 | { 24 | f(i, &s[i]); 25 | i++; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:25:48 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/10 23:25:50 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strrchr(const char *s, int c) 16 | { 17 | int i; 18 | 19 | i = (int)ft_strlen(s) + 1; 20 | while (i--) 21 | if (*(s + i) == (char)c) 22 | return ((char *)s + i); 23 | return (NULL); 24 | } 25 | -------------------------------------------------------------------------------- /lib/libft/src/list/ft_lstadd_back.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_back.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/13 18:03:27 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/22 18:33:46 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_back(t_list **lst, t_list *new) 16 | { 17 | t_list *tmp; 18 | 19 | if (!*lst) 20 | *lst = new; 21 | else 22 | { 23 | tmp = ft_lstlast(*lst); 24 | tmp->next = new; 25 | new->prev = tmp; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /lib/libft/src/list/ft_lstnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/13 15:37:16 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/28 17:03:25 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstnew(void *content) 16 | { 17 | t_list *head; 18 | 19 | head = malloc(sizeof(t_list)); 20 | if (!head) 21 | return (NULL); 22 | head->content = content; 23 | head->next = NULL; 24 | head->prev = NULL; 25 | return (head); 26 | } 27 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:24:30 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/28 17:03:13 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strdup(const char *s1) 16 | { 17 | size_t len; 18 | char *dup; 19 | 20 | len = ft_strlen(s1) + 1; 21 | dup = malloc(sizeof(char) * (len)); 22 | if (!dup) 23 | return (NULL); 24 | ft_memcpy(dup, s1, len); 25 | return (dup); 26 | } 27 | -------------------------------------------------------------------------------- /lib/libft/src/list/ft_lstclear.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstclear.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/13 18:09:59 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/11 15:46:48 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstclear(t_list **lst, void (*del)(void *)) 16 | { 17 | t_list *buffer; 18 | 19 | if (!lst || !*lst || !del) 20 | return ; 21 | while (*lst) 22 | { 23 | buffer = (*lst)->next; 24 | ft_lstdelone(*lst, del); 25 | *lst = buffer; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /lib/libft/src/memory/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:23:38 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/12 21:42:04 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memchr(const void *s, int c, size_t n) 16 | { 17 | const unsigned char *ptr; 18 | 19 | ptr = (unsigned char *)s; 20 | while (n--) 21 | { 22 | if (*ptr == (unsigned char)c) 23 | return ((void *)ptr); 24 | ptr++; 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:25:27 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/11 00:16:45 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | 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] != '\0' && s1[i] == s2[i] && i < n - 1) 23 | i++; 24 | return ((unsigned char)s1[i] - (unsigned char)s2[i]); 25 | } 26 | -------------------------------------------------------------------------------- /src/push_swap/opti_to_the_top.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* opti_to_the_top.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 21:08:20 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/28 18:45:14 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void chunk_to_the_top(t_ps *data, t_chunk *to_sort) 16 | { 17 | if (to_sort->loc == BOTTOM_B && current_size(&data->b) == to_sort->size) 18 | to_sort->loc = TOP_B; 19 | if (to_sort->loc == BOTTOM_A && current_size(&data->a) == to_sort->size) 20 | to_sort->loc = TOP_A; 21 | } 22 | -------------------------------------------------------------------------------- /lib/libft/src/memory/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:23:49 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/12 21:25:21 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memcpy(void *dst, const void *src, size_t n) 16 | { 17 | unsigned char *d; 18 | unsigned char *s; 19 | 20 | if (!dst && !src) 21 | return (NULL); 22 | d = (unsigned char *)dst; 23 | s = (unsigned char *)src; 24 | while (n--) 25 | *d++ = *s++; 26 | return (dst); 27 | } 28 | -------------------------------------------------------------------------------- /lib/libft/src/memory/ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:23:44 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/05 13:21:30 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_memcmp(const void *s1, const void *s2, size_t n) 16 | { 17 | const unsigned char *p1; 18 | const unsigned char *p2; 19 | 20 | p1 = (const unsigned char *)s1; 21 | p2 = (const unsigned char *)s2; 22 | while (n-- > 0) 23 | { 24 | if (*p1 != *p2) 25 | return (*p1 - *p2); 26 | p1++; 27 | p2++; 28 | } 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_strlcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:25:17 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/11 00:14:33 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize) 16 | { 17 | size_t src_len; 18 | size_t i; 19 | 20 | src_len = ft_strlen(src); 21 | i = 0; 22 | if (dstsize > 0) 23 | { 24 | while (src[i] && i < dstsize - 1) 25 | { 26 | dst[i] = src[i]; 27 | i++; 28 | } 29 | dst[i] = '\0'; 30 | } 31 | return (src_len); 32 | } 33 | -------------------------------------------------------------------------------- /lib/libft/src/conversion/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:22:22 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/07 15:22:58 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_atoi(const char *str) 16 | { 17 | int sign; 18 | int value; 19 | 20 | sign = 1; 21 | value = 0; 22 | while (*str == ' ' || (*str >= '\t' && *str <= '\r')) 23 | str++; 24 | if (*str == '-' || *str == '+') 25 | sign = 44 - *str++; 26 | while (ft_isdigit(*str)) 27 | value = value * 10 + (*str++ - '0'); 28 | return (sign * value); 29 | } 30 | -------------------------------------------------------------------------------- /lib/libft/src/output/ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:24:11 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/13 15:13:51 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr_fd(int n, int fd) 16 | { 17 | if (n == -2147483648) 18 | { 19 | ft_putnbr_fd(n / 10, fd); 20 | ft_putstr_fd("8", fd); 21 | } 22 | else if (n < 0) 23 | { 24 | ft_putchar_fd('-', fd); 25 | ft_putnbr_fd(-n, fd); 26 | } 27 | else 28 | { 29 | if (n > 9) 30 | ft_putnbr_fd(n / 10, fd); 31 | ft_putchar_fd(n % 10 + '0', fd); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /include/checker_bonus.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* checker_bonus.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 20:55:38 by ugerkens #+# #+# */ 9 | /* Updated: 2025/01/22 17:07:42 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef CHECKER_BONUS_H 14 | # define CHECKER_BONUS_H 15 | 16 | # include "stack.h" 17 | 18 | // MAIN CHECKER 19 | int main(int argc, char *argv[]); 20 | void read_op(t_ps *data); 21 | bool test_sort(t_ps *data); 22 | 23 | // CHECK SORT 24 | void error_read_op(t_ps *data, char *line); 25 | int get_next_line_ps(int fd, char *line); 26 | t_op string_to_op(const char *str); 27 | void call_op(t_ps *data, t_op op); 28 | 29 | #endif -------------------------------------------------------------------------------- /lib/libft/src/memory/ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:23:54 by ugerkens #+# #+# */ 9 | /* Updated: 2023/03/12 21:25:26 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memmove(void *dst, const void *src, size_t len) 16 | { 17 | unsigned char *d; 18 | unsigned char *s; 19 | 20 | if (!dst && !src) 21 | return (NULL); 22 | d = (unsigned char *)dst; 23 | s = (unsigned char *)src; 24 | if (s < d && d < s + len) 25 | while (len--) 26 | d[len] = s[len]; 27 | else 28 | while (len--) 29 | *(d++) = *(s++); 30 | return (dst); 31 | } 32 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:24:40 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/28 17:03:11 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoin(char const *s1, char const *s2) 16 | { 17 | char *result; 18 | size_t len1; 19 | size_t len2; 20 | 21 | if (!s1 || !s2) 22 | return (NULL); 23 | len1 = ft_strlen(s1); 24 | len2 = ft_strlen(s2); 25 | result = malloc(sizeof(char) * (len1 + len2 + 1)); 26 | if (!result) 27 | return (NULL); 28 | ft_memcpy(result, s1, len1); 29 | ft_memcpy(result + len1, s2, len2 + 1); 30 | return (result); 31 | } 32 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:25:24 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/28 17:03:08 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 16 | { 17 | size_t len; 18 | char *result; 19 | int i; 20 | 21 | if (!s || !f) 22 | return (NULL); 23 | i = 0; 24 | len = ft_strlen(s); 25 | result = malloc(sizeof(char) * (len + 1)); 26 | if (!result) 27 | return (NULL); 28 | while (s[i]) 29 | { 30 | result[i] = f(i, s[i]); 31 | i++; 32 | } 33 | result[i] = '\0'; 34 | return (result); 35 | } 36 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:25:00 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/11 14:40:12 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize) 16 | { 17 | size_t dst_len; 18 | size_t src_len; 19 | 20 | src_len = ft_strlen(src); 21 | if (dstsize == 0) 22 | return (src_len); 23 | dst_len = ft_strlen(dst); 24 | if (dst_len >= dstsize) 25 | return (src_len + dstsize); 26 | dst += dst_len; 27 | dstsize -= dst_len; 28 | while (*src && --dstsize) 29 | *dst++ = *src++; 30 | *dst = '\0'; 31 | return (dst_len + src_len); 32 | } 33 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_substr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_substr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:25:58 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/28 17:03:03 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_substr(char const *s, unsigned int start, size_t len) 16 | { 17 | char *substr; 18 | size_t slen; 19 | 20 | if (!s) 21 | return (NULL); 22 | slen = ft_strlen(s); 23 | if (start >= slen) 24 | return (ft_strdup("")); 25 | if (len > slen - start) 26 | len = slen - start; 27 | substr = malloc(sizeof(char) * (len + 1)); 28 | if (!substr) 29 | return (NULL); 30 | ft_memcpy(substr, s + start, len); 31 | substr[len] = '\0'; 32 | return (substr); 33 | } 34 | -------------------------------------------------------------------------------- /lib/ft_printf/include/ft_printf.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/29 15:27:57 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/19 15:44:35 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef FT_PRINTF_H 14 | # define FT_PRINTF_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include 20 | 21 | int ft_printf(const char *str, ...); 22 | int ft_printformat(va_list *ap, char format); 23 | int ft_printchar(char c); 24 | int ft_printstr(char *str); 25 | int ft_printptr(void *ptr); 26 | int ft_printptr_addr(uintptr_t n, const char format); 27 | int ft_printint(int n); 28 | int ft_printunsigned(unsigned int n); 29 | int ft_printhex(unsigned int n, const char format); 30 | 31 | #endif -------------------------------------------------------------------------------- /lib/libft/src/string/ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:25:54 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/11 15:25:31 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strtrim(char const *s1, char const *set) 16 | { 17 | char *trimmed; 18 | size_t start; 19 | size_t end; 20 | 21 | if (!s1 || !set) 22 | return (NULL); 23 | start = 0; 24 | end = ft_strlen(s1) - 1; 25 | while (s1[start] && ft_strchr(set, s1[start])) 26 | start++; 27 | if (start == end + 1) 28 | return (ft_strdup("")); 29 | while (ft_strchr(set, s1[end])) 30 | end--; 31 | trimmed = ft_substr(s1, start, end - start + 1); 32 | if (!trimmed) 33 | return (NULL); 34 | return (trimmed); 35 | } 36 | -------------------------------------------------------------------------------- /lib/libft/src/list/ft_lstmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/13 19:47:39 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/12 12:12:04 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) 16 | { 17 | t_list *new_lst; 18 | t_list *node; 19 | void *mapped_content; 20 | 21 | if (!lst || !f || !del) 22 | return (NULL); 23 | new_lst = NULL; 24 | while (lst) 25 | { 26 | mapped_content = f(lst->content); 27 | node = ft_lstnew(mapped_content); 28 | if (!node) 29 | { 30 | del(mapped_content); 31 | ft_lstclear(&new_lst, del); 32 | return (NULL); 33 | } 34 | ft_lstadd_back(&new_lst, node); 35 | lst = lst->next; 36 | } 37 | return (new_lst); 38 | } 39 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:25:33 by ugerkens #+# #+# */ 9 | /* Updated: 2023/04/05 13:45:12 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnstr(const char *haystack, const char *needle, size_t len) 16 | { 17 | size_t needle_len; 18 | size_t haystack_len; 19 | 20 | if (!*needle) 21 | return ((char *)haystack); 22 | if (len == 0) 23 | return (NULL); 24 | needle_len = ft_strlen(needle); 25 | haystack_len = ft_strlen(haystack); 26 | if (len > haystack_len) 27 | len = haystack_len; 28 | while (len >= needle_len) 29 | { 30 | if (ft_strncmp(haystack, needle, needle_len) == 0) 31 | return ((char *)haystack); 32 | haystack++; 33 | len--; 34 | } 35 | return (NULL); 36 | } 37 | -------------------------------------------------------------------------------- /src/stack/op_push.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* op_push.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 21:08:41 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/27 21:08:44 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "stack.h" 14 | 15 | void push(t_stack *src, t_stack *dest) 16 | { 17 | int dest_i; 18 | 19 | if (is_full(dest)) 20 | return ; 21 | dest_i = next_up(dest, dest->top); 22 | dest->stack[dest_i] = src->stack[src->top]; 23 | dest->top = dest_i; 24 | src->stack[src->top] = 0; 25 | src->top = next_down(src, src->top); 26 | } 27 | 28 | void push_a(t_ps *data) 29 | { 30 | push(&data->b, &data->a); 31 | if (data->writing_mode) 32 | save_op(data, pa); 33 | } 34 | 35 | void push_b(t_ps *data) 36 | { 37 | push(&data->a, &data->b); 38 | if (data->writing_mode) 39 | save_op(data, pb); 40 | } 41 | -------------------------------------------------------------------------------- /lib/ft_printf/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: ugerkens +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2023/03/22 17:56:24 by ugerkens #+# #+# # 9 | # Updated: 2023/07/27 21:04:46 by ugerkens ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | # Variables 14 | NAME = ft_printf.a 15 | CC = gcc 16 | RM = rm -rf 17 | CFLAGS = -Wall -Wextra -Werror -Isrc -Iinclude 18 | 19 | # Paths and Files 20 | VPATH = src 21 | SRCS = ft_printf.c ft_print_nbr.c 22 | 23 | OBJS = $(addprefix obj/, $(SRCS:.c=.o)) 24 | 25 | # Rules 26 | $(NAME): $(OBJS) 27 | ar rcs $(NAME) $(OBJS) 28 | 29 | all: $(NAME) 30 | 31 | obj: 32 | mkdir -p obj 33 | 34 | obj/%.o: %.c | obj 35 | $(CC) $(CFLAGS) -o $@ -c $< 36 | 37 | clean: 38 | $(RM) obj 39 | 40 | fclean: clean 41 | $(RM) $(NAME) 42 | 43 | re: fclean all 44 | 45 | .SILENT: 46 | 47 | .PHONY: all clean fclean re 48 | -------------------------------------------------------------------------------- /src/stack/op_swap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* op_swap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 21:08:57 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/27 21:09:00 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "stack.h" 14 | 15 | void swap(t_stack *stk) 16 | { 17 | int tmp; 18 | 19 | if (stk->stack[next_down(stk, stk->top)] == 0) 20 | return ; 21 | tmp = stk->stack[next_down(stk, stk->top)]; 22 | stk->stack[next_down(stk, stk->top)] = stk->stack[stk->top]; 23 | stk->stack[stk->top] = tmp; 24 | } 25 | 26 | void swap_a(t_ps *data) 27 | { 28 | swap(&data->a); 29 | if (data->writing_mode) 30 | save_op(data, sa); 31 | } 32 | 33 | void swap_b(t_ps *data) 34 | { 35 | swap(&data->b); 36 | if (data->writing_mode) 37 | save_op(data, sb); 38 | } 39 | 40 | void swap_ab(t_ps *data) 41 | { 42 | swap(&data->a); 43 | swap(&data->b); 44 | if (data->writing_mode) 45 | save_op(data, ss); 46 | } 47 | -------------------------------------------------------------------------------- /src/stack/op_rotate.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* op_rotate.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 21:08:52 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/27 21:17:29 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "stack.h" 14 | 15 | void rotate(t_stack *stk) 16 | { 17 | if (is_full(stk)) 18 | { 19 | stk->bottom = stk->top; 20 | stk->top = next_down(stk, stk->top); 21 | } 22 | else 23 | { 24 | stk->bottom = next_down(stk, stk->bottom); 25 | stk->stack[stk->bottom] = stk->stack[stk->top]; 26 | stk->stack[stk->top] = 0; 27 | stk->top = next_down(stk, stk->top); 28 | } 29 | } 30 | 31 | void rotate_a(t_ps *data) 32 | { 33 | rotate(&data->a); 34 | if (data->writing_mode) 35 | save_op(data, ra); 36 | } 37 | 38 | void rotate_b(t_ps *data) 39 | { 40 | rotate(&data->b); 41 | if (data->writing_mode) 42 | save_op(data, rb); 43 | } 44 | 45 | void rotate_ab(t_ps *data) 46 | { 47 | rotate(&data->a); 48 | rotate(&data->b); 49 | if (data->writing_mode) 50 | save_op(data, rr); 51 | } 52 | -------------------------------------------------------------------------------- /lib/libft/src/conversion/ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:23:20 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/28 17:03:30 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int int_len(int n) 16 | { 17 | int len; 18 | 19 | len = 0; 20 | if (n == 0) 21 | return (1); 22 | if (n < 0) 23 | len++; 24 | while (n) 25 | { 26 | n /= 10; 27 | len++; 28 | } 29 | return (len); 30 | } 31 | 32 | char *ft_itoa(int n) 33 | { 34 | int ncpy; 35 | int i; 36 | char *str; 37 | 38 | if (n == -2147483648) 39 | return (ft_strdup("-2147483648")); 40 | ncpy = n; 41 | if (n < 0) 42 | ncpy = -n; 43 | i = int_len(n); 44 | str = malloc(sizeof(char) * (i + 1)); 45 | if (!str) 46 | return (NULL); 47 | str[i] = '\0'; 48 | i--; 49 | if (ncpy == 0) 50 | str[i] = '0'; 51 | while (ncpy > 0) 52 | { 53 | str[i--] = (ncpy % 10) + '0'; 54 | ncpy /= 10; 55 | } 56 | if (n < 0) 57 | str[i] = '-'; 58 | return (str); 59 | } 60 | -------------------------------------------------------------------------------- /src/stack/op_reverse_rotate.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* op_reverse_rotate.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 21:08:47 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/27 21:08:50 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "stack.h" 14 | 15 | void r_rotate(t_stack *stk) 16 | { 17 | if (is_full(stk)) 18 | { 19 | stk->top = stk->bottom; 20 | stk->bottom = next_up(stk, stk->bottom); 21 | } 22 | else 23 | { 24 | stk->top = next_up(stk, stk->top); 25 | stk->stack[stk->top] = stk->stack[stk->bottom]; 26 | stk->stack[stk->bottom] = 0; 27 | stk->bottom = next_up(stk, stk->bottom); 28 | } 29 | } 30 | 31 | void r_rotate_a(t_ps *data) 32 | { 33 | r_rotate(&data->a); 34 | if (data->writing_mode) 35 | save_op(data, rra); 36 | } 37 | 38 | void r_rotate_b(t_ps *data) 39 | { 40 | r_rotate(&data->b); 41 | if (data->writing_mode) 42 | save_op(data, rrb); 43 | } 44 | 45 | void r_rotate_ab(t_ps *data) 46 | { 47 | r_rotate(&data->a); 48 | r_rotate(&data->b); 49 | if (data->writing_mode) 50 | save_op(data, rrr); 51 | } 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 |

3 | An Effective Push Swap 4 |

5 |

6 | Did this project help you? Give it a 🌟! 7 |

8 | 9 | ## 📊 General information 10 | Version: 6. Bonus included.
11 | Article about the algorithm of this project: [Push Swap in less than 4200 operations](https://medium.com/@ulysse.gerkens/push-swap-in-less-than-4200-operations-c292f034f6c0).
12 | 13 | ## 🚀 Benchmark 14 | Perform on 10000 random samples of 500 numbers.
15 | Worst = 3871 instructions
16 | Average = 3784 instructions
17 | Best = 3680 instructions
18 | Standard deviation = 27.0 instructions
19 | 20 | ## ⚙️ Implementation details 21 | Cf. [/push_swap_docs.md](https://github.com/ulyssegerkens/push_swap/blob/main/push_swap_docs.md): Stacks data structure; Error management; Inputs format.
22 | 23 | 24 | ## 🛠️ Useful tools 25 | - [/SimonCROS/push_swap_tester: Simple and effective tester](https://github.com/SimonCROS/push_swap_tester) 26 | - [/Niimphu/push_swap_visualiser: Beautiful visualizer](https://github.com/Niimphu/push_swap_visualiser) 27 | - [/peufo/push_swap_ui: Project index, visualizer, stats](https://push.peuf.ch/) 28 | 29 | 30 | ## 🧬 Program Flowchart 31 | [/illustrations/push_swap_flowchart.svg](https://github.com/ulyssegerkens/push_swap/blob/main/illustrations/push_swap_flowchart.svg) 32 | 33 | ## 📋 Testing 34 | Tested by 42 students (125%) and mentioned as an outstanding project 🎉. 35 | 36 | ## ✨ Illustrations 37 | https://github.com/ulyssegerkens/push_swap/assets/99326326/77c80f0d-5759-4849-b321-d4f5506d3351 38 | 39 | https://github.com/ulyssegerkens/push_swap/assets/99326326/71651fc8-4528-46b0-ae41-477ff0b505a7 40 | 41 | https://github.com/ulyssegerkens/push_swap/assets/99326326/5a501e82-7e0f-488f-b115-a1579171d245 42 | 43 | https://github.com/ulyssegerkens/push_swap/assets/99326326/a061b35d-aa0b-4143-9ce4-b3d8b496eb96 44 | -------------------------------------------------------------------------------- /src/stack/op_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* op_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 21:09:02 by ugerkens #+# #+# */ 9 | /* Updated: 2025/01/22 17:04:33 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "stack.h" 14 | 15 | void save_op(t_ps *data, t_op op) 16 | { 17 | t_list *new; 18 | 19 | new = ft_lstnew((void *)(uintptr_t)op); 20 | if (!new) 21 | error(data); 22 | ft_lstadd_back(&data->op_list, new); 23 | } 24 | 25 | void print_operations(t_list *head) 26 | { 27 | t_list *reader; 28 | 29 | reader = head; 30 | while (reader) 31 | { 32 | ft_printf("%s\n", op_to_string(op_from(reader))); 33 | reader = reader->next; 34 | } 35 | } 36 | 37 | const char *op_to_string(t_op op) 38 | { 39 | static const char *strings[12]; 40 | 41 | strings[0] = "null_op"; 42 | strings[1] = "pa"; 43 | strings[2] = "pb"; 44 | strings[3] = "ra"; 45 | strings[4] = "rb"; 46 | strings[5] = "rr"; 47 | strings[6] = "rra"; 48 | strings[7] = "rrb"; 49 | strings[8] = "rrr"; 50 | strings[9] = "sa"; 51 | strings[10] = "sb"; 52 | strings[11] = "ss"; 53 | return (strings[op]); 54 | } 55 | 56 | t_op op_from(t_list *node) 57 | { 58 | return ((t_op)(uintptr_t)node->content); 59 | } 60 | -------------------------------------------------------------------------------- /src/stack/stack_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* stack_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 21:09:14 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/28 22:00:33 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "stack.h" 14 | 15 | int next_up(t_stack *stk, int index) 16 | { 17 | if (current_size(stk) == 0) 18 | return (index); 19 | if (index == 0) 20 | return (stk->size - 1); 21 | else 22 | return (index - 1); 23 | } 24 | 25 | int next_down(t_stack *stk, int index) 26 | { 27 | if (current_size(stk) == 0) 28 | return (index); 29 | if (index == stk->size - 1) 30 | return (0); 31 | else 32 | return (index + 1); 33 | } 34 | 35 | int value(t_stack *stk, int n) 36 | { 37 | int i; 38 | 39 | i = stk->top; 40 | while (--n > 0) 41 | i = next_down(stk, i); 42 | return (stk->stack[i]); 43 | } 44 | 45 | int current_size(t_stack *stk) 46 | { 47 | if (stk->top == stk->bottom && stk->stack[stk->top] == 0) 48 | return (0); 49 | if (stk->top > stk->bottom) 50 | return ((stk->size - stk->top) + (stk->bottom + 1)); 51 | else 52 | return (stk->bottom - stk->top + 1); 53 | } 54 | 55 | bool is_full(t_stack *stk) 56 | { 57 | return (stk->size == current_size(stk)); 58 | } 59 | -------------------------------------------------------------------------------- /lib/libft/src/string/ft_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/10 23:24:21 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/28 17:03:16 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int ft_wordlen(char const *s, char c) 16 | { 17 | int len; 18 | 19 | len = 0; 20 | while (*s && *s != c) 21 | { 22 | len++; 23 | s++; 24 | } 25 | return (len); 26 | } 27 | 28 | static int ft_countwords(char const *s, char c) 29 | { 30 | int count; 31 | 32 | count = 0; 33 | while (*s) 34 | { 35 | if (*s != c) 36 | { 37 | count++; 38 | s += ft_wordlen(s, c); 39 | } 40 | else 41 | s++; 42 | } 43 | return (count); 44 | } 45 | 46 | static void *free_strs(char **strs) 47 | { 48 | int i; 49 | 50 | i = 0; 51 | while (strs[i]) 52 | free(strs[i++]); 53 | free(strs); 54 | return (NULL); 55 | } 56 | 57 | char **ft_split(char const *s, char c) 58 | { 59 | char **strs; 60 | int i; 61 | int count; 62 | 63 | if (!s) 64 | return (NULL); 65 | count = ft_countwords(s, c); 66 | strs = malloc(sizeof(char *) * (count + 1)); 67 | if (!strs) 68 | return (NULL); 69 | strs[count] = NULL; 70 | i = 0; 71 | while (*s) 72 | { 73 | if (*s != c) 74 | { 75 | strs[i] = ft_substr(s, 0, ft_wordlen(s, c)); 76 | if (!strs[i++]) 77 | return (free_strs(strs)); 78 | s += ft_wordlen(s, c); 79 | } 80 | else 81 | s++; 82 | } 83 | return (strs); 84 | } 85 | -------------------------------------------------------------------------------- /src/push_swap/opti_post_sort.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* opti_post_sort.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 20:58:19 by ugerkens #+# #+# */ 9 | /* Updated: 2025/01/22 17:04:33 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void post_sort_optimization(t_ps *data) 16 | { 17 | if (!data->op_list) 18 | return ; 19 | eliminate_neutral_op(data->op_list); 20 | merge_op(data->op_list); 21 | } 22 | 23 | void eliminate_neutral_op(t_list *op_list) 24 | { 25 | t_list *ref; 26 | t_list *cmp; 27 | t_op op_neutral; 28 | 29 | ref = op_list->next; 30 | while (ref && ref->next) 31 | { 32 | op_neutral = neutral_op(op_from(ref)); 33 | if (op_neutral) 34 | { 35 | cmp = ref->next; 36 | while (!op_on_same_stack(op_from(ref), op_from(cmp)) 37 | && op_from(cmp) != op_neutral && cmp->next) 38 | cmp = cmp->next; 39 | if (ref->prev && op_from(cmp) == op_neutral) 40 | { 41 | ref = ref->prev; 42 | remove_op(ref->next); 43 | remove_op(cmp); 44 | continue ; 45 | } 46 | } 47 | ref = ref->next; 48 | } 49 | } 50 | 51 | void merge_op(t_list *op_list) 52 | { 53 | t_list *reader; 54 | t_op child; 55 | 56 | reader = op_list; 57 | while (reader && reader->next) 58 | { 59 | child = child_op(op_from(reader), op_from(reader->next)); 60 | if (child) 61 | { 62 | remove_op(reader->next); 63 | reader->content = (void *)(uintptr_t)child; 64 | } 65 | reader = reader->next; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/stack/data_mngt.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* data_mngt.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 21:08:35 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/28 23:17:40 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "stack.h" 14 | 15 | void init_data(t_ps *data, int argc, char **argv, bool writing_mode) 16 | { 17 | --argc; 18 | init_stack(data, &data->a, argc); 19 | init_stack(data, &data->b, argc); 20 | fill_stack(data, &data->a, argc, ++argv); 21 | data->writing_mode = writing_mode; 22 | data->op_list = NULL; 23 | } 24 | 25 | void init_stack(t_ps *data, t_stack *stk, int size) 26 | { 27 | stk->stack = malloc(sizeof(int) * size); 28 | if (!stk->stack) 29 | error(data); 30 | stk->top = 0; 31 | stk->bottom = 0; 32 | stk->size = size; 33 | ft_memset(stk->stack, 0, sizeof(int) * size); 34 | } 35 | 36 | bool is_sorted(t_ps *data) 37 | { 38 | int i; 39 | int rank; 40 | 41 | i = data->a.top; 42 | rank = 1; 43 | while (rank <= data->a.size) 44 | { 45 | if (data->a.stack[i] != rank) 46 | return (false); 47 | rank++; 48 | i = next_down(&data->a, i); 49 | } 50 | return (true); 51 | } 52 | 53 | void error(t_ps *data) 54 | { 55 | free_data(data); 56 | ft_putendl_fd("Error", 2); 57 | exit(EXIT_FAILURE); 58 | } 59 | 60 | void free_data(t_ps *data) 61 | { 62 | if (data->a.stack) 63 | free(data->a.stack); 64 | if (data->b.stack) 65 | free(data->b.stack); 66 | if (data->op_list) 67 | ft_lstclear(&data->op_list, NULL); 68 | } 69 | -------------------------------------------------------------------------------- /src/checker_bonus/checker_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* checker_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 20:56:49 by ugerkens #+# #+# */ 9 | /* Updated: 2025/01/22 17:04:33 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "checker_bonus.h" 14 | 15 | int main(int argc, char *argv[]) 16 | { 17 | t_ps data; 18 | 19 | init_data(&data, argc, argv, false); 20 | if (data.a.size == 0) 21 | exit(EXIT_SUCCESS); 22 | read_op(&data); 23 | if (test_sort(&data)) 24 | ft_printf("OK\n"); 25 | else 26 | ft_printf("KO\n"); 27 | free_data(&data); 28 | exit(EXIT_SUCCESS); 29 | } 30 | 31 | void read_op(t_ps *data) 32 | { 33 | t_list *new; 34 | int gnl_status; 35 | char *line; 36 | int line_status; 37 | 38 | gnl_status = 0; 39 | line_status = 1; 40 | line = malloc(sizeof(char) * 5); 41 | if (!line) 42 | error_read_op(data, line); 43 | while (line_status) 44 | { 45 | gnl_status = get_next_line_ps(0, line); 46 | if (gnl_status == EXIT_FAILURE) 47 | error_read_op(data, line); 48 | line_status = ft_strlen(line); 49 | if (line_status == 0) 50 | break ; 51 | new = ft_lstnew((void *)(uintptr_t)string_to_op(line)); 52 | if (!new) 53 | error_read_op(data, line); 54 | ft_lstadd_back(&data->op_list, new); 55 | } 56 | free(line); 57 | } 58 | 59 | bool test_sort(t_ps *data) 60 | { 61 | t_list *reader; 62 | 63 | reader = data->op_list; 64 | while (reader) 65 | { 66 | call_op(data, (t_op)(uintptr_t)reader->content); 67 | reader = reader->next; 68 | } 69 | return (is_full(&data->a) && is_sorted(data)); 70 | } 71 | -------------------------------------------------------------------------------- /lib/libft/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: ugerkens +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2023/03/13 20:09:26 by ugerkens #+# #+# # 9 | # Updated: 2023/07/27 21:03:52 by ugerkens ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | # Variables 14 | NAME = libft.a 15 | CC = gcc 16 | CFLAGS = -Wall -Wextra -Werror -Isrc -Iinclude 17 | RM = rm -rf 18 | 19 | # Paths and Files 20 | VPATH = src:src/conversion:src/list:src/memory:src/output:src/string 21 | SRCS = ft_atoi.c ft_itoa.c ft_tolower.c ft_toupper.c \ 22 | ft_lstadd_back.c ft_lstadd_front.c ft_lstclear.c ft_lstdelone.c ft_lstiter.c \ 23 | ft_lstlast.c ft_lstmap.c ft_lstnew.c ft_lstsize.c ft_bzero.c ft_calloc.c \ 24 | ft_memchr.c ft_memcmp.c ft_memcpy.c ft_memmove.c ft_memset.c \ 25 | ft_putchar_fd.c ft_putendl_fd.c ft_putnbr_fd.c ft_putstr_fd.c \ 26 | ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c ft_isprint.c \ 27 | ft_split.c ft_strchr.c ft_strcmp.c ft_strdup.c ft_striteri.c \ 28 | ft_strjoin.c ft_strlcat.c ft_strlcpy.c ft_strlen.c ft_strmapi.c \ 29 | ft_strncmp.c ft_strnstr.c ft_strrchr.c ft_strtrim.c ft_substr.c 30 | 31 | OBJS = $(addprefix obj/,$(SRCS:.c=.o)) 32 | 33 | # Rules 34 | $(NAME): $(OBJS) 35 | ar rcs $(NAME) $(OBJS) 36 | 37 | all: $(NAME) 38 | 39 | obj: 40 | mkdir -p obj 41 | 42 | obj/%.o: %.c | obj 43 | mkdir -p $(dir $@) 44 | $(CC) $(CFLAGS) -c $< -o $@ 45 | 46 | clean: 47 | $(RM) obj 48 | 49 | fclean: clean 50 | $(RM) $(NAME) 51 | 52 | re: fclean all 53 | 54 | .SILENT: 55 | 56 | .PHONY: all clean fclean re 57 | -------------------------------------------------------------------------------- /src/push_swap/sort.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* sort.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 21:08:28 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/30 18:22:12 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void sort(t_ps *data) 16 | { 17 | if (data->a.size <= 1 || is_sorted(data)) 18 | return ; 19 | else if (data->a.size == 3) 20 | sort_three_a(data); 21 | else if (data->a.size == 5) 22 | sort_five_a(data); 23 | else 24 | chunk_sort(data); 25 | post_sort_optimization(data); 26 | } 27 | 28 | void sort_three_a(t_ps *data) 29 | { 30 | int first; 31 | int second; 32 | int third; 33 | 34 | first = value(&data->a, 1); 35 | second = value(&data->a, 2); 36 | third = value(&data->a, 3); 37 | if (first > second && third > second && third > first) 38 | swap_a(data); 39 | else if (first > second && third > second && first > third) 40 | rotate_a(data); 41 | else if (second > first && second > third && first > third) 42 | r_rotate_a(data); 43 | else if (second > first && second > third && third > first) 44 | { 45 | swap_a(data); 46 | rotate_a(data); 47 | } 48 | else if (first > second && second > third && first > third) 49 | { 50 | swap_a(data); 51 | r_rotate_a(data); 52 | } 53 | } 54 | 55 | void sort_five_a(t_ps *data) 56 | { 57 | while (current_size(&data->a) > 3) 58 | { 59 | if (value(&data->a, 1) == 1 || value(&data->a, 1) == 2) 60 | push_b(data); 61 | else 62 | rotate_a(data); 63 | } 64 | if (value(&data->b, 1) < value(&data->b, 2)) 65 | swap_b(data); 66 | sort_three_a(data); 67 | push_a(data); 68 | push_a(data); 69 | } 70 | -------------------------------------------------------------------------------- /src/push_swap/chunk_sort.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* chunk_sort.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 20:56:55 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/30 18:38:01 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void chunk_sort(t_ps *data) 16 | { 17 | t_chunk chunk_all; 18 | 19 | chunk_all.loc = TOP_A; 20 | chunk_all.size = data->a.size; 21 | rec_chunk_sort(data, &chunk_all); 22 | } 23 | 24 | void rec_chunk_sort(t_ps *data, t_chunk *to_sort) 25 | { 26 | t_split_dest dest; 27 | 28 | chunk_to_the_top(data, to_sort); 29 | easy_sort(data, to_sort); 30 | if (to_sort->size <= 3) 31 | { 32 | if (to_sort->size == 3) 33 | sort_three(data, to_sort); 34 | else if (to_sort->size == 2) 35 | sort_two(data, to_sort); 36 | else if (to_sort->size == 1) 37 | sort_one(data, to_sort); 38 | return ; 39 | } 40 | chunk_split(data, to_sort, &dest); 41 | rec_chunk_sort(data, &dest.max); 42 | rec_chunk_sort(data, &dest.mid); 43 | rec_chunk_sort(data, &dest.min); 44 | } 45 | 46 | void sort_two(t_ps *data, t_chunk *to_sort) 47 | { 48 | if (to_sort->loc == BOTTOM_A || to_sort->loc == BOTTOM_B 49 | || to_sort->loc == TOP_B) 50 | { 51 | move_from_to(data, to_sort->loc, TOP_A); 52 | move_from_to(data, to_sort->loc, TOP_A); 53 | } 54 | if (value(&data->a, 1) > value(&data->a, 2)) 55 | swap_a(data); 56 | to_sort->size -= 2; 57 | } 58 | 59 | void sort_one(t_ps *data, t_chunk *to_sort) 60 | { 61 | if (to_sort->loc == BOTTOM_A || to_sort->loc == BOTTOM_B 62 | || to_sort->loc == TOP_B) 63 | move_from_to(data, to_sort->loc, TOP_A); 64 | to_sort->size -= 1; 65 | } 66 | -------------------------------------------------------------------------------- /lib/ft_printf/src/ft_printf.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/21 11:20:38 by ugerkens #+# #+# */ 9 | /* Updated: 2023/06/26 17:39:24 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int ft_printf(const char *str, ...) 16 | { 17 | va_list args; 18 | size_t len; 19 | int i; 20 | 21 | va_start(args, str); 22 | len = 0; 23 | i = 0; 24 | while (str[i]) 25 | { 26 | if (str[i] == '%') 27 | len += ft_printformat(&args, str[++i]); 28 | else 29 | len += ft_printchar(str[i]); 30 | i++; 31 | } 32 | va_end(args); 33 | return (len); 34 | } 35 | 36 | int ft_printformat(va_list *ap, char format) 37 | { 38 | size_t len; 39 | 40 | len = 0; 41 | if (format == 'c') 42 | len += ft_printchar((char)va_arg(*ap, int)); 43 | else if (format == 's') 44 | len += ft_printstr(va_arg(*ap, char *)); 45 | else if (format == 'p') 46 | len += ft_printptr(va_arg(*ap, void *)); 47 | else if (format == 'd' || format == 'i') 48 | len += ft_printint(va_arg(*ap, int)); 49 | else if (format == 'u') 50 | len += ft_printunsigned(va_arg(*ap, unsigned int)); 51 | else if (format == 'x' || format == 'X') 52 | len += ft_printhex((int)va_arg(*ap, unsigned int), format); 53 | else if (format == '%') 54 | len += ft_printchar('%'); 55 | return (len); 56 | } 57 | 58 | int ft_printchar(char c) 59 | { 60 | return ((int)write(1, &c, 1)); 61 | } 62 | 63 | int ft_printstr(char *str) 64 | { 65 | size_t len; 66 | 67 | len = 0; 68 | if (str == NULL) 69 | return (ft_printstr("(null)")); 70 | while (*str) 71 | len += ft_printchar(*str++); 72 | return (len); 73 | } 74 | -------------------------------------------------------------------------------- /src/push_swap/chunk_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* chunk_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 20:58:02 by ugerkens #+# #+# */ 9 | /* Updated: 2025/01/22 17:06:14 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | int chunk_value(t_ps *data, t_chunk *chunk, int n) 16 | { 17 | t_loc loc; 18 | t_stack *stk; 19 | int i; 20 | 21 | loc = chunk->loc; 22 | stk = loc_to_stack(data, loc); 23 | if (loc == TOP_A || loc == TOP_B) 24 | i = stk->top; 25 | else if (loc == BOTTOM_A || loc == BOTTOM_B) 26 | i = stk->bottom; 27 | if (loc == TOP_A || loc == TOP_B) 28 | while (--n > 0) 29 | i = next_down(stk, i); 30 | else if (loc == BOTTOM_A || loc == BOTTOM_B) 31 | while (--n > 0) 32 | i = next_up(stk, i); 33 | return (stk->stack[i]); 34 | } 35 | 36 | int chunk_max_value(t_ps *data, t_chunk *chunk) 37 | { 38 | t_stack *stk; 39 | int size; 40 | int max_value; 41 | int i; 42 | 43 | stk = loc_to_stack(data, chunk->loc); 44 | size = chunk->size; 45 | max_value = 0; 46 | if (chunk->loc == TOP_A || chunk->loc == TOP_B) 47 | i = stk->top; 48 | else if (chunk->loc == BOTTOM_A || chunk->loc == BOTTOM_B) 49 | i = stk->bottom; 50 | while (size--) 51 | { 52 | if (stk->stack[i] > max_value) 53 | max_value = stk->stack[i]; 54 | if (chunk->loc == TOP_A || chunk->loc == TOP_B) 55 | i = next_down(stk, i); 56 | else if (chunk->loc == BOTTOM_A || chunk->loc == BOTTOM_B) 57 | i = next_up(stk, i); 58 | } 59 | return (max_value); 60 | } 61 | 62 | t_stack *loc_to_stack(t_ps *data, t_loc loc) 63 | { 64 | if (loc == TOP_A || loc == BOTTOM_A) 65 | return (&data->a); 66 | else 67 | return (&data->b); 68 | } 69 | -------------------------------------------------------------------------------- /src/push_swap/opti_sort_asap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* opti_sort_asap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 20:58:30 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/28 23:12:37 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void split_max_reduction(t_ps *data, t_chunk *max) 16 | { 17 | t_stack *a; 18 | 19 | a = &data->a; 20 | if (max->loc == TOP_A && max->size == 3 && is_consecutive(value(a, 1), 21 | value(a, 2), value(a, 3), value(a, 4)) && a_partly_sort(data, 4)) 22 | { 23 | sort_three(data, max); 24 | return ; 25 | } 26 | if (max->loc == TOP_A && value(a, 1) == value(a, 3) - 1 27 | && a_partly_sort(data, 3)) 28 | { 29 | swap_a(data); 30 | max->size--; 31 | } 32 | if (max->loc == TOP_A && a_partly_sort(data, 1)) 33 | max->size--; 34 | } 35 | 36 | bool a_partly_sort(t_ps *data, int from) 37 | { 38 | int i; 39 | t_stack *a; 40 | int value; 41 | 42 | a = &data->a; 43 | i = a->top; 44 | while (--from) 45 | i = next_down(a, i); 46 | while (a->stack[i] != data->a.size) 47 | { 48 | value = a->stack[i]; 49 | i = next_down(a, i); 50 | if (a->stack[i] != value + 1) 51 | return (false); 52 | } 53 | return (true); 54 | } 55 | 56 | bool is_consecutive(int a, int b, int c, int d) 57 | { 58 | sort_three_numbers(&a, &b, &c); 59 | return ((b - a == 1) && (c - b == 1) && (d - c == 1)); 60 | } 61 | 62 | void sort_three_numbers(int *a, int *b, int *c) 63 | { 64 | int temp; 65 | 66 | if (*a > *b) 67 | { 68 | temp = *a; 69 | *a = *b; 70 | *b = temp; 71 | } 72 | if (*a > *c) 73 | { 74 | temp = *a; 75 | *a = *c; 76 | *c = temp; 77 | } 78 | if (*b > *c) 79 | { 80 | temp = *b; 81 | *b = *c; 82 | *c = temp; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/push_swap/move.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* move.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 20:58:08 by ugerkens #+# #+# */ 9 | /* Updated: 2025/01/22 17:06:14 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | int move_from_to(t_ps *data, t_loc from, t_loc to) 16 | { 17 | if (from == TOP_A) 18 | move_from_top_a(data, to); 19 | else if (from == TOP_B) 20 | move_from_top_b(data, to); 21 | else if (from == BOTTOM_A) 22 | move_from_bottom_a(data, to); 23 | else if (from == BOTTOM_B) 24 | move_from_bottom_b(data, to); 25 | return (1); 26 | } 27 | 28 | void move_from_top_a(t_ps *data, t_loc to) 29 | { 30 | if (to == TOP_B) 31 | push_b(data); 32 | else if (to == BOTTOM_A) 33 | rotate_a(data); 34 | else if (to == BOTTOM_B) 35 | { 36 | push_b(data); 37 | rotate_b(data); 38 | } 39 | } 40 | 41 | void move_from_top_b(t_ps *data, t_loc to) 42 | { 43 | if (to == TOP_A) 44 | push_a(data); 45 | else if (to == BOTTOM_B) 46 | rotate_b(data); 47 | else if (to == BOTTOM_A) 48 | { 49 | push_a(data); 50 | rotate_a(data); 51 | } 52 | } 53 | 54 | void move_from_bottom_a(t_ps *data, t_loc to) 55 | { 56 | if (to == TOP_A) 57 | r_rotate_a(data); 58 | else if (to == TOP_B) 59 | { 60 | r_rotate_a(data); 61 | push_b(data); 62 | } 63 | else if (to == BOTTOM_B) 64 | { 65 | r_rotate_a(data); 66 | push_b(data); 67 | rotate_b(data); 68 | } 69 | } 70 | 71 | void move_from_bottom_b(t_ps *data, t_loc to) 72 | { 73 | if (to == TOP_B) 74 | r_rotate_b(data); 75 | else if (to == TOP_A) 76 | { 77 | r_rotate_b(data); 78 | push_a(data); 79 | } 80 | else if (to == BOTTOM_A) 81 | { 82 | r_rotate_b(data); 83 | push_a(data); 84 | rotate_a(data); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/push_swap/opti_easy_sort.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* opti_easy_sort.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 20:58:15 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/28 23:13:09 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void easy_sort(t_ps *data, t_chunk *to_sort) 16 | { 17 | while (to_sort->loc != TOP_A && to_sort->size) 18 | { 19 | if (value(&data->a, 1) == chunk_value(data, to_sort, 1) + 1 20 | && to_sort->size > 0) 21 | sort_one(data, to_sort); 22 | else if (value(&data->a, 1) == chunk_value(data, to_sort, 2) + 1 23 | && to_sort->size > 1) 24 | easy_sort_second(data, to_sort); 25 | else 26 | break ; 27 | } 28 | } 29 | 30 | void easy_sort_second(t_ps *data, t_chunk *to_sort) 31 | { 32 | if (to_sort->loc == TOP_B) 33 | handle_top_b(data, to_sort); 34 | else if (to_sort->loc == BOTTOM_A) 35 | handle_bottom_a(data, to_sort); 36 | else if (to_sort->loc == BOTTOM_B) 37 | handle_bottom_b(data, to_sort); 38 | to_sort->size--; 39 | } 40 | 41 | void handle_top_b(t_ps *data, t_chunk *to_sort) 42 | { 43 | swap_b(data); 44 | push_a(data); 45 | if (value(&data->b, 1) == value(&data->a, 1) - 1) 46 | { 47 | push_a(data); 48 | to_sort->size--; 49 | } 50 | } 51 | 52 | void handle_bottom_a(t_ps *data, t_chunk *to_sort) 53 | { 54 | r_rotate_a(data); 55 | r_rotate_a(data); 56 | swap_a(data); 57 | if (value(&data->a, 1) == value(&data->a, 2) - 1) 58 | to_sort->size--; 59 | else 60 | rotate_a(data); 61 | } 62 | 63 | void handle_bottom_b(t_ps *data, t_chunk *to_sort) 64 | { 65 | r_rotate_b(data); 66 | r_rotate_b(data); 67 | push_a(data); 68 | if (value(&data->b, 1) == value(&data->a, 1) - 1) 69 | { 70 | push_a(data); 71 | to_sort->size--; 72 | } 73 | else 74 | rotate_b(data); 75 | } 76 | -------------------------------------------------------------------------------- /lib/ft_printf/src/ft_print_nbr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_print_nbr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/29 15:19:56 by ugerkens #+# #+# */ 9 | /* Updated: 2023/06/13 14:34:50 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int ft_printptr(void *ptr) 16 | { 17 | size_t len; 18 | 19 | len = 0; 20 | len += ft_printstr("0x"); 21 | len += ft_printptr_addr((uintptr_t)ptr, 'x'); 22 | return (len); 23 | } 24 | 25 | int ft_printptr_addr(uintptr_t n, const char format) 26 | { 27 | size_t len; 28 | char ref; 29 | 30 | len = 0; 31 | ref = 'a'; 32 | if (format == 'X') 33 | ref = 'A'; 34 | if (n >= 16) 35 | len += ft_printptr_addr(n / 16, format); 36 | n %= 16; 37 | if (n <= 9) 38 | ft_printchar(n + '0'); 39 | else 40 | ft_printchar(n - 10 + ref); 41 | return (len + 1); 42 | } 43 | 44 | int ft_printint(int n) 45 | { 46 | int len; 47 | 48 | len = 0; 49 | if (n == -2147483648) 50 | return (ft_printstr("-2147483648")); 51 | if (n < 0) 52 | { 53 | len += ft_printchar('-'); 54 | n *= -1; 55 | } 56 | if (n >= 10) 57 | len += ft_printint(n / 10); 58 | len += ft_printchar('0' + (n % 10)); 59 | return (len); 60 | } 61 | 62 | int ft_printunsigned(unsigned int n) 63 | { 64 | int len; 65 | 66 | len = 0; 67 | if (n >= 10) 68 | len += ft_printunsigned(n / 10); 69 | len += ft_printchar('0' + (n % 10)); 70 | return (len); 71 | } 72 | 73 | int ft_printhex(unsigned int n, const char format) 74 | { 75 | size_t len; 76 | char ref; 77 | 78 | len = 0; 79 | ref = 'a'; 80 | if (format == 'X') 81 | ref = 'A'; 82 | if (n >= 16) 83 | len += ft_printptr_addr(n / 16, format); 84 | n %= 16; 85 | if (n <= 9) 86 | ft_printchar(n + '0'); 87 | else 88 | ft_printchar(n - 10 + ref); 89 | return (len + 1); 90 | } 91 | -------------------------------------------------------------------------------- /src/push_swap/opti_post_sort_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* opti_post_sort_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 21:15:00 by ugerkens #+# #+# */ 9 | /* Updated: 2025/01/22 17:04:33 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | t_op neutral_op(t_op op) 16 | { 17 | if (op == pa) 18 | return (pb); 19 | if (op == pb) 20 | return (pa); 21 | if (op == ra) 22 | return (rra); 23 | if (op == rb) 24 | return (rrb); 25 | if (op == rra) 26 | return (ra); 27 | if (op == rrb) 28 | return (rb); 29 | if (op == sa) 30 | return (sa); 31 | if (op == sb) 32 | return (sb); 33 | if (op == rr) 34 | return (rrr); 35 | return (null_op); 36 | } 37 | 38 | bool op_on_same_stack(t_op ref, t_op to_check) 39 | { 40 | if (ref == pa || ref == pb || ref == rr || ref == rrr) 41 | return (true); 42 | if (to_check == pa || to_check == pb || to_check == rr || to_check == rrr) 43 | return (true); 44 | else if (ref == ra || ref == rra || ref == sa) 45 | return (to_check == ra || to_check == rra || to_check == sa); 46 | else if (ref == rb || ref == rrb || ref == sb) 47 | return (to_check == rb || to_check == rrb || to_check == sb); 48 | return (false); 49 | } 50 | 51 | t_op child_op(t_op first, t_op second) 52 | { 53 | if ((first == ra && second == rb) || (first == rb && second == ra)) 54 | return (rr); 55 | else if ((first == rra && second == rrb) || (first == rrb && second == rra)) 56 | return (rrr); 57 | else if ((first == sa && second == sb) || (first == sb && second == sa)) 58 | return (ss); 59 | else 60 | return (null_op); 61 | } 62 | 63 | void remove_op(t_list *to_delete) 64 | { 65 | to_delete->prev->next = to_delete->next; 66 | if (to_delete->next) 67 | to_delete->next->prev = to_delete->prev; 68 | ft_lstdelone(to_delete, NULL); 69 | } 70 | -------------------------------------------------------------------------------- /src/stack/stack_init.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* stack_init.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 21:09:08 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/28 18:44:51 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "stack.h" 14 | 15 | void fill_stack(t_ps *data, t_stack *stk, int size, char **arg) 16 | { 17 | int *numbers; 18 | int i; 19 | 20 | numbers = malloc(sizeof(int) * size); 21 | if (!numbers) 22 | error(data); 23 | i = 0; 24 | while (arg[i]) 25 | { 26 | if (!valid_arg(arg[i])) 27 | error(data); 28 | numbers[i] = ft_atoi(arg[i]); 29 | i++; 30 | } 31 | check_duplication(data, numbers, size); 32 | random_to_rank(numbers, stk->stack, size); 33 | stk->bottom = size - 1; 34 | free(numbers); 35 | } 36 | 37 | bool valid_arg(char *arg) 38 | { 39 | long long num; 40 | int sign; 41 | 42 | sign = 1; 43 | if (*arg == '\0') 44 | return (false); 45 | if (*arg == '-' || *arg == '+') 46 | { 47 | if (*arg == '-') 48 | sign = -1; 49 | arg++; 50 | if (*arg == '\0') 51 | return (false); 52 | } 53 | num = 0; 54 | while (*arg) 55 | { 56 | if (!ft_isdigit(*arg)) 57 | return (false); 58 | num = num * 10 + (*arg - '0'); 59 | if (((sign == 1 && num > INT_MAX) || (sign == -1 && - num < INT_MIN))) 60 | return (false); 61 | arg++; 62 | } 63 | return (true); 64 | } 65 | 66 | void check_duplication(t_ps *data, int *numbers, int size) 67 | { 68 | int i; 69 | int j; 70 | 71 | i = 0; 72 | while (i < size) 73 | { 74 | j = i + 1; 75 | while (j < size) 76 | { 77 | if (numbers[i] == numbers[j]) 78 | { 79 | free(numbers); 80 | error(data); 81 | } 82 | j++; 83 | } 84 | i++; 85 | } 86 | } 87 | 88 | void random_to_rank(int *numbers, int *rank, int size) 89 | { 90 | int i; 91 | int j; 92 | int count_smaller; 93 | 94 | i = 0; 95 | while (i < size) 96 | { 97 | j = 0; 98 | count_smaller = 0; 99 | while (j < size) 100 | if (numbers[j++] <= numbers[i]) 101 | count_smaller++; 102 | rank[i] = count_smaller; 103 | i++; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /include/stack.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* stack.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 20:56:27 by ugerkens #+# #+# */ 9 | /* Updated: 2025/01/22 17:09:32 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef STACK_H 14 | # define STACK_H 15 | 16 | # include "ft_printf.h" 17 | # include "libft.h" 18 | # include 19 | # include 20 | # include 21 | 22 | typedef struct s_stack 23 | { 24 | int *stack; 25 | int size; 26 | int top; 27 | int bottom; 28 | } t_stack; 29 | 30 | typedef struct s_push_swap 31 | { 32 | t_stack a; 33 | t_stack b; 34 | t_list *op_list; 35 | bool writing_mode; 36 | } t_ps; 37 | 38 | typedef enum e_op 39 | { 40 | null_op, 41 | pa, 42 | pb, 43 | ra, 44 | rb, 45 | rr, 46 | rra, 47 | rrb, 48 | rrr, 49 | sa, 50 | sb, 51 | ss 52 | } t_op; 53 | 54 | // DATA MANAGEMENT 55 | void init_data(t_ps *data, int argc, char **argv, bool writing_mode); 56 | void init_stack(t_ps *data, t_stack *stk, int size); 57 | bool is_sorted(t_ps *data); 58 | void free_data(t_ps *data); 59 | void error(t_ps *data); 60 | 61 | // STACK UTILS 62 | void fill_stack(t_ps *data, t_stack *stk, int size, char **arg); 63 | bool valid_arg(char *arg); 64 | void check_duplication(t_ps *data, int *numbers, int size); 65 | void random_to_rank(int *numbers, int *rank, int size); 66 | 67 | // STACK UTILS 68 | int next_up(t_stack *stk, int index); 69 | int next_down(t_stack *stk, int index); 70 | int value(t_stack *stk, int n); 71 | int current_size(t_stack *stk); 72 | bool is_full(t_stack *stk); 73 | 74 | // OPERATIONS UTILS 75 | void save_op(t_ps *data, t_op op); 76 | void print_operations(t_list *head); 77 | const char *op_to_string(t_op op); 78 | t_op op_from(t_list *node); 79 | 80 | // OPERATIONS ON STACKS 81 | void push(t_stack *src, t_stack *dest); 82 | void push_a(t_ps *data); 83 | void push_b(t_ps *data); 84 | void r_rotate(t_stack *stk); 85 | void r_rotate_a(t_ps *data); 86 | void r_rotate_b(t_ps *data); 87 | void r_rotate_ab(t_ps *data); 88 | void rotate(t_stack *stk); 89 | void rotate_a(t_ps *data); 90 | void rotate_b(t_ps *data); 91 | void rotate_ab(t_ps *data); 92 | void swap(t_stack *stk); 93 | void swap_a(t_ps *data); 94 | void swap_b(t_ps *data); 95 | void swap_ab(t_ps *data); 96 | 97 | #endif 98 | -------------------------------------------------------------------------------- /src/push_swap/chunk_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* chunk_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 20:57:19 by ugerkens #+# #+# */ 9 | /* Updated: 2025/01/22 17:06:14 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void chunk_split(t_ps *data, t_chunk *to_split, t_split_dest *dest) 16 | { 17 | int pivot_1; 18 | int pivot_2; 19 | int max_value; 20 | int next_value; 21 | 22 | innit_size(&dest->min, &dest->mid, &dest->max); 23 | set_split_loc(to_split->loc, &dest->min, &dest->mid, &dest->max); 24 | set_third_pivots(to_split->loc, to_split->size, &pivot_1, &pivot_2); 25 | max_value = chunk_max_value(data, to_split); 26 | while (to_split->size--) 27 | { 28 | next_value = chunk_value(data, to_split, 1); 29 | if (next_value > max_value - pivot_2) 30 | { 31 | dest->max.size += move_from_to(data, to_split->loc, dest->max.loc); 32 | split_max_reduction(data, &dest->max); 33 | if (a_partly_sort(data, 1) && to_split->size) 34 | easy_sort(data, to_split); 35 | } 36 | else if (next_value > max_value - pivot_1) 37 | dest->mid.size += move_from_to(data, to_split->loc, dest->mid.loc); 38 | else 39 | dest->min.size += move_from_to(data, to_split->loc, dest->min.loc); 40 | } 41 | } 42 | 43 | void innit_size(t_chunk *min, t_chunk *mid, t_chunk *max) 44 | { 45 | min->size = 0; 46 | mid->size = 0; 47 | max->size = 0; 48 | } 49 | 50 | void set_split_loc(t_loc loc, t_chunk *min, t_chunk *mid, t_chunk *max) 51 | { 52 | if (loc == TOP_A) 53 | { 54 | min->loc = BOTTOM_B; 55 | mid->loc = TOP_B; 56 | max->loc = BOTTOM_A; 57 | } 58 | else if (loc == BOTTOM_A) 59 | { 60 | min->loc = BOTTOM_B; 61 | mid->loc = TOP_B; 62 | max->loc = TOP_A; 63 | } 64 | else if (loc == TOP_B) 65 | { 66 | min->loc = BOTTOM_B; 67 | mid->loc = BOTTOM_A; 68 | max->loc = TOP_A; 69 | } 70 | else if (loc == BOTTOM_B) 71 | { 72 | min->loc = TOP_B; 73 | mid->loc = BOTTOM_A; 74 | max->loc = TOP_A; 75 | } 76 | } 77 | 78 | void set_third_pivots(t_loc loc, int crt_size, int *pivot_1, int *pivot_2) 79 | { 80 | *pivot_2 = crt_size / 3; 81 | if (loc == TOP_A || loc == BOTTOM_A) 82 | *pivot_1 = 2 * crt_size / 3; 83 | if (loc == TOP_B || loc == BOTTOM_B) 84 | *pivot_1 = crt_size / 2; 85 | if ((loc == TOP_A || loc == BOTTOM_A) && crt_size < 15) 86 | *pivot_1 = crt_size; 87 | if (loc == BOTTOM_B && crt_size < 8) 88 | *pivot_2 = crt_size / 2; 89 | } 90 | -------------------------------------------------------------------------------- /src/checker_bonus/checker_utils_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* checker_utils_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 20:56:40 by ugerkens #+# #+# */ 9 | /* Updated: 2025/01/22 17:04:33 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "checker_bonus.h" 14 | 15 | void error_read_op(t_ps *data, char *line) 16 | { 17 | if (line) 18 | free(line); 19 | error(data); 20 | } 21 | 22 | int get_next_line_ps(int fd, char *line) 23 | { 24 | int i; 25 | int read_status; 26 | char char_buffer; 27 | 28 | i = 0; 29 | char_buffer = ' '; 30 | while (char_buffer != '\n') 31 | { 32 | read_status = read(fd, &char_buffer, 1); 33 | if (read_status == -1) 34 | return (EXIT_FAILURE); 35 | if (read_status == 0) 36 | break ; 37 | line[i++] = char_buffer; 38 | if (i > 4) 39 | return (EXIT_FAILURE); 40 | } 41 | if ((i > 0 && line[i - 1] != '\n') || line[0] == '\n') 42 | return (EXIT_FAILURE); 43 | if (read_status == 0) 44 | line[0] = '\0'; 45 | else 46 | line[i - 1] = '\0'; 47 | return (EXIT_SUCCESS); 48 | } 49 | 50 | t_op string_to_op(const char *str) 51 | { 52 | if (!ft_strcmp(str, "pa")) 53 | return (pa); 54 | else if (!ft_strcmp(str, "pb")) 55 | return (pb); 56 | else if (!ft_strcmp(str, "rra")) 57 | return (rra); 58 | else if (!ft_strcmp(str, "rrb")) 59 | return (rrb); 60 | else if (!ft_strcmp(str, "rrr")) 61 | return (rrr); 62 | else if (!ft_strcmp(str, "ra")) 63 | return (ra); 64 | else if (!ft_strcmp(str, "rb")) 65 | return (rb); 66 | else if (!ft_strcmp(str, "rr")) 67 | return (rr); 68 | else if (!ft_strcmp(str, "sa")) 69 | return (sa); 70 | else if (!ft_strcmp(str, "sb")) 71 | return (sb); 72 | else if (!ft_strcmp(str, "ss")) 73 | return (ss); 74 | else 75 | return (null_op); 76 | } 77 | 78 | void call_op(t_ps *data, t_op op) 79 | { 80 | if (op == pa) 81 | push_a(data); 82 | else if (op == pb) 83 | push_b(data); 84 | else if (op == rra) 85 | r_rotate_a(data); 86 | else if (op == rrb) 87 | r_rotate_b(data); 88 | else if (op == rrr) 89 | r_rotate_ab(data); 90 | else if (op == ra) 91 | rotate_a(data); 92 | else if (op == rb) 93 | rotate_b(data); 94 | else if (op == rr) 95 | rotate_ab(data); 96 | else if (op == sa) 97 | swap_a(data); 98 | else if (op == sb) 99 | swap_b(data); 100 | else if (op == ss) 101 | swap_ab(data); 102 | else 103 | error(data); 104 | } 105 | -------------------------------------------------------------------------------- /lib/libft/include/libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/03/13 20:16:33 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/24 00:12:37 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | 16 | # include 17 | # include 18 | # include 19 | 20 | typedef struct s_list 21 | { 22 | void *content; 23 | struct s_list *next; 24 | struct s_list *prev; 25 | } t_list; 26 | 27 | int ft_isalpha(int c); 28 | int ft_isdigit(int c); 29 | int ft_isalnum(int c); 30 | int ft_isascii(int c); 31 | int ft_isprint(int c); 32 | size_t ft_strlen(const char *s); 33 | void *ft_memset(void *b, int c, size_t len); 34 | void ft_bzero(void *s, size_t n); 35 | void *ft_memcpy(void *dst, const void *src, size_t n); 36 | void *ft_memmove(void *dst, const void *src, size_t len); 37 | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); 38 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize); 39 | int ft_toupper(int c); 40 | int ft_tolower(int c); 41 | char *ft_strchr(const char *s, int c); 42 | char *ft_strrchr(const char *s, int c); 43 | int ft_strcmp(const char *s1, const char *s2); 44 | int ft_strncmp(const char *s1, const char *s2, size_t n); 45 | void *ft_memchr(const void *s, int c, size_t n); 46 | int ft_memcmp(const void *s1, const void *s2, size_t n); 47 | char *ft_strnstr(const char *haystack, const char *needle, 48 | size_t len); 49 | int ft_atoi(const char *str); 50 | void *ft_calloc(size_t count, size_t size); 51 | char *ft_strdup(const char *s1); 52 | 53 | char *ft_substr(char const *s, unsigned int start, size_t len); 54 | char *ft_strjoin(char const *s1, char const *s2); 55 | char *ft_strtrim(char const *s1, char const *set); 56 | char **ft_split(char const *s, char c); 57 | char *ft_itoa(int n); 58 | 59 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 60 | void ft_striteri(char *s, void (*f)(unsigned int, char *)); 61 | 62 | void ft_putchar_fd(char c, int fd); 63 | void ft_putstr_fd(char *s, int fd); 64 | void ft_putendl_fd(char *s, int fd); 65 | void ft_putnbr_fd(int n, int fd); 66 | 67 | t_list *ft_lstnew(void *content); 68 | void ft_lstadd_front(t_list **lst, t_list *new); 69 | int ft_lstsize(t_list *lst); 70 | t_list *ft_lstlast(t_list *lst); 71 | void ft_lstadd_back(t_list **lst, t_list *new); 72 | void ft_lstdelone(t_list *lst, void (*del)(void *)); 73 | void ft_lstclear(t_list **lst, void (*del)(void *)); 74 | void ft_lstiter(t_list *lst, void (*f)(void *)); 75 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), 76 | void (*del)(void *)); 77 | 78 | #endif -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: ugerkens +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2023/07/27 14:02:06 by ugerkens #+# #+# # 9 | # Updated: 2025/01/22 17:15:50 by ugerkens ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | # Variables 14 | CC = gcc 15 | CFLAGS = -Wall -Wextra -Werror -Iinclude -Isrc -O3 16 | RM = rm -rf 17 | CHECKER = checker 18 | PUSH_SWAP = push_swap 19 | NAME = $(PUSH_SWAP) $(CHECKER) 20 | 21 | # Libraries 22 | LIBFT = libft.a 23 | LIBFT_DIR = lib/libft 24 | LIBFT_FILE = $(LIBFT_DIR)/$(LIBFT) 25 | CFLAGS += -I $(LIBFT_DIR)/include 26 | 27 | FT_PRINTF = ft_printf.a 28 | FT_PRINTF_DIR = lib/ft_printf 29 | FT_PRINTF_FILE = $(FT_PRINTF_DIR)/$(FT_PRINTF) 30 | CFLAGS += -I $(FT_PRINTF_DIR)/include 31 | 32 | MAKE_LIB = make --no-print-directory -C 33 | 34 | # Source and Object Files 35 | VPATH = src:src/stack:src/push_swap:src/checker_bonus:include 36 | STACK_INC = stack.h 37 | STACK_SRC = data_mngt.c stack_init.c stack_utils.c op_push.c \ 38 | op_reverse_rotate.c op_rotate.c op_swap.c op_utils.c 39 | PUSH_SWAP_INC = $(STACK_INC) push_swap.h 40 | PUSH_SWAP_SRC = $(STACK_SRC) main.c sort.c move.c chunk_sort.c \ 41 | chunk_split.c chunk_utils.c opti_easy_sort.c \ 42 | opti_sort_asap.c opti_sort_three.c opti_to_the_top.c \ 43 | opti_post_sort.c opti_post_sort_utils.c 44 | CHECKER_INC = $(STACK_INC) checker_bonus.h 45 | CHECKER_SRC = $(STACK_SRC) checker_bonus.c checker_utils_bonus.c 46 | 47 | # Rules 48 | all: $(PUSH_SWAP) 49 | 50 | bonus: $(CHECKER) 51 | 52 | build: 53 | mkdir -p build 54 | 55 | LIB = $(LIBFT_FILE) $(FT_PRINTF_FILE) 56 | PUSH_SWAP_OBJ = $(PUSH_SWAP_SRC:%.c=build/push_swap/%.o) 57 | CHECKER_OBJ = $(CHECKER_SRC:%.c=build/checker/%.o) 58 | 59 | $(PUSH_SWAP_OBJ): build/push_swap/%.o: %.c $(PUSH_SWAP_INC) 60 | @mkdir -p $(dir $@) 61 | $(CC) $(CFLAGS) -c $< -o $@ 62 | 63 | $(CHECKER_OBJ): build/checker/%.o: %.c $(CHECKER_INC) 64 | @mkdir -p $(dir $@) 65 | $(CC) $(CFLAGS) -c $< -o $@ 66 | 67 | $(LIBFT_FILE): 68 | $(MAKE_LIB) $(LIBFT_DIR) 69 | $(FT_PRINTF_FILE): 70 | $(MAKE_LIB) $(FT_PRINTF_DIR) 71 | 72 | $(PUSH_SWAP): $(LIB) $(PUSH_SWAP_OBJ) 73 | $(CC) $(CFLAGS) $(LIB) $(PUSH_SWAP_OBJ) -o $@ 74 | 75 | $(CHECKER): $(LIB) $(CHECKER_OBJ) 76 | $(CC) $(CFLAGS) $(LIB) $(CHECKER_OBJ) -o $@ 77 | 78 | lib_clean: 79 | $(MAKE_LIB) $(LIBFT_DIR) clean 80 | $(MAKE_LIB) $(FT_PRINTF_DIR) clean 81 | 82 | lib_fclean: 83 | $(MAKE_LIB) $(LIBFT_DIR) fclean 84 | $(MAKE_LIB) $(FT_PRINTF_DIR) fclean 85 | 86 | clean: lib_clean 87 | $(RM) build 88 | 89 | fclean: clean lib_fclean 90 | $(RM) $(NAME) 91 | 92 | re: fclean all 93 | 94 | .SILENT: 95 | 96 | .PHONY: all bonus lib_clean lib_fclean clean fclean re 97 | -------------------------------------------------------------------------------- /src/push_swap/opti_sort_three.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* opti_sort_three.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 21:08:16 by ugerkens #+# #+# */ 9 | /* Updated: 2023/07/28 17:33:43 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void sort_three(t_ps *data, t_chunk *to_sort) 16 | { 17 | t_stack *stk; 18 | int max; 19 | 20 | stk = loc_to_stack(data, to_sort->loc); 21 | max = chunk_max_value(data, to_sort); 22 | if (to_sort->loc == TOP_A) 23 | sort_three_top_a(data, to_sort, stk, max); 24 | else if (to_sort->loc == BOTTOM_A) 25 | sort_three_bottom_a(data, to_sort, stk, max); 26 | else if (to_sort->loc == TOP_B) 27 | sort_three_top_b(data, to_sort, stk, max); 28 | else if (to_sort->loc == BOTTOM_B) 29 | sort_three_bottom_b(data, to_sort, stk, max); 30 | } 31 | 32 | void sort_three_top_a(t_ps *data, t_chunk *to_sort, t_stack *stk, int max) 33 | { 34 | if (stk->stack[stk->top] == max) 35 | { 36 | swap_a(data); 37 | rotate_a(data); 38 | swap_a(data); 39 | r_rotate_a(data); 40 | } 41 | else if (stk->stack[next_down(stk, stk->top)] == max) 42 | { 43 | rotate_a(data); 44 | swap_a(data); 45 | r_rotate_a(data); 46 | } 47 | to_sort->loc = TOP_A; 48 | to_sort->size -= 1; 49 | sort_two(data, to_sort); 50 | } 51 | 52 | void sort_three_top_b(t_ps *data, t_chunk *to_sort, t_stack *stk, int max) 53 | { 54 | push_a(data); 55 | if (stk->stack[stk->top] == max) 56 | { 57 | push_a(data); 58 | swap_a(data); 59 | } 60 | else if (stk->stack[next_down(stk, stk->top)] == max) 61 | { 62 | swap_b(data); 63 | push_a(data); 64 | swap_a(data); 65 | } 66 | else 67 | push_a(data); 68 | push_a(data); 69 | to_sort->loc = TOP_A; 70 | to_sort->size -= 1; 71 | sort_two(data, to_sort); 72 | } 73 | 74 | void sort_three_bottom_a(t_ps *data, t_chunk *to_sort, t_stack *stk, int max) 75 | { 76 | r_rotate_a(data); 77 | r_rotate_a(data); 78 | if (stk->stack[stk->top] == max) 79 | { 80 | swap_a(data); 81 | r_rotate_a(data); 82 | } 83 | else if (stk->stack[next_down(stk, stk->top)] == max) 84 | r_rotate_a(data); 85 | else 86 | { 87 | push_b(data); 88 | r_rotate_a(data); 89 | swap_a(data); 90 | push_a(data); 91 | } 92 | to_sort->loc = TOP_A; 93 | to_sort->size -= 1; 94 | sort_two(data, to_sort); 95 | } 96 | 97 | void sort_three_bottom_b(t_ps *data, t_chunk *to_sort, t_stack *stk, int max) 98 | { 99 | r_rotate_b(data); 100 | r_rotate_b(data); 101 | if (stk->stack[stk->top] == max) 102 | { 103 | push_a(data); 104 | r_rotate_b(data); 105 | } 106 | else if (stk->stack[next_down(stk, stk->top)] == max) 107 | { 108 | swap_b(data); 109 | push_a(data); 110 | r_rotate_b(data); 111 | } 112 | else 113 | { 114 | r_rotate_b(data); 115 | push_a(data); 116 | } 117 | to_sort->loc = TOP_B; 118 | to_sort->size -= 1; 119 | sort_two(data, to_sort); 120 | } 121 | -------------------------------------------------------------------------------- /include/push_swap.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* push_swap.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ugerkens +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/07/27 20:55:45 by ugerkens #+# #+# */ 9 | /* Updated: 2025/01/22 17:09:23 by ugerkens ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef PUSH_SWAP_H 14 | # define PUSH_SWAP_H 15 | 16 | # include "stack.h" 17 | 18 | typedef enum e_loc 19 | { 20 | TOP_A, 21 | BOTTOM_A, 22 | TOP_B, 23 | BOTTOM_B 24 | } t_loc; 25 | 26 | typedef struct s_chunk 27 | { 28 | t_loc loc; 29 | int size; 30 | } t_chunk; 31 | 32 | typedef struct s_split_dest 33 | { 34 | t_chunk min; 35 | t_chunk mid; 36 | t_chunk max; 37 | } t_split_dest; 38 | 39 | // MAIN PUSH_SWAP 40 | int main(int argc, char *argv[]); 41 | 42 | // SORT 43 | void sort(t_ps *data); 44 | void sort_three_a(t_ps *data); 45 | void sort_five_a(t_ps *data); 46 | 47 | // CHUNK SORT 48 | void chunk_sort(t_ps *data); 49 | void rec_chunk_sort(t_ps *data, t_chunk *to_sort); 50 | void sort_two(t_ps *data, t_chunk *to_sort); 51 | void sort_one(t_ps *data, t_chunk *to_sort); 52 | 53 | // CHUNK UTILS 54 | int chunk_value(t_ps *data, t_chunk *chunk, int n); 55 | int chunk_max_value(t_ps *data, t_chunk *chunk); 56 | t_stack *loc_to_stack(t_ps *data, t_loc loc); 57 | 58 | // CHUNK SPLIT 59 | void chunk_split(t_ps *data, t_chunk *to_split, t_split_dest *dest); 60 | void innit_size(t_chunk *min, t_chunk *mid, t_chunk *max); 61 | void set_split_loc(t_loc loc, t_chunk *min, t_chunk *mid, t_chunk *max); 62 | void set_third_pivots(t_loc loc, int crt_size, int *pivot_1, 63 | int *pivot_2); 64 | 65 | // MOVE 66 | int move_from_to(t_ps *data, t_loc from, t_loc to); 67 | void move_from_top_a(t_ps *data, t_loc to); 68 | void move_from_top_b(t_ps *data, t_loc to); 69 | void move_from_bottom_a(t_ps *data, t_loc to); 70 | void move_from_bottom_b(t_ps *data, t_loc to); 71 | 72 | // OPTIMIZATION: EASY SORT 73 | void easy_sort(t_ps *data, t_chunk *to_sort); 74 | void easy_sort_second(t_ps *data, t_chunk *to_sort); 75 | void handle_top_b(t_ps *data, t_chunk *to_sort); 76 | void handle_bottom_a(t_ps *data, t_chunk *to_sort); 77 | void handle_bottom_b(t_ps *data, t_chunk *to_sort); 78 | 79 | // OPTIMIZATION: SORT THREE 80 | void sort_three(t_ps *data, t_chunk *to_sort); 81 | void sort_three_top_a(t_ps *data, t_chunk *to_sort, t_stack *stk, 82 | int max); 83 | void sort_three_top_b(t_ps *data, t_chunk *to_sort, t_stack *stk, 84 | int max); 85 | void sort_three_bottom_a(t_ps *data, t_chunk *to_sort, t_stack *stk, 86 | int max); 87 | void sort_three_bottom_b(t_ps *data, t_chunk *to_sort, t_stack *stk, 88 | int max); 89 | 90 | // OPTIMIZATION: SORT ASAP 91 | void split_max_reduction(t_ps *data, t_chunk *max); 92 | bool a_partly_sort(t_ps *data, int from); 93 | bool is_consecutive(int a, int b, int c, int d); 94 | void sort_three_numbers(int *a, int *b, int *c); 95 | 96 | // OPTIMIZATION: TO THE TOP 97 | void chunk_to_the_top(t_ps *data, t_chunk *to_sort); 98 | 99 | // POST SORT OPTIMIZATION 100 | void post_sort_optimization(t_ps *data); 101 | void eliminate_neutral_op(t_list *op_list); 102 | void merge_op(t_list *op_list); 103 | 104 | // POST SORT OPTIMIZATION: UTILS 105 | t_op neutral_op(t_op op); 106 | bool op_on_same_stack(t_op ref, t_op to_check); 107 | t_op child_op(t_op first, t_op second); 108 | void remove_op(t_list *to_delete); 109 | 110 | #endif -------------------------------------------------------------------------------- /push_swap_docs.md: -------------------------------------------------------------------------------- 1 | # Push Swap documentation 2 | 3 | ## Table of Contents 4 | 5 | 1. [Stacks data structure](#stacks-data-structure) 6 | - [Due to random_to_rank, should I work with a bigger value type than int to accept a dataset with all int?](#due-to-random_to_rank-should-i-work-with-a-bigger-value-type-than-int-to-accept-a-dataset-with-all-int) 7 | 8 | 2. [Error management](#error-management) 9 | - [Whats the fd 2 in `ft_putendl_fd("Error", 2);`?](#whats-the-fd-2-in-ft_putendl_fderror-2) 10 | - [Differences between `return()` and `exit()`](#differences-between-return-and-exit) 11 | 12 | 3. [Inputs format](#inputs) 13 | - [Why do not manage the input `./push_swap "1 2 3 4"`](#why-do-not-manage-the-input-push_swap-1-2-3-4) 14 | - [Why does it manage `./push_swap 1 2 "3" 4`](#why-does-it-manage-push_swap-1-2-3-4) 15 | - [How to use `./checker 1 2 3` without giving him an input with the pipe.](#how-to-use-checker-1-2-3-without-giving-him-an-input-with-the-pipe) 16 | 17 | 4. [Checker: Why do not reuse `get_next_line`](#checker-why-we-cannot-reuse-get_next_line) 18 | 19 | --- 20 |
21 | 22 | ## Stacks data structure 23 | 24 | Two int arrays, [circular buffer](https://en.wikipedia.org/wiki/Circular_buffer).
25 | [42 Stack Overflow Subject: What data structure did you use for your push_swap?](https://stackoverflowteams.com/c/42network/questions/373) 26 | 27 | - Due to `random_to_rank()`, should I work with a bigger value type than int to accept a dataset with all int? 28 | 29 | Practically absurd : 30 | 31 | The space required to store a specific number of integers in an array depends on the size of the integer type on your system. 32 | 33 | In C, the `int` type is typically 4 bytes (or 32 bits) on most modern systems, although this can vary. You can verify the size of an `int` in bytes on your specific system by using the `sizeof` operator: `printf("Size of int: %zu bytes\n", sizeof(int));` 34 | 35 | Assuming that `int` is 4 bytes, to store `2147483647` integers in an array, you'd need: 36 | `2147483647 integers * 4 bytes/integer = 8589934588 bytes` 37 | 38 | That's approximately 8.59 GB. 39 | 40 | Keep in mind that this is a huge amount of memory, and trying to allocate this much space at once could fail on many systems due to insufficient available memory. If you need to work with this many integers, you may need to use a data structure or algorithm that can handle the data in chunks rather than all at once. 41 | 42 |
43 | 44 | ## Error management 45 | 46 | - Whats the fd 2 in `ft_putendl_fd("Error", 2);` ? 47 | 48 | In Unix-like operating systems (which include Linux and MacOS), file descriptors `0`, `1`, and `2` are, by convention, the standard input, standard output, and standard error, respectively. 49 | 50 | Here is a brief description of each: 51 | 52 | - `0` (`STDIN_FILENO`): Standard input, usually from the keyboard. 53 | - `1` (`STDOUT_FILENO`): Standard output, usually to the terminal. 54 | - `2` (`STDERR_FILENO`): Standard error, also usually to the terminal. This is meant for outputting error messages. 55 | 56 | Therefore, in this case, `"Error"` will be sent to `stderr`, not `stdout`. This is useful because you can then redirect these streams independently of each other. For instance, you might want to redirect error messages to a log file, but keep regular output going to the terminal. 57 | 58 | - Differences between `return()` and `exit()` 59 | 60 | `return()` and `exit()` are both used to terminate the execution of a program, but they differ in their usage and behavior: 61 | 62 | 1. `return()`: 63 | - `return()` is used within a function to return a value to the caller and terminate the function's execution. 64 | - It is typically used in functions with a return type other than `void`. The return value can be used by the calling code. 65 | - When `return` is encountered, it transfers control back to the caller, and the program continues executing from where the function was called. 66 | - `return` allows you to gracefully exit a function and provide a result or status back to the caller. 67 | 2. `exit()`: 68 | - `exit()` is used to terminate the entire program, regardless of where it is called. 69 | - It is typically used to indicate a significant error condition or to explicitly terminate the program's execution. 70 | - When `exit()` is called, the program terminates immediately, and control is handed back to the operating system. 71 | - `exit()` performs cleanup tasks, such as closing open files and freeing allocated memory, before terminating the program. 72 | - You can specify an exit status code (an integer value) as an argument to `exit()` to indicate the reason for termination. By convention, a status code of 0 indicates successful execution, while nonzero values represent different error conditions. 73 | 74 |
75 | 76 | ## Inputs 77 | 78 | - Why do not manage the input `./push_swap "1 2 3 4"` 79 | 80 | [Related threads on slack](https://42born2code.slack.com/archives/CMX2R5JSW/p1657468649909519).
81 | When you pass a string argument like `"1 2 3 4"` to your program, it gets interpreted as a single argument. Since you're expecting each number to be a separate argument, it throws an error. 82 | 83 | - Why does it manage `./push_swap 1 2 "3" 4` 84 | 85 | When you input `"1"` as an argument in the command line, the 86 | shell automatically strips away the double quotes before passing the 87 | argument to your program. As a result, your program receives `1` instead of `"1"`. 88 | 89 | Unfortunately, there's no built-in way to detect if an argument was originally wrapped in quotes in the command line, because the shell removes those quotes before your program even starts. 90 | 91 | - How to use `./checker 1 2 3` without giving him an input with the pipe. 92 | 93 | **Ctrl+D (EOF)**: In Unix-like systems, you can send an End-of-File (EOF) signal by pressing `Ctrl+D`. This will be recognized as the end of input by many programs. If your program reads from standard input in a loop, sending an EOF signal will break out of the loop. 94 | 95 |
96 | 97 | ## Checker: Why we cannot reuse `get_next_line` 98 | 99 | This concern the new curriculum version. 100 | 101 | The problem is the following: GNL don’t make the difference between an empty line an an error. It return NULL in both case. So, if we reached the end of the file and we (re)call GNL, it will return NULL, and we’ll not enable to say if this NULL its due to an error in the GNL or due to an empty file. 102 | 103 | The GNL from the previous curriculum version place the line in a buffer give as parameter and return an error. So I think it could works. 104 | 105 | Operation in the GNL from the new curriculum version: 106 | 107 | ```c 108 | if (!line || line[0] == '\0') 109 | { 110 | free(line); 111 | return (NULL); 112 | } 113 | ``` --------------------------------------------------------------------------------