From 772733d41e4d8509ea0e470fb289a20567ca3260 Mon Sep 17 00:00:00 2001 From: kinou-p Date: Mon, 17 Jan 2022 16:20:46 +0100 Subject: [PATCH] done --- Makefile | 8 ++- indexing.c | 12 ++-- libft/Makefile | 3 +- libft/ft_atoi.c | 8 +-- libft/ft_bzero.c | 4 +- libft/ft_calloc.c | 6 +- libft/ft_itoa.c | 8 +-- libft/ft_lstbeforelast.c | 6 +- libft/ft_lstclear.c | 4 +- libft/ft_lstnew.c | 6 +- libft/ft_memccpy.c | 6 +- libft/ft_memchr.c | 6 +- libft/ft_memcmp.c | 6 +- libft/ft_memcpy.c | 6 +- libft/ft_memmove.c | 8 +-- libft/ft_memset.c | 4 +- libft/ft_putnbr_fd.c | 6 +- libft/ft_split.c | 17 +++--- libft/ft_strchr.c | 10 ++-- libft/ft_strjoin.c | 42 ------------- libft/ft_strlcpy.c | 6 +- libft/ft_strlen.c | 4 +- libft/ft_strmapi.c | 6 +- libft/ft_strncmp.c | 4 +- libft/ft_strnstr.c | 8 +-- libft/ft_strrchr.c | 4 +- libft/ft_strtrim.c | 7 ++- libft/libft.h | 9 ++- main.c | 20 +++---- optimise_move.c | 124 +++++++++++++++++++++++++++++++++++++++ optimise_move2.c | 83 ++++++++++++++++++++++++++ process.c | 100 +++++++++++++++---------------- push_swap.c | 82 ++++++++++---------------- push_swap.h | 81 ++++++++++++++----------- set_a.c | 65 +++++++++++--------- sort_liltte.c | 0 sorting.c | 68 ++++----------------- s_list.c => t_slist.c | 32 +++++----- 38 files changed, 500 insertions(+), 379 deletions(-) delete mode 100644 libft/ft_strjoin.c create mode 100644 optimise_move.c create mode 100644 optimise_move2.c create mode 100644 sort_liltte.c rename s_list.c => t_slist.c (70%) diff --git a/Makefile b/Makefile index 57c4cc1..c4a88c3 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: apommier +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2021/11/13 13:06:47 by apommier #+# #+# # -# Updated: 2022/01/14 20:19:54 by apommier ### ########.fr # +# Updated: 2022/01/17 14:08:51 by apommier ### ########.fr # # # # **************************************************************************** # @@ -15,11 +15,13 @@ SRCS = push_swap.c \ process.c\ main.c\ sorting.c\ - s_list.c\ + t_slist.c\ set_a.c\ + optimise_move.c\ + optimise_move2.c\ indexing.c OBJS = ${SRCS:.c=.o} -CFLAGS = +CFLAGS = -Wall -Wextra -Werror RM = rm -rf LIBFT = ./libft diff --git a/indexing.c b/indexing.c index 01417e2..47de8a0 100644 --- a/indexing.c +++ b/indexing.c @@ -12,21 +12,21 @@ #include "push_swap.h" -void lst_indexing(t_list *list) +void lst_indexing(t_list *list) { - int index; - t_list *swap; + int index; + t_list *swap; t_list *save; swap = list; save = list; index = 0; - while(save) + while (save) { index = 1; while (swap) { - if (*(int*)save->nbr > *(int*)swap->nbr) + if (*(int *)save->nbr > *(int *)swap->nbr) index++; swap = swap->next; } @@ -34,4 +34,4 @@ void lst_indexing(t_list *list) swap = list; save = save->next; } -} \ No newline at end of file +} diff --git a/libft/Makefile b/libft/Makefile index 626fa36..d169134 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -24,7 +24,6 @@ SRCS = ft_memset.c \ ft_calloc.c \ ft_strdup.c \ ft_substr.c \ - ft_strjoin.c \ ft_strtrim.c \ ft_split.c \ ft_itoa.c \ @@ -38,7 +37,7 @@ BONUS_C = ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c ft_lstadd_back ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c ft_lstbeforelast.c BONUS_O = ${BONUS_C:.c=.o} CC = gcc -CFLAGS = -Wall -Wextra -Werror +CFLAGS = -Wall -Wextra RM = rm -rf .c.o: diff --git a/libft/ft_atoi.c b/libft/ft_atoi.c index a9db23e..d7d0a83 100644 --- a/libft/ft_atoi.c +++ b/libft/ft_atoi.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/29 00:09:17 by apommier #+# #+# */ -/* Updated: 2020/12/12 12:05:13 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:24:37 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,9 @@ int ft_atoi(const char *nptr) { - int i; - int nbr; - int minus; + int i; + int nbr; + int minus; minus = 1; nbr = 0; diff --git a/libft/ft_bzero.c b/libft/ft_bzero.c index 6480413..5baa4fe 100644 --- a/libft/ft_bzero.c +++ b/libft/ft_bzero.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/29 00:09:48 by apommier #+# #+# */ -/* Updated: 2020/11/29 17:02:33 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:26:30 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ void ft_bzero(void *s, size_t n) char *p; int i; - p = (char*)s; + p = (char *)s; i = 0; while (n != 0) { diff --git a/libft/ft_calloc.c b/libft/ft_calloc.c index 08404eb..2856eb2 100644 --- a/libft/ft_calloc.c +++ b/libft/ft_calloc.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/29 00:09:57 by apommier #+# #+# */ -/* Updated: 2020/12/16 16:13:27 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:27:24 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,8 @@ void *ft_calloc(size_t nmemb, size_t size) i = 0; new = malloc(size * nmemb); + if (!new) + return (0); if (new) { while (size * nmemb - i) @@ -27,5 +29,5 @@ void *ft_calloc(size_t nmemb, size_t size) i++; } } - return ((void*)new); + return ((void *)new); } diff --git a/libft/ft_itoa.c b/libft/ft_itoa.c index 7de7203..70f2fd0 100644 --- a/libft/ft_itoa.c +++ b/libft/ft_itoa.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_itoa.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/08 18:20:19 by apommier #+# #+# */ -/* Updated: 2020/12/20 11:52:40 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:28:01 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ static char *fill(long n, int j, int minus) char *dest; j += minus; - dest = (char*)malloc(sizeof(char) * (j + 1)); + dest = (char *)malloc(sizeof(char) * (j + 1)); if (dest == 0) return (0); dest[j] = 0; @@ -34,7 +34,7 @@ static char *fill(long n, int j, int minus) return (dest); } -char *ft_itoa(int n) +char *ft_itoa(int n) { long i; long k; diff --git a/libft/ft_lstbeforelast.c b/libft/ft_lstbeforelast.c index 7218e24..535dd53 100644 --- a/libft/ft_lstbeforelast.c +++ b/libft/ft_lstbeforelast.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/12/09 19:16:55 by apommier #+# #+# */ -/* Updated: 2022/01/13 19:00:16 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:28:22 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ t_list *ft_lstbeforelast(t_list *lst) { - t_list *save; + t_list *save; save = 0; if (!lst) @@ -25,4 +25,4 @@ t_list *ft_lstbeforelast(t_list *lst) lst = lst->next; } return (save); -} \ No newline at end of file +} diff --git a/libft/ft_lstclear.c b/libft/ft_lstclear.c index 1251484..152fb31 100644 --- a/libft/ft_lstclear.c +++ b/libft/ft_lstclear.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/09 19:58:04 by apommier #+# #+# */ -/* Updated: 2022/01/07 17:55:06 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:28:39 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ void ft_lstclear(t_list **lst, void (*del)(void*)) { - t_list *chr; + t_list *chr; chr = *lst; while (*lst) diff --git a/libft/ft_lstnew.c b/libft/ft_lstnew.c index 1acbb5b..e108f42 100644 --- a/libft/ft_lstnew.c +++ b/libft/ft_lstnew.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/09 01:06:20 by apommier #+# #+# */ -/* Updated: 2022/01/15 23:45:37 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:29:09 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,9 @@ t_list *ft_lstnew(void *content) { - t_list *new; + t_list *new; - new = (t_list*)malloc(sizeof(t_list)); + new = (t_list *)malloc(sizeof(t_list)); if (!new) return (0); new->swap = 0; diff --git a/libft/ft_memccpy.c b/libft/ft_memccpy.c index c305fc1..0b1644c 100644 --- a/libft/ft_memccpy.c +++ b/libft/ft_memccpy.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/29 00:11:04 by apommier #+# #+# */ -/* Updated: 2020/12/12 14:07:04 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:29:38 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,8 +19,8 @@ void *ft_memccpy(void *dest, const void *src, int c, size_t n) size_t i; i = 0; - p = (char*)dest; - p1 = (const char*)src; + p = (char *)dest; + p1 = (const char *)src; while (i < n) { p[i] = p1[i]; diff --git a/libft/ft_memchr.c b/libft/ft_memchr.c index 6a0fb37..ce8f294 100644 --- a/libft/ft_memchr.c +++ b/libft/ft_memchr.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/29 00:11:39 by apommier #+# #+# */ -/* Updated: 2020/12/12 13:53:45 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:29:51 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,9 @@ void *ft_memchr(const void *s, int c, size_t n) { - unsigned char *str; + unsigned char *str; - str = (unsigned char*)s; + str = (unsigned char *)s; while (n) { if ((unsigned char)c == *str) diff --git a/libft/ft_memcmp.c b/libft/ft_memcmp.c index 58a837c..f9af3ee 100644 --- a/libft/ft_memcmp.c +++ b/libft/ft_memcmp.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/29 00:11:51 by apommier #+# #+# */ -/* Updated: 2020/12/12 14:14:51 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:30:32 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,8 +19,8 @@ int ft_memcmp(const void *s1, const void *s2, size_t n) i = 0; while (n) { - if (((unsigned char*)s1)[i] != ((unsigned char*)s2)[i]) - return (((unsigned char*)s1)[i] - ((unsigned char*)s2)[i]); + if (((unsigned char *)s1)[i] != ((unsigned char *)s2)[i]) + return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]); n--; i++; } diff --git a/libft/ft_memcpy.c b/libft/ft_memcpy.c index a5abdd8..92c0480 100644 --- a/libft/ft_memcpy.c +++ b/libft/ft_memcpy.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/29 00:12:03 by apommier #+# #+# */ -/* Updated: 2020/12/16 16:14:24 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:30:43 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,8 +20,8 @@ void *ft_memcpy(void *dest, const void *src, size_t n) if (!dest && !src) return (dest); - p = (char*)dest; - p1 = (const char*)src; + p = (char *)dest; + p1 = (const char *)src; i = 0; while (i < n) { diff --git a/libft/ft_memmove.c b/libft/ft_memmove.c index 829c13f..0a8a6db 100644 --- a/libft/ft_memmove.c +++ b/libft/ft_memmove.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/29 00:12:14 by apommier #+# #+# */ -/* Updated: 2020/12/13 21:15:13 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:31:05 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ void *ft_memmove(void *dest, const void *src, size_t n) { - size_t i; + size_t i; i = 0; if (!dest && !src) @@ -23,7 +23,7 @@ void *ft_memmove(void *dest, const void *src, size_t n) { while (n) { - ((unsigned char*)dest)[n - 1] = ((unsigned char*)src)[n - 1]; + ((unsigned char *)dest)[n - 1] = ((unsigned char *)src)[n - 1]; n--; } } @@ -31,7 +31,7 @@ void *ft_memmove(void *dest, const void *src, size_t n) { while (i < n) { - ((unsigned char*)dest)[i] = ((unsigned char*)src)[i]; + ((unsigned char *)dest)[i] = ((unsigned char *)src)[i]; i++; } } diff --git a/libft/ft_memset.c b/libft/ft_memset.c index da16346..58dacd9 100644 --- a/libft/ft_memset.c +++ b/libft/ft_memset.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/29 00:12:24 by apommier #+# #+# */ -/* Updated: 2020/12/11 15:48:06 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:31:28 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ void *ft_memset(void *s, int c, size_t n) unsigned char *p; i = 0; - p = (unsigned char*)s; + p = (unsigned char *)s; while (n > 0) { p[i] = (unsigned char)c; diff --git a/libft/ft_putnbr_fd.c b/libft/ft_putnbr_fd.c index faa08fd..eb6aebd 100644 --- a/libft/ft_putnbr_fd.c +++ b/libft/ft_putnbr_fd.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_putnbr_fd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/08 23:25:57 by apommier #+# #+# */ -/* Updated: 2020/12/12 09:42:09 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:31:56 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ void ft_putnbr_fd(int n, int fd) { - long nbr; + long nbr; nbr = n; if (nbr < 0) diff --git a/libft/ft_split.c b/libft/ft_split.c index 3c39bc6..1fd6a7f 100644 --- a/libft/ft_split.c +++ b/libft/ft_split.c @@ -3,23 +3,23 @@ /* ::: :::::::: */ /* ft_split.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/07 00:54:12 by apommier #+# #+# */ -/* Updated: 2020/12/13 23:07:09 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:34:08 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -static int fill_tab(char *s, char c, char **dest, size_t index) +static int fill_tab(char *s, char c, char **dest, size_t index) { int i; i = 0; while (s[i] != c && s[i]) i++; - dest[index] = (char*)ft_calloc(i + 1, sizeof(char)); + dest[index] = (char *)ft_calloc(i + 1, sizeof(char)); if (dest[index] == 0) return (0); i = 0; @@ -31,7 +31,7 @@ static int fill_tab(char *s, char c, char **dest, size_t index) return (1); } -static void call(char *s, char c, char **dest, int j) +static void call(char *s, char c, char **dest, int j) { int index; int k; @@ -51,7 +51,7 @@ static void call(char *s, char c, char **dest, int j) } } -char **ft_split(char const *s, char c) +char **ft_split(char const *s, char c) { int i; int j; @@ -71,9 +71,10 @@ char **ft_split(char const *s, char c) while (s[i] == c && s[i]) i++; } - if (!(dest = (char**)malloc(sizeof(char*) * (i + j)))) + dest = (char **)malloc(sizeof(char *) * (i + j)); + if (!dest) return (0); dest[j] = 0; - call((char*)s, c, dest, j); + call((char *)s, c, dest, j); return (dest); } diff --git a/libft/ft_strchr.c b/libft/ft_strchr.c index a711097..b17fa23 100644 --- a/libft/ft_strchr.c +++ b/libft/ft_strchr.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_strchr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/12 13:57:59 by apommier #+# #+# */ -/* Updated: 2020/12/12 13:58:05 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:34:28 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,13 +14,13 @@ char *ft_strchr(const char *s, int c) { - unsigned char *str; + unsigned char *str; - str = (unsigned char*)s; + str = (unsigned char *)s; while ((*str != c) && (*str != 0)) str++; if (*str == c) - return ((char*)str); + return ((char *)str); else return (0); } diff --git a/libft/ft_strjoin.c b/libft/ft_strjoin.c deleted file mode 100644 index 250d499..0000000 --- a/libft/ft_strjoin.c +++ /dev/null @@ -1,42 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strjoin.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/11 15:38:13 by apommier #+# #+# */ -/* Updated: 2020/12/16 17:00:44 by apommier ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -char *ft_strjoin(char const *s1, char const *s2) -{ - char *dest; - int i; - int j; - - i = 0; - j = 0; - if (!s1 || !s2) - return (0); - if (!(dest = (char*)malloc(ft_strlen(s1) + ft_strlen(s2) + 1))) - return (0); - while (s1[i] && s1) - { - dest[j] = s1[i]; - j++; - i++; - } - i = 0; - while (s2[i] && s2) - { - dest[j] = s2[i]; - j++; - i++; - } - dest[j] = 0; - return (dest); -} diff --git a/libft/ft_strlcpy.c b/libft/ft_strlcpy.c index 8d826c8..05af28a 100644 --- a/libft/ft_strlcpy.c +++ b/libft/ft_strlcpy.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/29 00:13:07 by apommier #+# #+# */ -/* Updated: 2020/12/12 16:07:05 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:38:48 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,8 @@ size_t ft_strlcpy(char *dst, const char *src, size_t size) { - int i; - int j; + int i; + int j; j = 0; i = 0; diff --git a/libft/ft_strlen.c b/libft/ft_strlen.c index c4bad3d..a071444 100644 --- a/libft/ft_strlen.c +++ b/libft/ft_strlen.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/29 00:13:19 by apommier #+# #+# */ -/* Updated: 2020/11/29 17:12:23 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:38:56 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ size_t ft_strlen(const char *s) { - size_t i; + size_t i; i = 0; while (s[i] != 0) diff --git a/libft/ft_strmapi.c b/libft/ft_strmapi.c index 0040b83..a983137 100644 --- a/libft/ft_strmapi.c +++ b/libft/ft_strmapi.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_strmapi.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/08 19:03:09 by apommier #+# #+# */ -/* Updated: 2020/12/16 16:52:49 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:39:05 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) if (!s) return (0); i = 0; - dest = (char*)ft_calloc(ft_strlen(s) + 1, sizeof(char)); + dest = (char *)ft_calloc(ft_strlen(s) + 1, sizeof(char)); if (!dest) return (0); while (s[i]) diff --git a/libft/ft_strncmp.c b/libft/ft_strncmp.c index 91f1d0b..6de7048 100644 --- a/libft/ft_strncmp.c +++ b/libft/ft_strncmp.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/29 00:13:31 by apommier #+# #+# */ -/* Updated: 2020/12/13 20:44:39 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:39:13 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ int ft_strncmp(const char *s1, const char *s2, size_t n) { - int i; + int i; i = 0; while (n && (s1[i] || s2[i])) diff --git a/libft/ft_strnstr.c b/libft/ft_strnstr.c index af1e057..e993e0d 100644 --- a/libft/ft_strnstr.c +++ b/libft/ft_strnstr.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/29 00:13:42 by apommier #+# #+# */ -/* Updated: 2020/12/13 22:55:27 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:39:28 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,9 @@ char *ft_strnstr(const char *big, const char *little, size_t len) i = 0; if (!little[i]) - return ((char*)big); + return ((char *)big); if (!little[i]) - return ((char*)big); + return ((char *)big); while (big[i] && len - i) { j = 0; @@ -32,7 +32,7 @@ char *ft_strnstr(const char *big, const char *little, size_t len) while (little[j] == big[i + j] && little[j] && len - i - j) j++; if (little[j] == 0) - return ((char*)&big[i]); + return ((char *)&big[i]); else i++; } diff --git a/libft/ft_strrchr.c b/libft/ft_strrchr.c index 55751e8..0d11e30 100644 --- a/libft/ft_strrchr.c +++ b/libft/ft_strrchr.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/29 00:13:52 by apommier #+# #+# */ -/* Updated: 2020/12/16 16:14:41 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:39:37 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ char *ft_strrchr(const char *s, int c) char *str; char *last; - str = (char*)s; + str = (char *)s; if (c == 0) { while (*str) diff --git a/libft/ft_strtrim.c b/libft/ft_strtrim.c index a6cda30..3930252 100644 --- a/libft/ft_strtrim.c +++ b/libft/ft_strtrim.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/29 23:52:05 by apommier #+# #+# */ -/* Updated: 2020/12/16 18:06:01 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:40:17 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,7 @@ static int len_dest(const char *s1, char const *set) return (len); } -char *ft_strtrim(char const *s1, char const *set) +char *ft_strtrim(char const *s1, char const *set) { int j; int i; @@ -57,7 +57,8 @@ char *ft_strtrim(char const *s1, char const *set) if (!s1) return (0); len = len_dest(s1, set); - if (!(dest = ft_calloc(len + 1, 1))) + dest = ft_calloc(len + 1, 1); + if (!dest) return (0); while (is_set(set, s1[i])) i++; diff --git a/libft/libft.h b/libft/libft.h index 1a76de7..767d2a1 100644 --- a/libft/libft.h +++ b/libft/libft.h @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/11 03:21:27 by apommier #+# #+# */ -/* Updated: 2022/01/13 18:56:56 by apommier ### ########.fr */ +/* Updated: 2022/01/17 15:47:21 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,12 @@ # include # include -typedef struct s_list +typedef struct t_slist { void *nbr; int index; int swap; - struct s_list *next; + struct t_slist *next; } t_list; void *ft_memset(void *s, int c, size_t n); @@ -50,7 +50,6 @@ void *ft_calloc(size_t nmenb, size_t size); char *ft_strdup(const char *s); char *ft_substr(char const *s, unsigned int start, size_t len); -char *ft_strjoin(char const *s1, char const *s2); char *ft_strtrim(char const *s1, char const *set); char **ft_split(char const *s, char c); char *ft_itoa(int n); @@ -70,6 +69,6 @@ void ft_lstclear(t_list **lst, void (*del)(void *)); void ft_lstiter(t_list *lst, void (*f)(void *)); t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); -t_list *ft_lstbeforelast(t_list *lst); +t_list *ft_lstbeforelast(t_list *lst); #endif diff --git a/main.c b/main.c index e5e2774..d811114 100644 --- a/main.c +++ b/main.c @@ -6,19 +6,19 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/25 23:15:43 by apommier #+# #+# */ -/* Updated: 2022/01/15 20:51:55 by apommier ### ########.fr */ +/* Updated: 2022/01/17 11:46:44 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" -int main(int argc, char *argv[]) +int main(int argc, char *argv[]) { - if (argc == 1) - { - ft_putstr_fd("Error\n", 2); - return (1); - } - push_swap(argc, argv); - return (1); -} \ No newline at end of file + if (argc == 1) + { + ft_putstr_fd("Error\n", 2); + return (1); + } + push_swap(argc, argv); + return (1); +} diff --git a/optimise_move.c b/optimise_move.c new file mode 100644 index 0000000..c68ce2e --- /dev/null +++ b/optimise_move.c @@ -0,0 +1,124 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* optimise_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/15 23:50:43 by apommier #+# #+# */ +/* Updated: 2022/01/17 14:09:26 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include"push_swap.h" + +int ft_strcmp(const char *s1, t_slist *str) +{ + int i; + char *s2; + + if (!str) + return (1); + s2 = (char *)str->action; + i = 0; + if (s2) + { + while (s1[i] || s2[i]) + { + if (s1[i] != s2[i]) + { + return ((unsigned char)s1[i] - (unsigned char)s2[i]); + } + i++; + } + return (0); + } + return (1); +} + +void cut_lst(t_slist *lst) +{ + t_slist *save; + + save = 0; + if (!lst) + return ; + if (lst->next) + { + save = lst->next->next; + lst->next->next = 0; + if (lst->next) + free(lst->next); + lst->next = save; + } +} + +t_slist *transform_list(t_slist *start, t_slist *start2, int r1, int r2) +{ + t_slist *type; + + type = start; + if (r2 < r1) + { + while (r2--) + { + cut_lst(start2); + start->action = "rr"; + start = start->next; + } + } + else + { + while (r1--) + { + cut_lst(start2); + start->action = "rr"; + start = start->next; + } + } + return (type); +} + +t_slist *sequence(t_slist *lst, t_slist *start1, t_slist *start2, int r1) +{ + char *type; + int r2; + + type = 0; + r2 = 0; + if (!ft_strcmp("ra", lst) || !ft_strcmp("rb", lst)) + { + type = lst->action; + while (!ft_strcmp(type, lst) && r2++ >= 0) + lst = lst->next; + lst = transform_list(start1, start2, r1, r2); + } + return (lst); +} + +void optimise_move(t_slist **action, t_slist *start2, int r1) +{ + t_slist *start1; + t_slist *lst; + char *type; + + type = 0; + lst = *action; + while (lst) + { + r1 = 0; + if (!ft_strcmp("ra", lst) || !ft_strcmp("rb", lst)) + { + start1 = lst; + type = lst->action; + while (!ft_strcmp(type, lst) && r1++ >= 0) + { + start2 = lst; + lst = lst->next; + } + lst = sequence(lst, start1, start2, r1); + } + else if (lst) + lst = lst->next; + } +} diff --git a/optimise_move2.c b/optimise_move2.c new file mode 100644 index 0000000..3bdbd74 --- /dev/null +++ b/optimise_move2.c @@ -0,0 +1,83 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* optimise_move2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/17 07:44:45 by apommier #+# #+# */ +/* Updated: 2022/01/17 14:16:41 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +t_slist *transform_list2(t_slist *start, t_slist *start2, int r1, int r2) +{ + t_slist *type; + + type = start; + if (r2 < r1) + { + while (r2--) + { + cut_lst(start2); + start->action = "rrr"; + start = start->next; + } + } + else + { + while (r1--) + { + cut_lst(start2); + start->action = "rrr"; + start = start->next; + } + } + return (type); +} + +t_slist *sequence2(t_slist *lst, t_slist *start1, t_slist *start2, int r1) +{ + char *type; + int r2; + + type = 0; + r2 = 0; + if (!ft_strcmp("rra", lst) || !ft_strcmp("rrb", lst)) + { + type = lst->action; + while (!ft_strcmp(type, lst) && r2++ >= 0) + lst = lst->next; + lst = transform_list2(start1, start2, r1, r2); + } + return (lst); +} + +void optimise_move2(t_slist **action, t_slist *start2, int r1) +{ + t_slist *start1; + t_slist *lst; + char *type; + + type = 0; + lst = *action; + while (lst) + { + r1 = 0; + if (!ft_strcmp("rra", lst) || !ft_strcmp("rrb", lst)) + { + start1 = lst; + type = lst->action; + while (!ft_strcmp(type, lst) && r1++ >= 0) + { + start2 = lst; + lst = lst->next; + } + lst = sequence2(lst, start1, start2, r1); + } + else if (lst) + lst = lst->next; + } +} diff --git a/process.c b/process.c index 88b64a9..e7b5c83 100644 --- a/process.c +++ b/process.c @@ -1,78 +1,78 @@ /* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* process.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2021/11/25 23:15:09 by apommier #+# #+# */ -/* Updated: 2021/11/25 23:15:09 by apommier ### ########.fr */ -/* */ +/* */ +/* ::: :::::::: */ +/* process.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/17 11:52:37 by apommier #+# #+# */ +/* Updated: 2022/01/17 11:52:37 by apommier ### ########.fr */ +/* */ /* ************************************************************************** */ #include "push_swap.h" -t_list *ft_sa_sb(t_list **list, s_list **process, char type, int set) +void ft_sa_sb(t_list **list, t_slist **process, char type, int set) { - t_list *swap; + t_list *swap; - swap = (*list)->next->next; - (*list)->next->next = *list; - *list = (*list)->next; - (*list)->next->next = swap; - if (set) - { - if (type == 'a') - s_lstadd_back(process, new_slist("sa\n")); - else if (type == 'b') - s_lstadd_back(process, new_slist("sb\n")); - } + swap = (*list)->next->next; + (*list)->next->next = *list; + *list = (*list)->next; + (*list)->next->next = swap; + if (set) + { + if (type == 'a') + s_lstadd_back(process, new_slist("sa")); + else if (type == 'b') + s_lstadd_back(process, new_slist("sb")); + } } -t_list *ft_ra_rb(t_list **list, s_list **process, char type) +void ft_ra_rb(t_list **list, t_slist **process, char type) { - t_list *swap; + t_list *swap; - swap = *list; + swap = *list; (ft_lstlast(*list))->next = *list; *list = (*list)->next; - swap->next = 0; - if (type == 'a') - s_lstadd_back(process, new_slist("ra\n")); - else if (type == 'b') - s_lstadd_back(process, new_slist("rb\n")); + swap->next = 0; + if (type == 'a') + s_lstadd_back(process, new_slist("ra")); + else if (type == 'b') + s_lstadd_back(process, new_slist("rb")); } -t_list *ft_pa(t_list **list_a, t_list **list_b, s_list **process) +t_list *ft_pa(t_list **list_a, t_list **list_b, t_slist **process) { - t_list *swap; + t_list *swap; - swap = *list_b; - *list_b = (*list_b)->next; + swap = *list_b; + *list_b = (*list_b)->next; ft_lstadd_front(list_a, swap); - s_lstadd_back(process, new_slist("pa\n")); - return (*list_b); + s_lstadd_back(process, new_slist("pa")); + return (*list_b); } -t_list *ft_pb(t_list **list_a, t_list **list_b, s_list **process) +void ft_pb(t_list **list_a, t_list **list_b, t_slist **process) { - t_list *swap; + t_list *swap; - swap = *list_a; - *list_a = (*list_a)->next; + swap = *list_a; + *list_a = (*list_a)->next; ft_lstadd_front(list_b, swap); - s_lstadd_back(process, new_slist("pb\n")); + s_lstadd_back(process, new_slist("pb")); } -t_list * ft_rra_rrb(t_list **list, s_list **process, char type) +void ft_rra_rrb(t_list **list, t_slist **process, char type) { - t_list *swap; + t_list *swap; - swap = ft_lstlast(*list); - ft_lstbeforelast(*list)->next = 0; - ft_lstadd_front(list, swap); - if (type == 'a') - s_lstadd_back(process, new_slist("rra\n")); - else if (type == 'b') - s_lstadd_back(process, new_slist("rrb\n")); + swap = ft_lstlast(*list); + ft_lstbeforelast(*list)->next = 0; + ft_lstadd_front(list, swap); + if (type == 'a') + s_lstadd_back(process, new_slist("rra")); + else if (type == 'b') + s_lstadd_back(process, new_slist("rrb")); } diff --git a/push_swap.c b/push_swap.c index cf4b1a0..24550a1 100644 --- a/push_swap.c +++ b/push_swap.c @@ -6,27 +6,12 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/25 23:10:37 by apommier #+# #+# */ -/* Updated: 2022/01/15 23:28:47 by apommier ### ########.fr */ +/* Updated: 2022/01/17 15:44:11 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" -void printf_list(t_list *start) -{ - int i = 0; - - printf("----printf_list----\n"); - while(start) - { - i++; - printf("lst%d --- nbr: %d index: %d\n", i,*(int*)start->nbr, start->index); - start = start->next; - } - printf("----end_print----\n"); - return; -} - int is_nbr(int nbrarg, char **list) { int i; @@ -57,8 +42,8 @@ int is_nbr(int nbrarg, char **list) int is_double(int nbrarg, char **list) { - int i; - int j; + int i; + int j; int len; i = 1; @@ -67,7 +52,7 @@ int is_double(int nbrarg, char **list) while (nbrarg - j) { i = 1; - while(list[j + i]) + while (list[j + i]) { if (ft_strlen(list[j]) > ft_strlen(list[j + i])) len = ft_strlen(list[j]); @@ -82,9 +67,9 @@ int is_double(int nbrarg, char **list) return (1); } -int push_swap(int nbrarg, char **list) +int push_swap(int nbrarg, char **list) { - t_list *start; + t_list *start; start = 0; if (!is_nbr(nbrarg, list) || !is_double(nbrarg, list)) @@ -94,42 +79,18 @@ int push_swap(int nbrarg, char **list) } start = set_list(--nbrarg, list); if (ft_lstsize(start) == 1) - return(1); + return (1); lst_indexing(start); - find_loop_index(start); + start = find_loop_index(start); ft_lstclear(&start, &free); return (1); } -/*t_list *set_list(int nbrarg, char **srcs) -{ - int *tmp; - t_list *start; - t_list *swap; - - start = 0; - swap = 0; - *(srcs)++; - tmp = ft_calloc(sizeof(int), 1); - *tmp = ft_atoi*(srcs); - start = ft_lstnew(tmp); - swap = start; - while (--nbrarg) - { - *(srcs)++; - tmp = ft_calloc(sizeof(int), 1); - *tmp = ft_atoi*(srcs); - swap->next = ft_lstnew(tmp); - swap = swap->next; - } - return (start); -}*/ - t_list *set_list(int nbrarg, char **srcs) { - int *tmp; - int i; - int j; + int *tmp; + int i; + int j; t_list *swap; j = 1; @@ -152,4 +113,23 @@ t_list *set_list(int nbrarg, char **srcs) j++; } return (swap); -} \ No newline at end of file +} + +t_list *best_b(t_list **b, t_list *start_b, t_list *a, int best_move) +{ + int move; + t_list *best_b; + + move = 0; + while (*b) + { + move = find_nbr(ft_lstsize(start_b), a, start_b, (*b)->index); + if (move < best_move) + { + best_move = move; + best_b = *b; + } + *b = (*b)->next; + } + return (best_b); +} diff --git a/push_swap.h b/push_swap.h index d537a87..39db1b0 100644 --- a/push_swap.h +++ b/push_swap.h @@ -6,56 +6,67 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/25 23:15:17 by apommier #+# #+# */ -/* Updated: 2022/01/14 23:03:47 by apommier ### ########.fr */ +/* Updated: 2022/01/17 15:46:29 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef FT_PRINTF_H -# define FT_PRINTF_H +#ifndef PUSH_SWAP_H +# define PUSH_SWAP_H # include # include # include "./libft/libft.h" -typedef struct process_list +typedef struct procest_slist { - void *action; - struct process_list *next; -} s_list; + void *action; + struct procest_slist *next; +} t_slist; void sort3(t_list *list); -int push_swap(int nbrarg, char **list); -int is_nbr(int nbrarg, char **list); -t_list *set_list(int nbrarg, char **list); -void lst_indexing(t_list *list); -int is_double(int nbrarg, char **list); -int is_sorted(t_list *list); -void find_loop_index(t_list *list); -int find_loop(t_list *start, t_list *list, int setswap); +int push_swap(int nbrarg, char **list); +int is_nbr(int nbrarg, char **list); +t_list *set_list(int nbrarg, char **list); +void lst_indexing(t_list *list); +int is_double(int nbrarg, char **list); +int is_sorted(t_list *list); +t_list *find_loop_index(t_list *list); +int find_loop(t_list *start, t_list *list, int setswap); t_list *ft_lstnew(void *content); +/*t_slist fonction*/ +t_slist *new_slist(void *content); +void s_lstadd_back(t_slist **alst, t_slist *new); +t_slist *s_lstlast(t_slist *lst); +void s_lstclear(t_slist **lst); +void printf_slist(t_slist *start); -s_list *new_slist(void *content); -void s_lstadd_back(s_list **alst, s_list *new); -s_list *s_lstlast(s_list *lst); -void s_lstclear(s_list **lst); -void printf_slist(s_list *start); +/*process fonction*/ +void ft_sa_sb(t_list **list, t_slist **process, char type, int set); +void ft_ra_rb(t_list **list, t_slist **process, char type); +t_list *ft_pa(t_list **list_a, t_list **list_b, t_slist **process); +void ft_pb(t_list **list_a, t_list **list_b, t_slist **process); +void ft_rra_rrb(t_list **list, t_slist **process, char type); -t_list *ft_sa_sb(t_list **list, s_list **process, char type, int set); -t_list *ft_ra_rb(t_list **list, s_list **process, char type); -t_list *ft_pa(t_list **list_a, t_list **list_b, s_list **process); -t_list *ft_pb(t_list **list_a, t_list **list_b, s_list **process); -t_list *ft_rra_rrb(t_list **list, s_list **process, char type); - -t_list *swap(t_list *a, t_list **b, s_list **action, t_list *to_swap); -t_list *make_list(int place, t_list *list, s_list *action, char type); -t_list *swap_to_a(t_list *a, t_list *b, t_list *start_b, s_list *action); -int find_place_for_b(t_list *list, int index, int size_b); -int find_nbr(int size_b, t_list *a, t_list *b, int index_b); -void swap_to_b(t_list *list, t_list *best, t_list *b, int loop); -t_list *turn_list(t_list *a, s_list *action); - -void printf_list(t_list *start); +/*sort fonction*/ +t_list *best_b(t_list **b, t_list *start_b, t_list *a, int best_move); +t_list *swap(t_list *a, t_list **b, t_slist **action, t_list *to_swap); +t_list *make_list(int place, t_list *list, t_slist *action, char type); +t_list *swap_to_a(t_list *a, t_list *b, t_list *start_b, t_slist *action); +t_list *is_swap(t_list *list, t_slist **action, int *loop, t_list *best); +int find_place_for_b(t_list *list, int index, int size_b); +int find_nbr(int size_b, t_list *a, t_list *b, int index_b); +t_list *swap_to_b(t_list *list, t_list *best, t_list *b, int loop); +t_list *turn_list(t_list *a, t_slist *action); +/*optimise list of action*/ +t_slist *transform_list(t_slist *start, t_slist *start2, int r1, int r2); +t_slist *transform_list2(t_slist *start, t_slist *start2, int r1, int r2); +t_slist *sequence(t_slist *lst, t_slist *start1, t_slist *start2, int r1); +t_slist *sequence2(t_slist *lst, t_slist *start1, t_slist *start2, int r1); +void optimise_move(t_slist **action, t_slist *next_start, int r1); +void optimise_move2(t_slist **action, t_slist *start2, int r1); +void cut_lst(t_slist *lst); +int ft_strcmp(const char *s1, t_slist *str); #endif \ No newline at end of file diff --git a/set_a.c b/set_a.c index 3a123a6..6d822fd 100644 --- a/set_a.c +++ b/set_a.c @@ -6,13 +6,13 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/07 19:14:03 by apommier #+# #+# */ -/* Updated: 2022/01/15 23:30:15 by apommier ### ########.fr */ +/* Updated: 2022/01/17 15:07:09 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" -void find_loop_index(t_list *list) +t_list *find_loop_index(t_list *list) { t_list *start; t_list *save; @@ -33,14 +33,14 @@ void find_loop_index(t_list *list) list = list->next; } find_loop(start, save, 1); - swap_to_b(start, save, 0, loop_size2); - + list = swap_to_b(start, save, 0, loop_size2); + return (list); } int find_loop(t_list *start, t_list *list, int setswap) { - int countdown; - int loop_size; + int countdown; + int loop_size; t_list *swap; loop_size = 0; @@ -49,7 +49,7 @@ int find_loop(t_list *start, t_list *list, int setswap) while (countdown) { if (swap) - { + { if (swap->index > list->index) { list = swap; @@ -66,43 +66,36 @@ int find_loop(t_list *start, t_list *list, int setswap) return (loop_size); } -void swap_to_b(t_list *list, t_list *best, t_list *b, int loop) +t_list *swap_to_b(t_list *list, t_list *best, t_list *b, int loop) { - int swap; - int lst_size; - s_list *action; + int lst_size; + t_slist *action; action = 0; lst_size = ft_lstsize(list); - swap = 0; while (lst_size) { - ft_sa_sb(&list, &action, 'a', 0); - swap = find_loop(list, best, 0); - if (swap > loop) - { - loop = swap; - s_lstadd_back(&action, new_slist("sa\n")); - } - else - ft_sa_sb(&list, &action, 'a', 0); + list = is_swap(list, &action, &loop, best); if (list->swap) ft_pb(&list, &b, &action); else ft_ra_rb(&list, &action, 'a'); - lst_size--; + lst_size--; } list = swap_to_a(list, b, b, action); list = turn_list(list, action); + optimise_move(&action, 0, 0); + optimise_move2(&action, 0, 0); printf_slist(action); s_lstclear(&action); + return (list); } -t_list *turn_list(t_list *a, s_list *action) +t_list *turn_list(t_list *a, t_slist *action) { t_list *start; int place; - + start = a; place = 0; while (a->index > 1) @@ -111,19 +104,33 @@ t_list *turn_list(t_list *a, s_list *action) place++; } a = start; - //printf("place = %d size = %d\n", place, ft_lstsize(a)); if (place > ft_lstsize(a) / 2 && place > 2) { - //printf("if\n"); while (ft_lstsize(start) - (place++)) ft_rra_rrb(&start, &action, 'a'); } else { - //printf("else\n"); while (place--) ft_ra_rb(&start, &action, 'a'); } - //printf_list(start); return (start); -} \ No newline at end of file +} + +t_list *is_swap(t_list *list, t_slist **action, int *loop, t_list *best) +{ + int swap; + + swap = 0; + ft_sa_sb(&list, action, 'a', 0); + swap = find_loop(list, best, 0); + if (swap > *loop + 1) + { + *loop = swap; + s_lstadd_back(action, new_slist("sa")); + find_loop(list, best, 1); + } + else + ft_sa_sb(&list, action, 'a', 0); + return (list); +} diff --git a/sort_liltte.c b/sort_liltte.c new file mode 100644 index 0000000..e69de29 diff --git a/sorting.c b/sorting.c index 0566601..a174ac3 100644 --- a/sorting.c +++ b/sorting.c @@ -12,27 +12,27 @@ #include "push_swap.h" -t_list *swap(t_list *a, t_list **b, s_list **action, t_list *to_swap) +t_list *swap(t_list *a, t_list **b, t_slist **action, t_list *to_swap) { - int rotate; - int place; - t_list *start_b; + int place_for_b; + int place; + t_list *start_b; start_b = *b; place = 0; - rotate = 0; while ((*b)->index != to_swap->index) { place++; *b = (*b)->next; } start_b = make_list(place, start_b, *action, 'b'); - a = make_list(find_place_for_b(a, to_swap->index, ft_lstsize(start_b)), a, *action, 'a'); + place_for_b = find_place_for_b(a, to_swap->index, ft_lstsize(start_b)); + a = make_list(place_for_b, a, *action, 'a'); *b = ft_pa(&a, &start_b, action); return (a); } -t_list *make_list(int place, t_list *list, s_list *action, char type) +t_list *make_list(int place, t_list *list, t_slist *action, char type) { int lstsize; @@ -49,7 +49,6 @@ t_list *make_list(int place, t_list *list, s_list *action, char type) { while (place) { - ft_ra_rb(&list, &action, type); place--; } @@ -57,16 +56,15 @@ t_list *make_list(int place, t_list *list, s_list *action, char type) return (list); } -t_list *swap_to_a(t_list *a, t_list *b, t_list *start_b, s_list *action) +t_list *swap_to_a(t_list *a, t_list *b, t_list *start_b, t_slist *action) { int lst_size; int size_b; int move; int best_move; - t_list *best_b; - int i = 0; + t_list *to_swap; - best_b = 0; + to_swap = 0; size_b = ft_lstsize(b); move = size_b + ft_lstsize(a); best_move = move; @@ -75,17 +73,8 @@ t_list *swap_to_a(t_list *a, t_list *b, t_list *start_b, s_list *action) { best_move = ft_lstsize(a) + ft_lstsize(b); b = start_b; - while (b) - { - move = find_nbr(ft_lstsize(start_b), a, start_b, b->index); - if (move < best_move) - { - best_move = move; - best_b = b; - } - b = b->next; - } - a = swap(a, &start_b, &action, best_b); + to_swap = best_b(&b, start_b, a, best_move); + a = swap(a, &start_b, &action, to_swap); lst_size--; } return (a); @@ -137,36 +126,3 @@ int find_nbr(int size_b, t_list *a, t_list *b, int index_b) move += place; return (move); } - -/*void choose_swap(t_list *a, t_list *b, s_list *action, int lst_size) -{ - int size_a; - int size_b; - t_list *save_b; - int move; - int save_move; - - size_b = lst_size; - size_a = ft_lstsize(a); - printf("swapto a\n"); - move = 0; - save_move = size_b + size_a; - while (lst_size) - { - move = find_place_for_b(a, b->index); - if (move > size_a / 2) - move = size_a - move; - if (lst_size < size_b / 2) - move += size_b - lst_size; - else - move += lst_size; - if (move < save_move) - { - save_move = move; - save_b = b; - } - b = b->next; - lst_size--; - } - find_nbr(); -}*/ \ No newline at end of file diff --git a/s_list.c b/t_slist.c similarity index 70% rename from s_list.c rename to t_slist.c index 6689042..4b5966a 100644 --- a/s_list.c +++ b/t_slist.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* s_list.c :+: :+: :+: */ +/* t_slist.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -12,11 +12,11 @@ #include "push_swap.h" -s_list *new_slist(void *content) +t_slist *new_slist(void *content) { - s_list *new; + t_slist *new; - new = (s_list*)malloc(sizeof(s_list)); + new = (t_slist *)malloc(sizeof(t_slist)); if (!new) return (0); new->action = content; @@ -24,9 +24,9 @@ s_list *new_slist(void *content) return (new); } -void s_lstclear(s_list **lst) +void s_lstclear(t_slist **lst) { - s_list *chr; + t_slist *chr; chr = *lst; while (*lst) @@ -38,7 +38,7 @@ void s_lstclear(s_list **lst) lst = 0; } -void s_lstadd_back(s_list **alst, s_list *new) +void s_lstadd_back(t_slist **alst, t_slist *new) { if (*alst == 0) *alst = new; @@ -47,26 +47,24 @@ void s_lstadd_back(s_list **alst, s_list *new) new->next = 0; } -s_list *s_lstlast(s_list *lst) +t_slist *s_lstlast(t_slist *lst) { if (!lst) return (0); while (lst->next) - { - //printf("test lst = %s\n", (char*)lst->action); lst = lst->next; - } return (lst); } -void printf_slist(s_list *start) +void printf_slist(t_slist *start) { - int i = 0; - while(start) + int i; + + i = 0; + while (start) { i++; - ft_putstr_fd((char*)start->action, 1); + ft_putendl_fd((char *)start->action, 1); start = start->next; - } - return; + } }