Compare commits
21 Commits
428525b561
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
5a11c5f4cb | ||
|
d6d98b954f | ||
|
eb861b6b98 | ||
|
1b2796bdaf | ||
|
3c4dbf912b | ||
|
337fb1ad2f | ||
|
8aa1e733ac | ||
|
0b3ba46116 | ||
|
e0f584fd30 | ||
|
558f8edfab | ||
|
e7d88896e7 | ||
|
079902b834 | ||
|
3d54238d24 | ||
|
735c6af817 | ||
|
b993e0e68a | ||
|
d7cd178119 | ||
|
9ab20ed483 | ||
|
efab9452b1 | ||
|
3fc361618c | ||
|
770bfc428d | ||
|
2d351417dc |
55
Makefile
55
Makefile
@@ -6,7 +6,7 @@
|
||||
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2019/02/23 14:30:57 by tmaze #+# #+# #
|
||||
# Updated: 2019/03/06 11:37:28 by tmaze ### ########.fr #
|
||||
# Updated: 2019/03/18 14:27:44 by tmaze ### ########.fr #
|
||||
# #
|
||||
#******************************************************************************#
|
||||
|
||||
@@ -28,7 +28,7 @@ endif
|
||||
|
||||
# Compilator
|
||||
CC = gcc
|
||||
FLAGS = -Wall -Wextra -g
|
||||
FLAGS = -Wall -Wextra -Werror
|
||||
|
||||
# Folders
|
||||
LIBDIR = libft
|
||||
@@ -37,20 +37,24 @@ OBJDIR = objs
|
||||
INCDIR = includes libft
|
||||
|
||||
# Source files
|
||||
SRC = ps_push.c \
|
||||
SRC = ps_clean.c \
|
||||
ps_push.c \
|
||||
ps_rot.c \
|
||||
ps_rerot.c \
|
||||
ps_swap.c \
|
||||
ps_stktools.c \
|
||||
ps_stktools2.c
|
||||
ps_stktools2.c \
|
||||
ps_sort.c \
|
||||
ps_sorttools.c \
|
||||
ps_specsort.c
|
||||
|
||||
MAIN1 = checker.c
|
||||
MAIN2 = push_swap.c
|
||||
MAIN1 = checker.c
|
||||
MAIN2 = push_swap.c
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
OBJ1 = $(MAIN1:.c=.o)
|
||||
OBJ2 = $(MAIN2:.c=.o)
|
||||
OBJ1 = $(MAIN1:.c=.o)
|
||||
OBJ2 = $(MAIN2:.c=.o)
|
||||
|
||||
LIB = ft
|
||||
LIBFILE = libft.a
|
||||
@@ -59,54 +63,53 @@ LIBFILE = libft.a
|
||||
OBJP = $(addprefix $(OBJDIR)/, $(SRC:.c=.o))
|
||||
OBJP1 = $(addprefix $(OBJDIR)/, $(MAIN1:.c=.o))
|
||||
OBJP2 = $(addprefix $(OBJDIR)/, $(MAIN2:.c=.o))
|
||||
INCP = $(foreach dir, $(INCDIR), -I$(dir))
|
||||
OBJS_DIRS = $(sort $(dir $(OBJP)))
|
||||
INCP = $(foreach dir, $(INCDIR), -I$(dir))
|
||||
|
||||
# Default Rule
|
||||
DRULE = all
|
||||
|
||||
# Main rules
|
||||
default :
|
||||
@echo -e "$(PUR)===> $(GRE)$(NAME) : $(PUR) START RULE : $(DRULE) <===$(DEF)"
|
||||
@echo -e "$(PUR)===> $(GRE)$(NAME2) : $(PUR) START RULE : $(DRULE) <===$(DEF)"
|
||||
@make $(DRULE)
|
||||
@echo -e "$(PUR)===> $(GRE)$(NAME) : $(PUR) END RULE : $(DRULE) <===$(DEF)"
|
||||
@echo -e "$(PUR)===> $(GRE)$(NAME2) : $(PUR) END RULE : $(DRULE) <===$(DEF)"
|
||||
|
||||
all : $(NAME1) $(NAME2)
|
||||
|
||||
re : fclean default
|
||||
|
||||
# Compilation rules
|
||||
|
||||
$(OBJDIR)/%.o : $(SRCDIR)/%.c $(OBJDIR)
|
||||
$(OBJDIR)/%.o : $(SRCDIR)/%.c includes/push_swap.h
|
||||
@mkdir -p $(OBJDIR)
|
||||
$(CC) $(FLAGS) -c -o $@ $< $(INCP)
|
||||
|
||||
$(OBJDIR) :
|
||||
@echo -e "$(YEL)===> $(GRE)$(NAME) : $(YEL) Objects Compilation <===$(DEF)"
|
||||
@mkdir -p $(OBJDIR)
|
||||
@mkdir -p $(OBJS_DIRS)
|
||||
|
||||
$(LIBDIR)/$(LIBFILE) :
|
||||
@echo -e "$(YEL)===> $(GRE)$(NAME) : $(YEL) Librairy Compilation <===$(DEF)"
|
||||
@echo -e "$(YEL)===> $(GRE)$(NAME2) : $(YEL) Librairy Compilation <===$(DEF)"
|
||||
$(MAKE) -C $(LIBDIR) all
|
||||
|
||||
$(NAME1) : $(OBJP1) $(OBJP) $(LIBDIR)/$(LIBFILE)
|
||||
@echo -e "$(YEL)===> $(GRE)$(NAME) : $(YEL) Binary Compilation <===$(DEF)"
|
||||
@echo -e "$(YEL)===> $(GRE)$(NAME1) : $(YEL) Binary Compilation <===$(DEF)"
|
||||
$(CC) $(FLAGS) -o $@ $^ $(INCP) -L$(LIBDIR) -l$(LIB)
|
||||
|
||||
$(NAME2) : $(OBJP2) $(OBJP) $(LIBDIR)/$(LIBFILE)
|
||||
@echo -e "$(YEL)===> $(GRE)$(NAME) : $(YEL) Binary Compilation <===$(DEF)"
|
||||
@echo -e "$(YEL)===> $(GRE)$(NAME2) : $(YEL) Binary Compilation <===$(DEF)"
|
||||
$(CC) $(FLAGS) -o $@ $^ $(INCP) -L$(LIBDIR) -l$(LIB)
|
||||
|
||||
# Cleaner rules
|
||||
clean :
|
||||
@echo -e "$(RED)===> $(GRE)$(NAME) : $(RED) Delete Object Files <===$(DEF)"
|
||||
@rm -rf $(OBJDIR)
|
||||
@$(RM) -rf $(OBJDIR)
|
||||
@$(MAKE) -C $(LIBDIR) clean
|
||||
|
||||
fclean : clean
|
||||
@echo -e "$(RED)===> $(GRE)$(NAME) : $(RED) Delete Binary File <===$(DEF)"
|
||||
@rm -f $(NAME1) $(NAME2)
|
||||
@$(RM) -f $(NAME1) $(NAME2)
|
||||
@$(RM) -rf *.dSYM
|
||||
@$(MAKE) -C $(LIBDIR) fclean
|
||||
|
||||
re : fclean default
|
||||
|
||||
# Phony
|
||||
.PHONY = default all re clean fclean $(OBJDIR) $(NAME)
|
||||
.PHONY = default all re clean fclean
|
||||
# Color
|
||||
DEF = \033[0m
|
||||
BLA = \033[30m
|
||||
|
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/21 14:13:53 by tmaze #+# #+# */
|
||||
/* Updated: 2019/03/06 11:07:38 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/03/20 12:24:09 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -17,8 +17,6 @@
|
||||
# include <unistd.h>
|
||||
# include "libft.h"
|
||||
|
||||
# include <stdio.h>
|
||||
|
||||
typedef struct s_stack
|
||||
{
|
||||
int nb;
|
||||
@@ -33,11 +31,9 @@ typedef struct s_psdata
|
||||
size_t size_a;
|
||||
size_t size_b;
|
||||
t_list *op;
|
||||
char viz;
|
||||
} t_psdata;
|
||||
|
||||
char **ft_strsplitwhitespace(char *s);
|
||||
int ft_iswhitespace(char c);
|
||||
|
||||
t_stack *ps_stknew(int nb);
|
||||
void ps_stkpsh(t_psdata *data, char c, t_stack *new);
|
||||
t_stack *ps_stkpop(t_psdata *data, char c);
|
||||
@@ -53,7 +49,21 @@ void ps_rerot(t_psdata *data, char c);
|
||||
void ps_rrerot(t_psdata *data);
|
||||
|
||||
int is_op(char *buff);
|
||||
int get_params(t_psdata *data, int ac, char **av);
|
||||
int check_input(char *in, int *ret);
|
||||
t_list *add_op(t_psdata *data, char *op);
|
||||
int get_params(t_psdata *data, int ac, char **av, int viz);
|
||||
int check_input(t_psdata *data, char *in, int *ret);
|
||||
|
||||
void mark_groups(t_psdata *data);
|
||||
t_stack *get_last_a(t_psdata *data);
|
||||
int get_size_firsts(t_psdata *data);
|
||||
int get_size_last(t_psdata *data);
|
||||
int get_nb_groups(t_psdata *data);
|
||||
|
||||
void sortfor3(t_psdata *data);
|
||||
void sortfor5(t_psdata *data);
|
||||
|
||||
void clean_op(t_psdata *data);
|
||||
|
||||
int sort(t_psdata *data);
|
||||
|
||||
#endif
|
||||
|
2
libft
2
libft
Submodule libft updated: e6970465e9...f798c9ce1d
@@ -6,12 +6,38 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/23 13:35:15 by tmaze #+# #+# */
|
||||
/* Updated: 2019/03/02 18:28:08 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/03/20 12:26:46 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
#define VISU_SPEED 500000
|
||||
|
||||
void debug(t_psdata *data, char *com)
|
||||
{
|
||||
t_stack *as;
|
||||
t_stack *bs;
|
||||
|
||||
as = data->a;
|
||||
bs = data->b;
|
||||
ft_printf(FT_CLEAR);
|
||||
ft_printf("============== %4s ==============\n", com);
|
||||
while (as || bs)
|
||||
{
|
||||
if (as && bs)
|
||||
ft_printf("%5d || %5d\n", as->nb, bs->nb);
|
||||
else if (!as && bs)
|
||||
ft_printf("%5c || %5d\n", ' ', bs->nb);
|
||||
else if (as && !bs)
|
||||
ft_printf("%5d || %5c\n", as->nb, ' ');
|
||||
as = (as) ? as->next : as;
|
||||
bs = (bs) ? bs->next : bs;
|
||||
}
|
||||
ft_printf("==================================\n");
|
||||
usleep(VISU_SPEED);
|
||||
}
|
||||
|
||||
void exec_actions(t_psdata *data)
|
||||
{
|
||||
char *buff;
|
||||
@@ -20,23 +46,22 @@ void exec_actions(t_psdata *data)
|
||||
nop = data->op;
|
||||
while (nop && (buff = (char*)nop->content))
|
||||
{
|
||||
if (ft_strlen(buff) == 2)
|
||||
{
|
||||
if (buff[0] == 's' && buff[1] != 's')
|
||||
ps_swap(data, buff[1]);
|
||||
else if (buff[0] == 's' && buff[1] == 's')
|
||||
ps_sswap(data);
|
||||
else if (buff[0] == 'r' && buff[1] != 'r')
|
||||
ps_rot(data, buff[1]);
|
||||
else if (buff[0] == 'r' && buff[1] == 'r')
|
||||
ps_rrot(data);
|
||||
else if (buff[0] == 'p')
|
||||
ps_push(data, buff[1]);
|
||||
}
|
||||
else if (buff[2] != 'r')
|
||||
if (ft_strcmp(buff, "rra") == 0 || ft_strcmp(buff, "rrb") == 0)
|
||||
ps_rerot(data, buff[2]);
|
||||
else if (buff[2] == 'r')
|
||||
else if (ft_strcmp(buff, "rrr") == 0)
|
||||
ps_rrerot(data);
|
||||
else if (buff[0] == 's' && buff[1] != 's')
|
||||
ps_swap(data, buff[1]);
|
||||
else if (buff[0] == 's' && buff[1] == 's')
|
||||
ps_sswap(data);
|
||||
else if (buff[0] == 'r' && buff[1] != 'r')
|
||||
ps_rot(data, buff[1]);
|
||||
else if (buff[0] == 'r' && buff[1] == 'r')
|
||||
ps_rrot(data);
|
||||
else if (buff[0] == 'p')
|
||||
ps_push(data, buff[1]);
|
||||
if (data->viz)
|
||||
debug(data, buff);
|
||||
nop = nop->next;
|
||||
}
|
||||
}
|
||||
@@ -44,20 +69,26 @@ void exec_actions(t_psdata *data)
|
||||
int read_ops(t_psdata *data)
|
||||
{
|
||||
t_list *nop;
|
||||
char buff[5];
|
||||
char *buff;
|
||||
char tmp[4];
|
||||
int ret;
|
||||
|
||||
ft_memset(buff, '\0', 5);
|
||||
while ((ret = read(1, buff, 4)) > 0)
|
||||
buff = NULL;
|
||||
ft_bzero(tmp, 4);
|
||||
while ((ret = ft_getline(&buff)) > 0)
|
||||
{
|
||||
buff[ft_strlen(buff) - 1] = '\0';
|
||||
if (ret > 4 || !is_op(buff) || (nop = ft_lstnew(buff, 4)) == NULL)
|
||||
ft_strncpy(tmp, buff, 3);
|
||||
if (ft_strlen(buff) > 4 || !is_op(buff)
|
||||
|| (nop = ft_lstnew(tmp, 4)) == NULL)
|
||||
{
|
||||
ft_putendl_fd("Error", 2);
|
||||
ft_strdel(&buff);
|
||||
return (1);
|
||||
}
|
||||
ft_lstaddend(&(data->op), nop);
|
||||
ft_strdel(&buff);
|
||||
}
|
||||
ft_strdel(&buff);
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -67,10 +98,11 @@ void check_stack(t_psdata *data)
|
||||
int nb;
|
||||
|
||||
ind = data->a;
|
||||
nb = ind->nb;
|
||||
if (ind)
|
||||
nb = ind->nb;
|
||||
while (ind)
|
||||
{
|
||||
if (ind->nb < nb)
|
||||
if (ind->nb < nb || data->size_b > 0)
|
||||
{
|
||||
ft_putendl("KO");
|
||||
return ;
|
||||
@@ -78,8 +110,10 @@ void check_stack(t_psdata *data)
|
||||
nb = ind->nb;
|
||||
ind = ind->next;
|
||||
}
|
||||
if (!ind)
|
||||
if (!ind && data->size_b == 0)
|
||||
ft_putendl("OK");
|
||||
else
|
||||
ft_putendl("KO");
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
@@ -90,11 +124,11 @@ int main(int ac, char **av)
|
||||
|
||||
ps_initdata(&data);
|
||||
ret = 0;
|
||||
if (!get_params(&data, ac, av))
|
||||
if (!get_params(&data, ac, av, 1))
|
||||
return (0);
|
||||
new = data.a;
|
||||
if (new == NULL)
|
||||
ft_putendl("Empty");
|
||||
ft_putendl_fd("Error", 2);
|
||||
if (new == NULL || read_ops(&data))
|
||||
{
|
||||
ps_stkclean(&data);
|
||||
|
61
srcs/ps_clean.c
Normal file
61
srcs/ps_clean.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ps_clean.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/16 13:14:52 by tmaze #+# #+# */
|
||||
/* Updated: 2019/03/18 15:19:06 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
static void clean_push(t_psdata *data, t_list ***prec, t_list **it)
|
||||
{
|
||||
**prec = (*it)->next->next;
|
||||
ft_memdel(&((*it)->next->content));
|
||||
ft_memdel((void**)(&((*it)->next)));
|
||||
ft_memdel(&((*it)->content));
|
||||
ft_memdel((void**)it);
|
||||
*it = data->op;
|
||||
*prec = &(data->op);
|
||||
}
|
||||
|
||||
static void clean_swap(t_psdata *data, t_list ***prec, t_list **it)
|
||||
{
|
||||
**prec = (*it)->next;
|
||||
ft_strcpy((char*)((*it)->next->next->content), "sa");
|
||||
ft_memdel(&((*it)->content));
|
||||
ft_memdel((void**)it);
|
||||
*it = data->op;
|
||||
*prec = &(data->op);
|
||||
}
|
||||
|
||||
void clean_op(t_psdata *data)
|
||||
{
|
||||
t_list *it;
|
||||
t_list **prec;
|
||||
|
||||
it = data->op;
|
||||
prec = &(data->op);
|
||||
while (it && it->next)
|
||||
{
|
||||
if (((char*)(it->content))[0] == 'p'
|
||||
&& ((char*)(it->next->content))[0] == 'p'
|
||||
&& ((char*)(it->content))[1] != ((char*)(it->next->content))[1])
|
||||
clean_push(data, &prec, &it);
|
||||
else if (it->next->next && ((char*)(it->content))[0] == 'p'
|
||||
&& ((char*)(it->content))[1] == 'b'
|
||||
&& ((char*)(it->next->next->content))[0] == 'p'
|
||||
&& ((char*)(it->next->next->content))[1] == 'a'
|
||||
&& ft_strcmp((char*)(it->next->content), "rra") == 0)
|
||||
clean_swap(data, &prec, &it);
|
||||
else
|
||||
{
|
||||
prec = &(it->next);
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
}
|
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/21 16:32:04 by tmaze #+# #+# */
|
||||
/* Updated: 2019/03/02 17:37:07 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/03/26 15:06:28 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -16,28 +16,26 @@ void ps_rerot(t_psdata *data, char c)
|
||||
{
|
||||
t_stack *s;
|
||||
t_stack *tmp;
|
||||
size_t size;
|
||||
size_t *size;
|
||||
|
||||
if (c == 'a')
|
||||
s = data->a;
|
||||
if (c == 'a')
|
||||
size = data->size_a;
|
||||
size = &(data->size_a);
|
||||
else if (c == 'b')
|
||||
s = data->b;
|
||||
if (c == 'b')
|
||||
size = data->size_b;
|
||||
if (s != NULL && size > 2)
|
||||
size = &(data->size_b);
|
||||
if (s != NULL && *size > 2)
|
||||
{
|
||||
tmp = s->next;
|
||||
while (tmp->next != NULL)
|
||||
{
|
||||
tmp = tmp->next;
|
||||
while (tmp->next != NULL && (tmp = tmp->next))
|
||||
s = s->next;
|
||||
}
|
||||
s->next = NULL;
|
||||
(*size)--;
|
||||
ps_stkpsh(data, c, tmp);
|
||||
}
|
||||
else if (s != NULL && size == 2)
|
||||
else if (s != NULL && *size == 2)
|
||||
ps_swap(data, c);
|
||||
}
|
||||
|
||||
|
89
srcs/ps_sort.c
Normal file
89
srcs/ps_sort.c
Normal file
@@ -0,0 +1,89 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ps_sort.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/16 13:21:02 by tmaze #+# #+# */
|
||||
/* Updated: 2019/03/19 12:35:27 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
static int join_lists(t_psdata *data, t_stack **last, int inds, int inds2)
|
||||
{
|
||||
while ((*last)->ind == inds2 || data->size_b > 0)
|
||||
{
|
||||
if (((*last)->ind != inds2 && data->size_b > 0)
|
||||
|| (data->size_b > 0 && data->b->nb > (*last)->nb))
|
||||
{
|
||||
if (add_op(data, "pa\0") == NULL)
|
||||
return (0);
|
||||
ps_push(data, 'a');
|
||||
data->a->ind = inds;
|
||||
}
|
||||
else if (data->size_b == 0 || data->b->nb < (*last)->nb)
|
||||
{
|
||||
if (add_op(data, "rra") == NULL)
|
||||
return (0);
|
||||
ps_rerot(data, 'a');
|
||||
data->a->ind = inds;
|
||||
(*last) = get_last_a(data);
|
||||
}
|
||||
(*last) = get_last_a(data);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int rotate_list(t_psdata *data, t_stack **last)
|
||||
{
|
||||
t_list *tmp;
|
||||
|
||||
if (get_nb_groups(data) > 2)
|
||||
{
|
||||
if (get_nb_groups(data) != 2
|
||||
&& data->a->ind != (*last)->ind && add_op(data, "rra") == NULL)
|
||||
return (0);
|
||||
else if (get_nb_groups(data) != 2 && data->a->ind != (*last)->ind)
|
||||
{
|
||||
ps_rerot(data, 'a');
|
||||
(*last) = get_last_a(data);
|
||||
while ((*last)->ind == data->a->ind
|
||||
&& (tmp = add_op(data, "rra")) != NULL)
|
||||
{
|
||||
ps_rerot(data, 'a');
|
||||
(*last) = get_last_a(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int sort(t_psdata *data)
|
||||
{
|
||||
t_list *tmp;
|
||||
t_stack *last;
|
||||
int inds;
|
||||
int inds2;
|
||||
|
||||
last = get_last_a(data);
|
||||
inds = last->ind + 1;
|
||||
while (data->a->ind != last->ind)
|
||||
{
|
||||
inds2 = data->a->ind;
|
||||
while (data->a && data->a->ind == inds2
|
||||
&& (tmp = add_op(data, "pb\0")) != NULL)
|
||||
ps_push(data, 'b');
|
||||
if (tmp == NULL)
|
||||
return (0);
|
||||
inds2 = last->ind;
|
||||
if (join_lists(data, &last, inds, inds2) == 0)
|
||||
return (0);
|
||||
if (rotate_list(data, &last) == 0)
|
||||
return (0);
|
||||
inds++;
|
||||
}
|
||||
return (1);
|
||||
}
|
100
srcs/ps_sorttools.c
Normal file
100
srcs/ps_sorttools.c
Normal file
@@ -0,0 +1,100 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ps_sorttools.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/16 13:23:27 by tmaze #+# #+# */
|
||||
/* Updated: 2019/03/16 13:23:57 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
void mark_groups(t_psdata *data)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int inds;
|
||||
|
||||
ptr = data->a;
|
||||
inds = 1;
|
||||
while (ptr)
|
||||
{
|
||||
ptr->ind = inds;
|
||||
if (ptr->next && ptr->nb > ptr->next->nb)
|
||||
inds++;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
}
|
||||
|
||||
t_stack *get_last_a(t_psdata *data)
|
||||
{
|
||||
t_stack *ptr;
|
||||
|
||||
ptr = data->a;
|
||||
while (ptr && ptr->next)
|
||||
ptr = ptr->next;
|
||||
return (ptr);
|
||||
}
|
||||
|
||||
int get_size_firsts(t_psdata *data)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int inds;
|
||||
int i;
|
||||
|
||||
inds = data->a->ind;
|
||||
i = 0;
|
||||
ptr = data->a;
|
||||
while (ptr && ptr->ind == inds && (i += 1))
|
||||
ptr = ptr->next;
|
||||
if (ptr)
|
||||
{
|
||||
inds = ptr->ind;
|
||||
while (ptr && ptr->ind == inds && (i += 1))
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
int get_size_last(t_psdata *data)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int inds;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
ptr = data->a;
|
||||
while (ptr && ptr->next)
|
||||
ptr = ptr->next;
|
||||
inds = ptr->ind;
|
||||
ptr = data->a;
|
||||
while (ptr)
|
||||
{
|
||||
i += (ptr->ind == inds) ? 1 : 0;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
int get_nb_groups(t_psdata *data)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int ind;
|
||||
int ret;
|
||||
|
||||
ret = 0;
|
||||
ptr = data->a;
|
||||
ind = ptr->ind;
|
||||
while (ptr)
|
||||
{
|
||||
if (ret == 0 || ptr->ind != ind)
|
||||
{
|
||||
ret++;
|
||||
ind = ptr->ind;
|
||||
}
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return (ret);
|
||||
}
|
85
srcs/ps_specsort.c
Normal file
85
srcs/ps_specsort.c
Normal file
@@ -0,0 +1,85 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ps_specsort.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/16 13:18:14 by tmaze #+# #+# */
|
||||
/* Updated: 2019/03/16 13:28:12 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
static void get_min_max(t_psdata *data, int *min_max, int *ind_min_max)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int i;
|
||||
|
||||
min_max[0] = FT_INT_MAX;
|
||||
min_max[1] = FT_INT_MIN;
|
||||
ptr = data->a;
|
||||
i = 0;
|
||||
while (ptr)
|
||||
{
|
||||
if (ptr->nb < min_max[0] && (ind_min_max[0] = i) == i)
|
||||
min_max[0] = ptr->nb;
|
||||
if (ptr->nb > min_max[1] && (ind_min_max[1] = i) == i)
|
||||
min_max[1] = ptr->nb;
|
||||
i++;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
}
|
||||
|
||||
void sortfor3(t_psdata *data)
|
||||
{
|
||||
int min_max[2];
|
||||
int ind_min_max[2];
|
||||
|
||||
get_min_max(data, min_max, ind_min_max);
|
||||
while (ind_min_max[0] != 0 || ind_min_max[1] != 2)
|
||||
{
|
||||
if (ind_min_max[0] == ind_min_max[1] - 1)
|
||||
ft_printf("sa\n");
|
||||
if (ind_min_max[0] == ind_min_max[1] - 1)
|
||||
ps_swap(data, 'a');
|
||||
if (ind_min_max[1] == 0)
|
||||
ft_printf("ra\n");
|
||||
if (ind_min_max[1] == 0)
|
||||
ps_rot(data, 'a');
|
||||
if (ind_min_max[0] == 2 && ind_min_max[1] == 1)
|
||||
ft_printf("rra\n");
|
||||
if (ind_min_max[0] == 2 && ind_min_max[1] == 1)
|
||||
ps_rerot(data, 'a');
|
||||
get_min_max(data, min_max, ind_min_max);
|
||||
}
|
||||
}
|
||||
|
||||
void sortfor5(t_psdata *data)
|
||||
{
|
||||
ft_printf("pb\n");
|
||||
ps_push(data, 'b');
|
||||
ft_printf("pb\n");
|
||||
ps_push(data, 'b');
|
||||
if (data->b->nb > data->b->next->nb)
|
||||
ft_printf("sb\n");
|
||||
if (data->b->nb > data->b->next->nb)
|
||||
ps_swap(data, 'b');
|
||||
sortfor3(data);
|
||||
data->b->ind = 1;
|
||||
data->b->next->ind = 1;
|
||||
data->a->ind = 2;
|
||||
data->a->next->ind = 2;
|
||||
data->a->next->next->ind = 2;
|
||||
while (data->a->ind == 2 || data->size_b > 0)
|
||||
{
|
||||
if (data->a->ind != 2 || (data->b && data->b->nb < data->a->nb))
|
||||
ft_printf("pa\n");
|
||||
if (data->a->ind != 2 || (data->b && data->b->nb < data->a->nb))
|
||||
ps_push(data, 'a');
|
||||
data->a->ind = 3;
|
||||
ft_printf("ra\n");
|
||||
ps_rot(data, 'a');
|
||||
}
|
||||
}
|
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/25 12:44:08 by tmaze #+# #+# */
|
||||
/* Updated: 2019/03/03 13:59:43 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/03/20 16:49:01 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -19,11 +19,13 @@ void ps_initdata(t_psdata *data)
|
||||
data->size_a = 0;
|
||||
data->size_b = 0;
|
||||
data->op = NULL;
|
||||
data->viz = '\0';
|
||||
}
|
||||
|
||||
int check_input(char *in, int *ret)
|
||||
int check_input(t_psdata *data, char *in, int *ret)
|
||||
{
|
||||
int i;
|
||||
t_stack *ptr;
|
||||
|
||||
i = 0;
|
||||
if (in[i] && ft_issign(in[i]))
|
||||
@@ -31,37 +33,45 @@ int check_input(char *in, int *ret)
|
||||
while (in[i] && ft_isdigit(in[i]))
|
||||
i++;
|
||||
if (in[i] == '\0' && ft_hasdigit(in) && ft_atois(in, ret))
|
||||
{
|
||||
ptr = data->a;
|
||||
while (ptr)
|
||||
{
|
||||
if (*ret == ptr->nb)
|
||||
return (0);
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int get_params(t_psdata *data, int ac, char **av)
|
||||
int get_params(t_psdata *data, int ac, char **av, int viz)
|
||||
{
|
||||
t_stack *new;
|
||||
char **tab;
|
||||
int nb;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = ac;
|
||||
nb = 0;
|
||||
while (--i > 0 && !(j = 0))
|
||||
if ((tab = ft_strsplitwhitespace(av[i])) != NULL)
|
||||
while (--i > 0)
|
||||
{
|
||||
if (i == 1 && viz && ft_strcmp(av[i], "-v") == 0)
|
||||
data->viz = 'a';
|
||||
else if (check_input(data, av[i], &nb) && (new = ps_stknew(nb)) != NULL)
|
||||
{
|
||||
while (tab[j])
|
||||
j++;
|
||||
while (--j >= 0)
|
||||
if (check_input(tab[j], &nb) && (new = ps_stknew(nb)) != NULL)
|
||||
ps_stkpsh(data, 'a', new);
|
||||
else
|
||||
{
|
||||
ft_putendl_fd("Error", 2);
|
||||
ps_stkclean(data);
|
||||
return (0);
|
||||
}
|
||||
ft_del_words_tables(&tab);
|
||||
new->ind = 0;
|
||||
ps_stkpsh(data, 'a', new);
|
||||
}
|
||||
return (1);
|
||||
else
|
||||
{
|
||||
if (ft_putendl_fd2("Error", 2))
|
||||
ft_putendl_fd("Error", 2);
|
||||
ps_stkclean(data);
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
return (ac != 1);
|
||||
}
|
||||
|
||||
int is_op(char *buff)
|
||||
@@ -75,3 +85,12 @@ int is_op(char *buff)
|
||||
i++;
|
||||
return (i < 11);
|
||||
}
|
||||
|
||||
t_list *add_op(t_psdata *data, char *op)
|
||||
{
|
||||
t_list *new;
|
||||
|
||||
if ((new = ft_lstnew(op, 4)) != NULL)
|
||||
ft_lstaddend(&(data->op), new);
|
||||
return (new);
|
||||
}
|
||||
|
114
srcs/push_swap.c
114
srcs/push_swap.c
@@ -6,114 +6,48 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/03 11:41:49 by tmaze #+# #+# */
|
||||
/* Updated: 2019/03/06 12:10:10 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/03/19 15:24:24 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
int sort(t_psdata *data, int size)
|
||||
void print_ops(t_psdata *data)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int i;
|
||||
t_list *it;
|
||||
|
||||
printf("start sort %d\n", size);
|
||||
if (size > 2)
|
||||
it = data->op;
|
||||
while (it)
|
||||
{
|
||||
if (!sort(data, ((size % 2) ? size + 1 : size) / 2))
|
||||
return (0);
|
||||
if (!sort(data, size - (((size % 2) ? size + 1 : size) / 2)))
|
||||
return (0);
|
||||
ft_putendl(it->content);
|
||||
it = it->next;
|
||||
}
|
||||
else if (size == 1)
|
||||
return (1);
|
||||
i = 0;
|
||||
ptr = data->a;
|
||||
while (i < ((size % 2) ? size + 1 : size) / 2)
|
||||
{
|
||||
ptr->ind = 1;
|
||||
ptr = ptr->next;
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
while (ptr && i < (size - (((size % 2) ? size + 1 : size) / 2)))
|
||||
{
|
||||
ptr->ind = 2;
|
||||
ptr = ptr->next;
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
ptr = data->a;
|
||||
while (ptr)
|
||||
{
|
||||
printf("@ind %d: nb=%d ind=%d\n", i, ptr->nb, ptr->ind);
|
||||
ptr = ptr->next;
|
||||
i++;
|
||||
}
|
||||
while (data->a->ind == 1)
|
||||
{
|
||||
printf("pb\n");
|
||||
if (!ps_push(data, 'b'))
|
||||
return (0);
|
||||
if (data->size_b > 1)
|
||||
{
|
||||
printf("rb\n");
|
||||
ps_rot(data, 'b');
|
||||
}
|
||||
}
|
||||
while (data->a->ind != 0 || data->size_b > 0)
|
||||
{
|
||||
printf("data->a->ind %d\ndata->a->nb %d\ndata->size_b %zu\n", data->a->ind, data->a->nb, data->size_b);
|
||||
if ((data->a->ind != 0 && data->size_b > 0 && data->a->nb > data->b->nb)
|
||||
|| (data->a->ind == 0 && data->size_b > 0))
|
||||
{
|
||||
printf("pa\n");
|
||||
ps_push(data, 'a');
|
||||
}
|
||||
printf("mise a zero:\ndata->a->ind %d\ndata->a->nb %d\n", data->a->ind, data->a->nb);
|
||||
data->a->ind = 0;
|
||||
printf("ra\n");
|
||||
ps_rot(data, 'a');
|
||||
i = 0;
|
||||
ptr = data->a;
|
||||
while (ptr)
|
||||
{
|
||||
printf("@ind %d: nb=%d ind=%d\n", i, ptr->nb, ptr->ind);
|
||||
ptr = ptr->next;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_stack *ptr;
|
||||
t_psdata data;
|
||||
|
||||
ps_initdata(&data);
|
||||
if (!get_params(&data, ac, av))
|
||||
if (!get_params(&data, ac, av, 0))
|
||||
return (0);
|
||||
ptr = data.a;
|
||||
if (ptr == NULL)
|
||||
ft_putendl("Empty");
|
||||
while (ptr)
|
||||
if (data.a == NULL)
|
||||
ft_putendl_fd("Error", 2);
|
||||
if (data.size_a == 3)
|
||||
sortfor3(&data);
|
||||
else if (data.size_a == 5)
|
||||
sortfor5(&data);
|
||||
if (data.size_a != 0 && data.size_a != 3 && data.size_a != 5)
|
||||
{
|
||||
ptr->ind = 0;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
if (!sort(&data, data.size_a))
|
||||
{
|
||||
printf("Error\n");
|
||||
ps_stkclean(&data);
|
||||
return (1);
|
||||
}
|
||||
ptr = data.a;
|
||||
while (ptr)
|
||||
{
|
||||
printf("%d\n", ptr->nb);
|
||||
ptr->ind = 0;
|
||||
ptr = ptr->next;
|
||||
mark_groups(&data);
|
||||
if (sort(&data) == 0)
|
||||
ft_putendl_fd("Error", 2);
|
||||
else
|
||||
{
|
||||
clean_op(&data);
|
||||
print_ops(&data);
|
||||
}
|
||||
}
|
||||
ps_stkclean(&data);
|
||||
return (0);
|
||||
}
|
||||
|
Reference in New Issue
Block a user