From d0fe1fb2fb4ae31e841db3b0f7fe48eb8550353d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl?= Date: Sat, 20 Apr 2024 14:48:16 +0200 Subject: [PATCH] Push the final version --- Makefile | 138 ++++++++++++++++++++++++++++++++++++++++ includes/push_swap.h | 66 +++++++++++++++++++ libft/ft_atoi.c | 54 ++++++++++++++++ libft/ft_isdigit.c | 20 ++++++ libft/ft_lstadd_back.c | 30 +++++++++ libft/ft_lstadd_front.c | 19 ++++++ libft/ft_lstlast.c | 27 ++++++++ libft/ft_lstnew.c | 26 ++++++++ libft/ft_lstsize.c | 28 ++++++++ libft/ft_memcpy.c | 32 ++++++++++ libft/ft_split.c | 100 +++++++++++++++++++++++++++++ libft/ft_strchr.c | 29 +++++++++ libft/ft_strdup.c | 26 ++++++++ libft/ft_strlcpy.c | 31 +++++++++ libft/ft_strlen.c | 23 +++++++ libft/ft_strncmp.c | 31 +++++++++ libft/ft_substr.c | 32 ++++++++++ sources/error.c | 53 +++++++++++++++ sources/main.c | 67 +++++++++++++++++++ sources/push.c | 56 ++++++++++++++++ sources/reverse.c | 62 ++++++++++++++++++ sources/rotate.c | 54 ++++++++++++++++ sources/sort_radix.c | 62 ++++++++++++++++++ sources/sort_simple.c | 79 +++++++++++++++++++++++ sources/sort_small.c | 58 +++++++++++++++++ sources/swap.c | 61 ++++++++++++++++++ sources/utilities.c | 98 ++++++++++++++++++++++++++++ sources/validation.c | 97 ++++++++++++++++++++++++++++ 28 files changed, 1459 insertions(+) create mode 100755 Makefile create mode 100755 includes/push_swap.h create mode 100755 libft/ft_atoi.c create mode 100755 libft/ft_isdigit.c create mode 100755 libft/ft_lstadd_back.c create mode 100755 libft/ft_lstadd_front.c create mode 100755 libft/ft_lstlast.c create mode 100755 libft/ft_lstnew.c create mode 100755 libft/ft_lstsize.c create mode 100755 libft/ft_memcpy.c create mode 100755 libft/ft_split.c create mode 100755 libft/ft_strchr.c create mode 100755 libft/ft_strdup.c create mode 100644 libft/ft_strlcpy.c create mode 100644 libft/ft_strlen.c create mode 100644 libft/ft_strncmp.c create mode 100755 libft/ft_substr.c create mode 100755 sources/error.c create mode 100755 sources/main.c create mode 100755 sources/push.c create mode 100755 sources/reverse.c create mode 100755 sources/rotate.c create mode 100755 sources/sort_radix.c create mode 100755 sources/sort_simple.c create mode 100755 sources/sort_small.c create mode 100755 sources/swap.c create mode 100755 sources/utilities.c create mode 100755 sources/validation.c diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..ad61a44 --- /dev/null +++ b/Makefile @@ -0,0 +1,138 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: rparodi +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # +# Updated: 2024/04/18 12:46:04 by rparodi ### ########.fr # +# # +# **************************************************************************** # +# Variables + +# Name +NAME = push_swap +LIBDIRNAME = libft +SRCDIRNAME = sources + +# Commands +CC = cc +RM = rm -rf + +# Flags +CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3 -MMD + +# Sources +LIB = ./libft/ft_atoi.c \ + ./libft/ft_isdigit.c \ + ./libft/ft_lstadd_back.c \ + ./libft/ft_lstadd_front.c \ + ./libft/ft_lstlast.c \ + ./libft/ft_lstnew.c \ + ./libft/ft_lstsize.c \ + ./libft/ft_memcpy.c \ + ./libft/ft_split.c \ + ./libft/ft_strchr.c \ + ./libft/ft_strdup.c \ + ./libft/ft_strlcpy.c \ + ./libft/ft_strlen.c \ + ./libft/ft_substr.c + +SRC = sources/error.c \ + sources/push.c \ + sources/reverse.c \ + sources/rotate.c \ + sources/sort_radix.c \ + sources/sort_simple.c \ + sources/sort_small.c \ + sources/swap.c \ + sources/utilities.c \ + sources/validation.c \ + sources/main.c + +# Objects +OBJDIRNAME = ./objects +OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.c=.o)) +LIB_OBJ = $(addprefix $(OBJDIRNAME)/,$(LIB:.c=.o)) + +# Colors +GREEN = \033[32m +GREY = \033[0;90m +RED = \033[0;31m +GOLD = \033[38;5;220m +END = \033[0m + +# Rules + +# All (make all) +all: header $(NAME) footer + +# Bonus (make bonus) +bonus: $(OBJ) $(LIB_OBJ) + @cc $(CFLAGS) $(MLXFLAGS) -o $(NAME) $(OBJ) $(LIB_OBJ) + +# Clean (make clean) +clean: + @printf '$(GREY) Removing $(END)$(RED)Objects$(END)\n' + @printf '$(GREY) Removing $(END)$(RED)Objects Folder$(END)\n' + @$(RM) $(OBJDIRNAME) + +# Clean (make fclean) +fclean: clean + @printf '$(GREY) Removing $(END)$(RED)Program$(END)\n' + @$(RM) $(NAME) + @echo "" + +# Restart (make re) +re: header fclean bonus all + +# Dependences for all +$(NAME): $(OBJ) $(LIB_OBJ) + @mkdir -p $(OBJDIRNAME) + @mkdir -p $(OBJDIRNAME)/$(LIBDIRNAME) + @mkdir -p $(OBJDIRNAME)/$(SRCDIRNAME) + @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' + @cc $(CFLAGS) -o $(NAME) $(OBJ) $(LIB_OBJ) + +# Creating the objects +$(OBJDIRNAME)/%.o: %.c + @mkdir -p $(dir $@) + @printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n' + @cc $(CFLAGS) -o $@ -c $< + +# Header +header: + @clear + @printf '\n\n' + @printf '$(GOLD) ******* ****** ******* $(END)\n' + @printf '$(GOLD) ****** *** ******* $(END)\n' + @printf '$(GOLD) ******* * ******* $(END)\n' + @printf '$(GOLD) ****** ******* $(END)\n' + @printf '$(GOLD) ******* ******* $(END)\n' + @printf '$(GOLD) ******************* ******* * $(END)\n' + @printf '$(GOLD) ******************* ******* *** $(END)\n' + @printf '$(GOLD) ****** ******* ****** $(END)\n' + @printf '$(GOLD) ****** $(END)\n' + @printf '$(GOLD) ****** $(END)\n' + @printf '$(GREY) Made by rparodi$(END)\n\n' + +# Footer +footer: + @printf "\n" + @printf "$(GOLD) ,_ _,$(END)\n" + @printf "$(GOLD) | \\___//|$(END)\n" + @printf "$(GOLD) |=6 6=|$(END)\n" + @printf "$(GOLD) \\=._Y_.=/$(END)\n" + @printf "$(GOLD) ) \` ( ,$(END)\n" + @printf "$(GOLD) / \\ (('$(END)\n" + @printf "$(GOLD) | | ))$(END)\n" + @printf "$(GOLD) /| | | |\\_//$(END)\n" + @printf "$(GOLD) \\| |._.| |/-\`$(END)\n" + @printf "$(GOLD) '\"' '\"'$(END)\n" + @printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n' + +# Phony +.PHONY: all bonus clean fclean re + +-include ${OBJ:.o=.d} diff --git a/includes/push_swap.h b/includes/push_swap.h new file mode 100755 index 0000000..4ea800c --- /dev/null +++ b/includes/push_swap.h @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* push_swap.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/07 11:11:07 by rparodi #+# #+# */ +/* Updated: 2024/04/18 14:51:13 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PUSH_SWAP_H +# define PUSH_SWAP_H + +# include +# include +# include + +typedef struct s_list +{ + int value; + int index; + struct s_list *next; +} t_list; + +int is_sorted(t_list **stack); +int pa(t_list **stack_a, t_list **stack_b); +int pb(t_list **stack_a, t_list **stack_b); +int sa(t_list **stack_a); +int sb(t_list **stack_b); +int ss(t_list **stack_a, t_list **stack_b); +int ra(t_list **stack_a); +int rb(t_list **stack_b); +int rr(t_list **stack_a, t_list **stack_b); +int rra(t_list **stack_a); +int rrb(t_list **stack_b); +int rrr(t_list **stack_a, t_list **stack_b); +int index_distance_head(t_list **stack, int index); +int ft_isdigit(int c); +int get_min(t_list **stack, int val); +int ft_lstsize(t_list *head); +long ft_atoi(const char *nptr); +char *ft_strchr(const char *s, int c); +char *ft_strdup(const char *s); +char *ft_substr(char const *s, unsigned int start, size_t len); +char **ft_split(char const *s); +void free_string(char **str); +void ft_lstadd_front(t_list **stack, t_list *new); +void ft_lstadd_back(t_list **stack, t_list *new); +void *ft_memcpy(void *dest, const void *src, size_t n); +void free_stack(t_list **stack); +void index_stack(t_list **stack); +void error_message(char *msg, t_list **maix, t_list **sb, char **s); +void check_args(char **argv); +void sort_3(t_list **stack_a); +void sort_4(t_list **stack_a, t_list **stack_b); +void sort_5(t_list **stack_a, t_list **stack_b); +void simple_sort(t_list **stack_a, t_list **stack_b); +void radix_sort(t_list **stack_a, t_list **stack_b); +size_t ft_strlcpy(char *dst, const char *src, size_t size); +size_t ft_len(const char *str); +t_list *ft_lstnew(int content); +t_list *ft_lstlast(t_list *head); + +#endif diff --git a/libft/ft_atoi.c b/libft/ft_atoi.c new file mode 100755 index 0000000..dfacbd0 --- /dev/null +++ b/libft/ft_atoi.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 17:22:41 by rparodi #+# #+# */ +/* Updated: 2024/03/07 11:11:31 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +static int ft_check_space(int c) +{ + if (c == 32 || (c >= 9 && c <= 13)) + return (1); + return (0); +} + +static int ft_check_sign(const char *nptr, int *i) +{ + while (ft_check_space(nptr[*i])) + (*i)++; + if (nptr[*i] == '-') + { + (*i)++; + return (-1); + } + else if (nptr[*i] == '+') + (*i)++; + return (1); +} + +long ft_atoi(const char *nptr) +{ + int i; + int sign; + long number; + + i = 0; + sign = ft_check_sign(nptr, &i); + number = 0; + while (nptr[i]) + { + if (nptr[i] >= '0' && nptr[i] <= '9') + number = (number * 10) + nptr[i] - '0'; + else + break ; + i++; + } + return (sign * number); +} diff --git a/libft/ft_isdigit.c b/libft/ft_isdigit.c new file mode 100755 index 0000000..4ea6927 --- /dev/null +++ b/libft/ft_isdigit.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 12:44:28 by rparodi #+# #+# */ +/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +int ft_isdigit(int c) +{ + if (c >= '0' && c <= '9') + return (c); + return (0); +} diff --git a/libft/ft_lstadd_back.c b/libft/ft_lstadd_back.c new file mode 100755 index 0000000..44414a3 --- /dev/null +++ b/libft/ft_lstadd_back.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/13 12:07:29 by rparodi #+# #+# */ +/* Updated: 2024/03/06 12:15:50 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +void ft_lstadd_back(t_list **stack, t_list *new) +{ + t_list *node; + + if (*stack) + { + node = ft_lstlast(*stack); + node->next = new; + new->next = NULL; + } + else + { + *stack = new; + (*stack)->next = NULL; + } +} diff --git a/libft/ft_lstadd_front.c b/libft/ft_lstadd_front.c new file mode 100755 index 0000000..ae391f0 --- /dev/null +++ b/libft/ft_lstadd_front.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/13 12:07:31 by rparodi #+# #+# */ +/* Updated: 2024/03/06 12:15:50 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +void ft_lstadd_front(t_list **stack, t_list *new) +{ + new->next = *stack; + *stack = new; +} diff --git a/libft/ft_lstlast.c b/libft/ft_lstlast.c new file mode 100755 index 0000000..82ac4dc --- /dev/null +++ b/libft/ft_lstlast.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/13 12:07:33 by rparodi #+# #+# */ +/* Updated: 2024/03/06 12:15:50 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +t_list *ft_lstlast(t_list *head) +{ + t_list *tmp; + + tmp = head; + while (tmp->next) + { + tmp = tmp->next; + if (tmp->next == NULL) + return (tmp); + } + return (tmp); +} diff --git a/libft/ft_lstnew.c b/libft/ft_lstnew.c new file mode 100755 index 0000000..cd99e9c --- /dev/null +++ b/libft/ft_lstnew.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/13 12:07:45 by rparodi #+# #+# */ +/* Updated: 2024/03/06 12:15:50 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +t_list *ft_lstnew(int value) +{ + t_list *new; + + new = (t_list *) malloc(sizeof(*new)); + if (!new) + return (NULL); + new->value = value; + new->index = -1; + new->next = NULL; + return (new); +} diff --git a/libft/ft_lstsize.c b/libft/ft_lstsize.c new file mode 100755 index 0000000..c541b93 --- /dev/null +++ b/libft/ft_lstsize.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/13 12:07:48 by rparodi #+# #+# */ +/* Updated: 2024/03/06 12:15:49 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +int ft_lstsize(t_list *head) +{ + size_t i; + t_list *tmp; + + tmp = head; + i = 0; + while (tmp) + { + tmp = tmp->next; + i++; + } + return (i); +} diff --git a/libft/ft_memcpy.c b/libft/ft_memcpy.c new file mode 100755 index 0000000..c0104b6 --- /dev/null +++ b/libft/ft_memcpy.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:49:46 by rparodi #+# #+# */ +/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + char *d; + const char *s; + size_t i; + + i = 0; + if (!dest && !src) + return (NULL); + d = (char *)dest; + s = (char *)src; + while (i < n) + { + d[i] = s[i]; + i++; + } + return ((void *)d); +} diff --git a/libft/ft_split.c b/libft/ft_split.c new file mode 100755 index 0000000..79c2817 --- /dev/null +++ b/libft/ft_split.c @@ -0,0 +1,100 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 13:56:02 by rparodi #+# #+# */ +/* Updated: 2024/04/18 15:21:57 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +static int ft_check_space(int c) +{ + if (c == 32 || (c >= 9 && c <= 13)) + return (1); + return (0); +} + +static int count_words(const char *str) +{ + int i; + int count; + + i = 0; + count = 0; + while (str[i]) + { + while (ft_check_space(str[i]) == 1 && str[i]) + i++; + if (ft_check_space(str[i]) == 0 && str[i]) + { + while (ft_check_space(str[i]) == 0 && str[i]) + i++; + count++; + } + } + return (count); +} + +static char *ft_strndup(const char *s, int j) +{ + int i; + char *str; + + i = 0; + str = (char *)malloc((j + 1)); + if (!str) + error_message("alloc strndup", NULL, NULL, NULL); + while (s[i] && i < j) + { + str[i] = s[i]; + i++; + } + str[i] = '\0'; + return (str); +} + +static char **ext_w(char **words_array, const char *str, int size) +{ + int i; + int j; + + i = 0; + j = 0; + while (j < size) + { + while (ft_check_space(str[i]) == 1 && str[i]) + i++; + str = str + i; + i = 0; + while (ft_check_space(str[i]) == 0 && str[i]) + i++; + words_array[j++] = ft_strndup(str, i); + str = str + i; + i = 0; + } + words_array[j] = 0; + return (words_array); +} + +char **ft_split(char const *s) +{ + int size; + char **words_array; + + size = count_words(s); + words_array = (char **)malloc(sizeof(char *) * (size + 1)); + if (!words_array) + error_message("alloc word cut", NULL, NULL, NULL); + if (size == 0) + { + words_array[0] = NULL; + return (words_array); + } + words_array = ext_w(words_array, s, size); + return (words_array); +} diff --git a/libft/ft_strchr.c b/libft/ft_strchr.c new file mode 100755 index 0000000..76669b2 --- /dev/null +++ b/libft/ft_strchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:32:19 by rparodi #+# #+# */ +/* Updated: 2024/04/17 19:44:38 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +char *ft_strchr(const char *s, int c) +{ + unsigned int i; + + i = 0; + if (c == 0) + return ((char *)s + ft_len(s)); + while (s[i] != '\0') + { + if (s[i] == (char)c) + return ((char *)s + i); + i++; + } + return (NULL); +} diff --git a/libft/ft_strdup.c b/libft/ft_strdup.c new file mode 100755 index 0000000..9ecf152 --- /dev/null +++ b/libft/ft_strdup.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:53:59 by rparodi #+# #+# */ +/* Updated: 2024/04/17 19:45:29 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +char *ft_strdup(const char *s) +{ + size_t len; + char *to_return; + + len = ft_len(s) + 1; + to_return = (char *)malloc(sizeof(char) * len); + if (!to_return) + return (NULL); + ft_strlcpy(to_return, s, len); + return (to_return); +} diff --git a/libft/ft_strlcpy.c b/libft/ft_strlcpy.c new file mode 100644 index 0000000..5d644e3 --- /dev/null +++ b/libft/ft_strlcpy.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:55:25 by rparodi #+# #+# */ +/* Updated: 2024/04/17 19:45:05 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +size_t ft_strlcpy(char *dst, const char *src, size_t size) +{ + size_t i; + + i = 0; + while (src[i] && i + 1 < size) + { + dst[i] = src[i]; + i++; + } + if (size > 0) + { + dst[i] = '\0'; + i++; + } + return (ft_len(src)); +} diff --git a/libft/ft_strlen.c b/libft/ft_strlen.c new file mode 100644 index 0000000..ce79ec8 --- /dev/null +++ b/libft/ft_strlen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:56:24 by rparodi #+# #+# */ +/* Updated: 2024/04/17 19:42:58 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +size_t ft_len(const char *s) +{ + size_t i; + + i = 0; + while (s[i] != '\0') + i++; + return (i); +} diff --git a/libft/ft_strncmp.c b/libft/ft_strncmp.c new file mode 100644 index 0000000..9afff02 --- /dev/null +++ b/libft/ft_strncmp.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:56:56 by rparodi #+# #+# */ +/* Updated: 2023/11/09 13:16:39 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + int diff; + + i = 0; + while ((s1[i] || s2[i]) && i < n) + { + if (s1[i] != s2[i]) + { + diff = (unsigned char)s1[i] - (unsigned char)s2[i]; + return (diff); + } + i++; + } + return (0); +} diff --git a/libft/ft_substr.c b/libft/ft_substr.c new file mode 100755 index 0000000..5c33e61 --- /dev/null +++ b/libft/ft_substr.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 13:54:42 by rparodi #+# #+# */ +/* Updated: 2024/04/17 19:45:46 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + size_t i; + size_t j; + char *str; + + i = start; + if (start >= ft_len(s)) + return (ft_strdup("")); + if (len + start > (ft_len(s))) + len = ft_len(s) - start; + j = 0; + str = (char *)malloc(len + 1); + if (!str || !s) + return (free(str), ft_strdup("")); + ft_strlcpy(str, s + start, len + 1); + return (str); +} diff --git a/sources/error.c b/sources/error.c new file mode 100755 index 0000000..66095d8 --- /dev/null +++ b/sources/error.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/13 12:08:13 by rparodi #+# #+# */ +/* Updated: 2024/04/18 15:17:25 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +void error_message(char *msg, t_list **maix, t_list **sb, char **s) +{ + (void)msg; + write(2, "Error\n", 6); + if (maix) + free_stack(maix); + if (sb) + free_stack(sb); + if (s) + free_string(s); + exit(EXIT_FAILURE); +} + +void free_string(char **str) +{ + int i; + + i = 0; + while (str[i]) + i++; + while (i >= 0) + free(str[i--]); + free(str); +} + +void free_stack(t_list **stack) +{ + t_list *head; + t_list *tmp; + + head = *stack; + while (head) + { + tmp = head; + head = head->next; + free(tmp); + } + free(stack); +} diff --git a/sources/main.c b/sources/main.c new file mode 100755 index 0000000..b0b44c2 --- /dev/null +++ b/sources/main.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/13 23:58:38 by jotavare #+# #+# */ +/* Updated: 2024/04/18 15:18:22 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +static void init_stack(t_list **stack, int argc, char **argv) +{ + t_list *new; + char **args; + int i; + + i = 0; + if (argc == 2) + args = ft_split(argv[1]); + else + { + i = 1; + args = argv; + } + while (args[i]) + { + new = ft_lstnew(ft_atoi(args[i])); + ft_lstadd_back(stack, new); + i++; + } + index_stack(stack); + if (argc == 2) + free_string(args); +} + +static void sort_stack(t_list **stack_a, t_list **stack_b) +{ + if (ft_lstsize(*stack_a) <= 5) + simple_sort(stack_a, stack_b); + else + radix_sort(stack_a, stack_b); +} + +int main(int argc, char **argv) +{ + t_list **stack_a; + t_list **stack_b; + + check_args(argv); + stack_a = (t_list **)malloc(sizeof(t_list)); + if (!stack_a) + error_message("alloc stack_a", NULL, NULL, NULL); + *stack_a = NULL; + init_stack(stack_a, argc, argv); + if (is_sorted(stack_a) == 1) + (free_stack(stack_a), exit(0)); + stack_b = (t_list **)malloc(sizeof(t_list)); + *stack_b = NULL; + sort_stack(stack_a, stack_b); + free_stack(stack_a); + free_stack(stack_b); + return (0); +} diff --git a/sources/push.c b/sources/push.c new file mode 100755 index 0000000..ad2090c --- /dev/null +++ b/sources/push.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* push.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/13 12:08:03 by rparodi #+# #+# */ +/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +static int push(t_list **stack_to, t_list **stack_from) +{ + t_list *tmp; + t_list *head_to; + t_list *head_from; + + if (ft_lstsize(*stack_from) == 0) + return (-1); + head_to = *stack_to; + head_from = *stack_from; + tmp = head_from; + head_from = head_from->next; + *stack_from = head_from; + if (!head_to) + { + head_to = tmp; + head_to->next = NULL; + *stack_to = head_to; + } + else + { + tmp->next = head_to; + *stack_to = tmp; + } + return (0); +} + +int pa(t_list **stack_a, t_list **stack_b) +{ + if (push(stack_a, stack_b) == -1) + return (-1); + write(1, "pa\n", 3); + return (0); +} + +int pb(t_list **stack_a, t_list **stack_b) +{ + if (push(stack_b, stack_a) == -1) + return (-1); + write(1, "pb\n", 3); + return (0); +} diff --git a/sources/reverse.c b/sources/reverse.c new file mode 100755 index 0000000..89bc4a6 --- /dev/null +++ b/sources/reverse.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* reverse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/13 12:08:06 by rparodi #+# #+# */ +/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +static int reverse(t_list **stack) +{ + t_list *head; + t_list *tail; + + if (ft_lstsize(*stack) < 2) + return (-1); + head = *stack; + tail = ft_lstlast(head); + while (head) + { + if (head->next->next == NULL) + { + head->next = NULL; + break ; + } + head = head->next; + } + tail->next = *stack; + *stack = tail; + return (0); +} + +int rra(t_list **stack_a) +{ + if (reverse(stack_a) == -1) + return (-1); + write(1, "rra\n", 4); + return (0); +} + +int rrb(t_list **stack_b) +{ + if (reverse(stack_b) == -1) + return (-1); + write(1, "rrb\n", 4); + return (0); +} + +int rrr(t_list **stack_a, t_list **stack_b) +{ + if ((ft_lstsize(*stack_a) < 2) || (ft_lstsize(*stack_b) < 2)) + return (-1); + reverse(stack_a); + reverse(stack_b); + write(1, "rrr\n", 4); + return (0); +} diff --git a/sources/rotate.c b/sources/rotate.c new file mode 100755 index 0000000..889b83a --- /dev/null +++ b/sources/rotate.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rotate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/13 12:08:07 by rparodi #+# #+# */ +/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +static int rotate(t_list **stack) +{ + t_list *head; + t_list *tail; + + if (ft_lstsize(*stack) < 2) + return (-1); + head = *stack; + tail = ft_lstlast(head); + *stack = head->next; + head->next = NULL; + tail->next = head; + return (0); +} + +int ra(t_list **stack_a) +{ + if (rotate(stack_a) == -1) + return (-1); + write(1, "ra\n", 3); + return (0); +} + +int rb(t_list **stack_b) +{ + if (rotate(stack_b) == -1) + return (-1); + write(1, "rb\n", 3); + return (0); +} + +int rr(t_list **stack_a, t_list **stack_b) +{ + if ((ft_lstsize(*stack_a) < 2) || (ft_lstsize(*stack_b) < 2)) + return (-1); + rotate(stack_a); + rotate(stack_b); + write(1, "rr\n", 3); + return (0); +} diff --git a/sources/sort_radix.c b/sources/sort_radix.c new file mode 100755 index 0000000..1f80ec7 --- /dev/null +++ b/sources/sort_radix.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort_radix.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/13 12:07:02 by rparodi #+# #+# */ +/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +static int get_max_bits(t_list **stack) +{ + t_list *head; + int max; + int max_bits; + + head = *stack; + max = head->index; + max_bits = 0; + while (head) + { + if (head->index > max) + max = head->index; + head = head->next; + } + while ((max >> max_bits) != 0) + max_bits++; + return (max_bits); +} + +void radix_sort(t_list **stack_a, t_list **stack_b) +{ + t_list *head_a; + int i; + int j; + int size; + int max_bits; + + i = 0; + head_a = *stack_a; + size = ft_lstsize(head_a); + max_bits = get_max_bits(stack_a); + while (i < max_bits) + { + j = 0; + while (j++ < size) + { + head_a = *stack_a; + if (((head_a->index >> i) & 1) == 1) + ra(stack_a); + else + pb(stack_a, stack_b); + } + while (ft_lstsize(*stack_b) != 0) + pa(stack_a, stack_b); + i++; + } +} diff --git a/sources/sort_simple.c b/sources/sort_simple.c new file mode 100755 index 0000000..05c3ffd --- /dev/null +++ b/sources/sort_simple.c @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort_simple.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/13 12:07:08 by rparodi #+# #+# */ +/* Updated: 2024/04/16 20:16:06 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +void sort_4(t_list **stack_a, t_list **stack_b) +{ + int distance; + + if (is_sorted(stack_a)) + return ; + distance = index_distance_head(stack_a, get_min(stack_a, -1)); + if (distance == 1) + ra(stack_a); + else if (distance == 2) + { + ra(stack_a); + ra(stack_a); + } + else if (distance == 3) + rra(stack_a); + if (is_sorted(stack_a)) + return ; + pb(stack_a, stack_b); + sort_3(stack_a); + pa(stack_a, stack_b); +} + +void sort_5(t_list **stack_a, t_list **stack_b) +{ + int distance; + + distance = index_distance_head(stack_a, get_min(stack_a, -1)); + if (distance == 1) + ra(stack_a); + else if (distance == 2) + { + ra(stack_a); + ra(stack_a); + } + else if (distance == 3) + { + rra(stack_a); + rra(stack_a); + } + else if (distance == 4) + rra(stack_a); + if (is_sorted(stack_a)) + return ; + pb(stack_a, stack_b); + sort_4(stack_a, stack_b); + pa(stack_a, stack_b); +} + +void simple_sort(t_list **stack_a, t_list **stack_b) +{ + int size; + + size = ft_lstsize(*stack_a); + if (is_sorted(stack_a) || size == 0 || size == 1) + return ; + if (size == 2) + sa(stack_a); + else if (size == 3) + sort_3(stack_a); + else if (size == 4) + sort_4(stack_a, stack_b); + else if (size == 5) + sort_5(stack_a, stack_b); +} diff --git a/sources/sort_small.c b/sources/sort_small.c new file mode 100755 index 0000000..d228bf8 --- /dev/null +++ b/sources/sort_small.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort_small.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/13 12:07:14 by rparodi #+# #+# */ +/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +static void sort_132(t_list **stack_a) +{ + ra(stack_a); + sa(stack_a); + rra(stack_a); +} + +static void sort_231(t_list **stack_a, t_list *head, int min) +{ + if (head->next->index == min) + sa(stack_a); + else + rra(stack_a); +} + +static void sort_312(t_list **stack_a, t_list *head, int min) +{ + if (head->next->index == min) + ra(stack_a); + else + { + sa(stack_a); + rra(stack_a); + } +} + +void sort_3(t_list **stack_a) +{ + t_list *head; + int min; + int next_min; + + head = *stack_a; + min = get_min(stack_a, -1); + next_min = get_min(stack_a, min); + if (is_sorted(stack_a) == 1) + return ; + if (head->index == min && head->next->index != next_min) + sort_132(stack_a); + else if (head->index == next_min) + sort_231(stack_a, head, min); + else + sort_312(stack_a, head, min); +} diff --git a/sources/swap.c b/sources/swap.c new file mode 100755 index 0000000..3e80f90 --- /dev/null +++ b/sources/swap.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/13 12:08:09 by rparodi #+# #+# */ +/* Updated: 2024/04/18 15:18:54 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +static int swap(t_list **stack) +{ + t_list *head; + t_list *next; + int tmp_val; + int tmp_index; + + if (ft_lstsize(*stack) < 2) + return (-1); + head = *stack; + next = head->next; + if (!head && !next) + error_message("Error swap", stack, NULL, NULL); + tmp_val = head->value; + tmp_index = head->index; + head->value = next->value; + head->index = next->index; + next->value = tmp_val; + next->index = tmp_index; + return (0); +} + +int sa(t_list **stack_a) +{ + if (swap(stack_a) == -1) + return (-1); + write(1, "sa\n", 3); + return (0); +} + +int sb(t_list **stack_b) +{ + if (swap(stack_b) == -1) + return (-1); + write(1, "sb\n", 3); + return (0); +} + +int ss(t_list **stack_a, t_list **stack_b) +{ + if ((ft_lstsize(*stack_a) < 2) || (ft_lstsize(*stack_b) < 2)) + return (-1); + swap(stack_a); + swap(stack_b); + write(1, "ss\n", 3); + return (0); +} diff --git a/sources/utilities.c b/sources/utilities.c new file mode 100755 index 0000000..bb7e23a --- /dev/null +++ b/sources/utilities.c @@ -0,0 +1,98 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utilities.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/13 12:08:14 by rparodi #+# #+# */ +/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +int index_distance_head(t_list **stack, int index) +{ + t_list *head; + int distance; + + distance = 0; + head = *stack; + while (head) + { + if (head->index == index) + break ; + distance++; + head = head->next; + } + return (distance); +} + +int get_min(t_list **stack, int val) +{ + t_list *head; + int min; + + head = *stack; + min = head->index; + while (head->next) + { + head = head->next; + if ((head->index < min) && head->index != val) + min = head->index; + } + return (min); +} + +static t_list *get_next_min(t_list **stack) +{ + t_list *head; + t_list *min; + int has_min; + + min = NULL; + has_min = 0; + head = *stack; + if (head) + { + while (head) + { + if ((head->index == -1) && (!has_min || head->value < min->value)) + { + min = head; + has_min = 1; + } + head = head->next; + } + } + return (min); +} + +void index_stack(t_list **stack) +{ + t_list *head; + int index; + + index = 0; + head = get_next_min(stack); + while (head) + { + head->index = index++; + head = get_next_min(stack); + } +} + +int is_sorted(t_list **stack) +{ + t_list *head; + + head = *stack; + while (head && head->next) + { + if (head->value > head->next->value) + return (0); + head = head->next; + } + return (1); +} diff --git a/sources/validation.c b/sources/validation.c new file mode 100755 index 0000000..c2b5338 --- /dev/null +++ b/sources/validation.c @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* validation.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/13 12:08:16 by rparodi #+# #+# */ +/* Updated: 2024/04/18 15:20:44 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/push_swap.h" + +static int ft_contains(int num, char **argv, int i) +{ + i++; + while (argv[i]) + { + if (ft_atoi(argv[i]) == num) + return (1); + i++; + } + return (0); +} + +static int ft_isnum(char *num) +{ + int i; + + i = 0; + if (num[0] == '-' || num[0] == '+') + i++; + if ((num[0] == '-' || num[0] == '+') && !num[1]) + return (0); + while (num[i]) + { + if (!ft_isdigit(num[i])) + return (0); + i++; + } + return (1); +} + +void ft_check_args_utils(int i, char **args) +{ + long tmp; + + tmp = 0; + if (i == 0) + { + while (args[i] != NULL) + { + tmp = ft_atoi(args[i]); + if (!ft_isnum(args[i]) || *args[i] == '\0') + error_message("not number heap", NULL, NULL, args); + if (ft_contains(tmp, args, i) == 1 || (ft_len(args[i]) > 11)) + error_message("overflow heap", NULL, NULL, args); + if (tmp < -2147483648 || tmp > 2147483647) + error_message("over int heap", NULL, NULL, args); + i++; + } + free_string(args); + } + else + { + while (args[i] != NULL) + { + tmp = ft_atoi(args[i]); + if (!ft_isnum(args[i]) || *args[i] == '\0') + error_message("not number stack", NULL, NULL, NULL); + if (ft_contains(tmp, args, i) == 1 || (ft_len(args[i]) > 11)) + error_message("overflow stack", NULL, NULL, NULL); + if (tmp < -2147483648 || tmp > 2147483647) + error_message("over int stack", NULL, NULL, NULL); + i++; + } + } +} + +void check_args(char **argv) +{ + int i; + char **args; + + if (argv[2] == NULL) + { + args = ft_split(argv[1]); + i = 0; + } + else + { + args = argv; + i = 1; + } + ft_check_args_utils(i, args); +}