Push the final version

This commit is contained in:
Raphaël 2024-04-20 14:48:16 +02:00
commit d0fe1fb2fb
28 changed files with 1459 additions and 0 deletions

138
Makefile Executable file
View file

@ -0,0 +1,138 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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}

66
includes/push_swap.h Executable file
View file

@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <unistd.h>
# include <stdlib.h>
# include <limits.h>
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

54
libft/ft_atoi.c Executable file
View file

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

20
libft/ft_isdigit.c Executable file
View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

30
libft/ft_lstadd_back.c Executable file
View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}
}

19
libft/ft_lstadd_front.c Executable file
View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}

27
libft/ft_lstlast.c Executable file
View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

26
libft/ft_lstnew.c Executable file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

28
libft/ft_lstsize.c Executable file
View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

32
libft/ft_memcpy.c Executable file
View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

100
libft/ft_split.c Executable file
View file

@ -0,0 +1,100 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

29
libft/ft_strchr.c Executable file
View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

26
libft/ft_strdup.c Executable file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

31
libft/ft_strlcpy.c Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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));
}

23
libft/ft_strlen.c Normal file
View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

31
libft/ft_strncmp.c Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

32
libft/ft_substr.c Executable file
View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

53
sources/error.c Executable file
View file

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

67
sources/main.c Executable file
View file

@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

56
sources/push.c Executable file
View file

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

62
sources/reverse.c Executable file
View file

@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* reverse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

54
sources/rotate.c Executable file
View file

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rotate.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

62
sources/sort_radix.c Executable file
View file

@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_radix.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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++;
}
}

79
sources/sort_simple.c Executable file
View file

@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_simple.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

58
sources/sort_small.c Executable file
View file

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_small.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

61
sources/swap.c Executable file
View file

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

98
sources/utilities.c Executable file
View file

@ -0,0 +1,98 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utilities.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

97
sources/validation.c Executable file
View file

@ -0,0 +1,97 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* validation.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}