This commit is contained in:
kinou-p 2022-01-17 16:20:46 +01:00
parent 597e1834e3
commit 772733d41e
38 changed files with 500 additions and 379 deletions

View File

@ -6,7 +6,7 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ # # By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2021/11/13 13:06:47 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\ process.c\
main.c\ main.c\
sorting.c\ sorting.c\
s_list.c\ t_slist.c\
set_a.c\ set_a.c\
optimise_move.c\
optimise_move2.c\
indexing.c indexing.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
CFLAGS = CFLAGS = -Wall -Wextra -Werror
RM = rm -rf RM = rm -rf
LIBFT = ./libft LIBFT = ./libft

View File

@ -12,21 +12,21 @@
#include "push_swap.h" #include "push_swap.h"
void lst_indexing(t_list *list) void lst_indexing(t_list *list)
{ {
int index; int index;
t_list *swap; t_list *swap;
t_list *save; t_list *save;
swap = list; swap = list;
save = list; save = list;
index = 0; index = 0;
while(save) while (save)
{ {
index = 1; index = 1;
while (swap) while (swap)
{ {
if (*(int*)save->nbr > *(int*)swap->nbr) if (*(int *)save->nbr > *(int *)swap->nbr)
index++; index++;
swap = swap->next; swap = swap->next;
} }
@ -34,4 +34,4 @@ void lst_indexing(t_list *list)
swap = list; swap = list;
save = save->next; save = save->next;
} }
} }

View File

@ -24,7 +24,6 @@ SRCS = ft_memset.c \
ft_calloc.c \ ft_calloc.c \
ft_strdup.c \ ft_strdup.c \
ft_substr.c \ ft_substr.c \
ft_strjoin.c \
ft_strtrim.c \ ft_strtrim.c \
ft_split.c \ ft_split.c \
ft_itoa.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 ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c ft_lstbeforelast.c
BONUS_O = ${BONUS_C:.c=.o} BONUS_O = ${BONUS_C:.c=.o}
CC = gcc CC = gcc
CFLAGS = -Wall -Wextra -Werror CFLAGS = -Wall -Wextra
RM = rm -rf RM = rm -rf
.c.o: .c.o:

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:09:17 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 ft_atoi(const char *nptr)
{ {
int i; int i;
int nbr; int nbr;
int minus; int minus;
minus = 1; minus = 1;
nbr = 0; nbr = 0;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:09:48 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; char *p;
int i; int i;
p = (char*)s; p = (char *)s;
i = 0; i = 0;
while (n != 0) while (n != 0)
{ {

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:09:57 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; i = 0;
new = malloc(size * nmemb); new = malloc(size * nmemb);
if (!new)
return (0);
if (new) if (new)
{ {
while (size * nmemb - i) while (size * nmemb - i)
@ -27,5 +29,5 @@ void *ft_calloc(size_t nmemb, size_t size)
i++; i++;
} }
} }
return ((void*)new); return ((void *)new);
} }

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */ /* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/08 18:20:19 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; char *dest;
j += minus; j += minus;
dest = (char*)malloc(sizeof(char) * (j + 1)); dest = (char *)malloc(sizeof(char) * (j + 1));
if (dest == 0) if (dest == 0)
return (0); return (0);
dest[j] = 0; dest[j] = 0;
@ -34,7 +34,7 @@ static char *fill(long n, int j, int minus)
return (dest); return (dest);
} }
char *ft_itoa(int n) char *ft_itoa(int n)
{ {
long i; long i;
long k; long k;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/09 19:16:55 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 *ft_lstbeforelast(t_list *lst)
{ {
t_list *save; t_list *save;
save = 0; save = 0;
if (!lst) if (!lst)
@ -25,4 +25,4 @@ t_list *ft_lstbeforelast(t_list *lst)
lst = lst->next; lst = lst->next;
} }
return (save); return (save);
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/09 19:58:04 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*)) void ft_lstclear(t_list **lst, void (*del)(void*))
{ {
t_list *chr; t_list *chr;
chr = *lst; chr = *lst;
while (*lst) while (*lst)

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/09 01:06:20 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 *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) if (!new)
return (0); return (0);
new->swap = 0; new->swap = 0;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:11:04 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; size_t i;
i = 0; i = 0;
p = (char*)dest; p = (char *)dest;
p1 = (const char*)src; p1 = (const char *)src;
while (i < n) while (i < n)
{ {
p[i] = p1[i]; p[i] = p1[i];

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:11:39 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) 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) while (n)
{ {
if ((unsigned char)c == *str) if ((unsigned char)c == *str)

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:11:51 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; i = 0;
while (n) while (n)
{ {
if (((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]); return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]);
n--; n--;
i++; i++;
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:12:03 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) if (!dest && !src)
return (dest); return (dest);
p = (char*)dest; p = (char *)dest;
p1 = (const char*)src; p1 = (const char *)src;
i = 0; i = 0;
while (i < n) while (i < n)
{ {

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:12:14 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) void *ft_memmove(void *dest, const void *src, size_t n)
{ {
size_t i; size_t i;
i = 0; i = 0;
if (!dest && !src) if (!dest && !src)
@ -23,7 +23,7 @@ void *ft_memmove(void *dest, const void *src, size_t n)
{ {
while (n) while (n)
{ {
((unsigned char*)dest)[n - 1] = ((unsigned char*)src)[n - 1]; ((unsigned char *)dest)[n - 1] = ((unsigned char *)src)[n - 1];
n--; n--;
} }
} }
@ -31,7 +31,7 @@ void *ft_memmove(void *dest, const void *src, size_t n)
{ {
while (i < n) while (i < n)
{ {
((unsigned char*)dest)[i] = ((unsigned char*)src)[i]; ((unsigned char *)dest)[i] = ((unsigned char *)src)[i];
i++; i++;
} }
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:12:24 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; unsigned char *p;
i = 0; i = 0;
p = (unsigned char*)s; p = (unsigned char *)s;
while (n > 0) while (n > 0)
{ {
p[i] = (unsigned char)c; p[i] = (unsigned char)c;

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */ /* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/08 23:25:57 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) void ft_putnbr_fd(int n, int fd)
{ {
long nbr; long nbr;
nbr = n; nbr = n;
if (nbr < 0) if (nbr < 0)

View File

@ -3,23 +3,23 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_split.c :+: :+: :+: */ /* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/07 00:54:12 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" #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; int i;
i = 0; i = 0;
while (s[i] != c && s[i]) while (s[i] != c && s[i])
i++; i++;
dest[index] = (char*)ft_calloc(i + 1, sizeof(char)); dest[index] = (char *)ft_calloc(i + 1, sizeof(char));
if (dest[index] == 0) if (dest[index] == 0)
return (0); return (0);
i = 0; i = 0;
@ -31,7 +31,7 @@ static int fill_tab(char *s, char c, char **dest, size_t index)
return (1); 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 index;
int k; 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 i;
int j; int j;
@ -71,9 +71,10 @@ char **ft_split(char const *s, char c)
while (s[i] == c && s[i]) while (s[i] == c && s[i])
i++; i++;
} }
if (!(dest = (char**)malloc(sizeof(char*) * (i + j)))) dest = (char **)malloc(sizeof(char *) * (i + j));
if (!dest)
return (0); return (0);
dest[j] = 0; dest[j] = 0;
call((char*)s, c, dest, j); call((char *)s, c, dest, j);
return (dest); return (dest);
} }

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */ /* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/12 13:57:59 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) 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)) while ((*str != c) && (*str != 0))
str++; str++;
if (*str == c) if (*str == c)
return ((char*)str); return ((char *)str);
else else
return (0); return (0);
} }

View File

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

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:13:07 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) size_t ft_strlcpy(char *dst, const char *src, size_t size)
{ {
int i; int i;
int j; int j;
j = 0; j = 0;
i = 0; i = 0;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:13:19 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 ft_strlen(const char *s)
{ {
size_t i; size_t i;
i = 0; i = 0;
while (s[i] != 0) while (s[i] != 0)

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */ /* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/08 19:03:09 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) if (!s)
return (0); return (0);
i = 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) if (!dest)
return (0); return (0);
while (s[i]) while (s[i])

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:13:31 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 ft_strncmp(const char *s1, const char *s2, size_t n)
{ {
int i; int i;
i = 0; i = 0;
while (n && (s1[i] || s2[i])) while (n && (s1[i] || s2[i]))

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:13:42 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; i = 0;
if (!little[i]) if (!little[i])
return ((char*)big); return ((char *)big);
if (!little[i]) if (!little[i])
return ((char*)big); return ((char *)big);
while (big[i] && len - i) while (big[i] && len - i)
{ {
j = 0; 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) while (little[j] == big[i + j] && little[j] && len - i - j)
j++; j++;
if (little[j] == 0) if (little[j] == 0)
return ((char*)&big[i]); return ((char *)&big[i]);
else else
i++; i++;
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:13:52 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 *str;
char *last; char *last;
str = (char*)s; str = (char *)s;
if (c == 0) if (c == 0)
{ {
while (*str) while (*str)

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 23:52:05 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); return (len);
} }
char *ft_strtrim(char const *s1, char const *set) char *ft_strtrim(char const *s1, char const *set)
{ {
int j; int j;
int i; int i;
@ -57,7 +57,8 @@ char *ft_strtrim(char const *s1, char const *set)
if (!s1) if (!s1)
return (0); return (0);
len = len_dest(s1, set); len = len_dest(s1, set);
if (!(dest = ft_calloc(len + 1, 1))) dest = ft_calloc(len + 1, 1);
if (!dest)
return (0); return (0);
while (is_set(set, s1[i])) while (is_set(set, s1[i]))
i++; i++;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/11 03:21:27 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 <unistd.h> # include <unistd.h>
# include <stdlib.h> # include <stdlib.h>
typedef struct s_list typedef struct t_slist
{ {
void *nbr; void *nbr;
int index; int index;
int swap; int swap;
struct s_list *next; struct t_slist *next;
} t_list; } t_list;
void *ft_memset(void *s, int c, size_t n); 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_strdup(const char *s);
char *ft_substr(char const *s, unsigned int start, size_t len); 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_strtrim(char const *s1, char const *set);
char **ft_split(char const *s, char c); char **ft_split(char const *s, char c);
char *ft_itoa(int n); 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 *)); void ft_lstiter(t_list *lst, void (*f)(void *));
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), t_list *ft_lstmap(t_list *lst, void *(*f)(void *),
void (*del)(void *)); void (*del)(void *));
t_list *ft_lstbeforelast(t_list *lst); t_list *ft_lstbeforelast(t_list *lst);
#endif #endif

20
main.c
View File

@ -6,19 +6,19 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/25 23:15:43 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" #include "push_swap.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc == 1) if (argc == 1)
{ {
ft_putstr_fd("Error\n", 2); ft_putstr_fd("Error\n", 2);
return (1); return (1);
} }
push_swap(argc, argv); push_swap(argc, argv);
return (1); return (1);
} }

124
optimise_move.c Normal file
View File

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

83
optimise_move2.c Normal file
View File

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

100
process.c
View File

@ -1,78 +1,78 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* process.c :+: :+: :+: */ /* process.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/25 23:15:09 by apommier #+# #+# */ /* Created: 2022/01/17 11:52:37 by apommier #+# #+# */
/* Updated: 2021/11/25 23:15:09 by apommier ### ########.fr */ /* Updated: 2022/01/17 11:52:37 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #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; swap = (*list)->next->next;
(*list)->next->next = *list; (*list)->next->next = *list;
*list = (*list)->next; *list = (*list)->next;
(*list)->next->next = swap; (*list)->next->next = swap;
if (set) if (set)
{ {
if (type == 'a') if (type == 'a')
s_lstadd_back(process, new_slist("sa\n")); s_lstadd_back(process, new_slist("sa"));
else if (type == 'b') else if (type == 'b')
s_lstadd_back(process, new_slist("sb\n")); 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; (ft_lstlast(*list))->next = *list;
*list = (*list)->next; *list = (*list)->next;
swap->next = 0; swap->next = 0;
if (type == 'a') if (type == 'a')
s_lstadd_back(process, new_slist("ra\n")); s_lstadd_back(process, new_slist("ra"));
else if (type == 'b') else if (type == 'b')
s_lstadd_back(process, new_slist("rb\n")); 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; swap = *list_b;
*list_b = (*list_b)->next; *list_b = (*list_b)->next;
ft_lstadd_front(list_a, swap); ft_lstadd_front(list_a, swap);
s_lstadd_back(process, new_slist("pa\n")); s_lstadd_back(process, new_slist("pa"));
return (*list_b); 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; swap = *list_a;
*list_a = (*list_a)->next; *list_a = (*list_a)->next;
ft_lstadd_front(list_b, swap); 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); swap = ft_lstlast(*list);
ft_lstbeforelast(*list)->next = 0; ft_lstbeforelast(*list)->next = 0;
ft_lstadd_front(list, swap); ft_lstadd_front(list, swap);
if (type == 'a') if (type == 'a')
s_lstadd_back(process, new_slist("rra\n")); s_lstadd_back(process, new_slist("rra"));
else if (type == 'b') else if (type == 'b')
s_lstadd_back(process, new_slist("rrb\n")); s_lstadd_back(process, new_slist("rrb"));
} }

View File

@ -6,27 +6,12 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/25 23:10:37 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" #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 is_nbr(int nbrarg, char **list)
{ {
int i; int i;
@ -57,8 +42,8 @@ int is_nbr(int nbrarg, char **list)
int is_double(int nbrarg, char **list) int is_double(int nbrarg, char **list)
{ {
int i; int i;
int j; int j;
int len; int len;
i = 1; i = 1;
@ -67,7 +52,7 @@ int is_double(int nbrarg, char **list)
while (nbrarg - j) while (nbrarg - j)
{ {
i = 1; i = 1;
while(list[j + i]) while (list[j + i])
{ {
if (ft_strlen(list[j]) > ft_strlen(list[j + i])) if (ft_strlen(list[j]) > ft_strlen(list[j + i]))
len = ft_strlen(list[j]); len = ft_strlen(list[j]);
@ -82,9 +67,9 @@ int is_double(int nbrarg, char **list)
return (1); return (1);
} }
int push_swap(int nbrarg, char **list) int push_swap(int nbrarg, char **list)
{ {
t_list *start; t_list *start;
start = 0; start = 0;
if (!is_nbr(nbrarg, list) || !is_double(nbrarg, list)) 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); start = set_list(--nbrarg, list);
if (ft_lstsize(start) == 1) if (ft_lstsize(start) == 1)
return(1); return (1);
lst_indexing(start); lst_indexing(start);
find_loop_index(start); start = find_loop_index(start);
ft_lstclear(&start, &free); ft_lstclear(&start, &free);
return (1); 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) t_list *set_list(int nbrarg, char **srcs)
{ {
int *tmp; int *tmp;
int i; int i;
int j; int j;
t_list *swap; t_list *swap;
j = 1; j = 1;
@ -152,4 +113,23 @@ t_list *set_list(int nbrarg, char **srcs)
j++; j++;
} }
return (swap); return (swap);
} }
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);
}

View File

@ -6,56 +6,67 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/25 23:15:17 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 #ifndef PUSH_SWAP_H
# define FT_PRINTF_H # define PUSH_SWAP_H
# include <stdio.h> # include <stdio.h>
# include <unistd.h> # include <unistd.h>
# include "./libft/libft.h" # include "./libft/libft.h"
typedef struct process_list typedef struct procest_slist
{ {
void *action; void *action;
struct process_list *next; struct procest_slist *next;
} s_list; } t_slist;
void sort3(t_list *list); void sort3(t_list *list);
int push_swap(int nbrarg, char **list); int push_swap(int nbrarg, char **list);
int is_nbr(int nbrarg, char **list); int is_nbr(int nbrarg, char **list);
t_list *set_list(int nbrarg, char **list); t_list *set_list(int nbrarg, char **list);
void lst_indexing(t_list *list); void lst_indexing(t_list *list);
int is_double(int nbrarg, char **list); int is_double(int nbrarg, char **list);
int is_sorted(t_list *list); int is_sorted(t_list *list);
void find_loop_index(t_list *list); t_list *find_loop_index(t_list *list);
int find_loop(t_list *start, t_list *list, int setswap); int find_loop(t_list *start, t_list *list, int setswap);
t_list *ft_lstnew(void *content); 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); /*process fonction*/
void s_lstadd_back(s_list **alst, s_list *new); void ft_sa_sb(t_list **list, t_slist **process, char type, int set);
s_list *s_lstlast(s_list *lst); void ft_ra_rb(t_list **list, t_slist **process, char type);
void s_lstclear(s_list **lst); t_list *ft_pa(t_list **list_a, t_list **list_b, t_slist **process);
void printf_slist(s_list *start); 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); /*sort fonction*/
t_list *ft_ra_rb(t_list **list, s_list **process, char type); t_list *best_b(t_list **b, t_list *start_b, t_list *a, int best_move);
t_list *ft_pa(t_list **list_a, t_list **list_b, s_list **process); t_list *swap(t_list *a, t_list **b, t_slist **action, t_list *to_swap);
t_list *ft_pb(t_list **list_a, t_list **list_b, s_list **process); t_list *make_list(int place, t_list *list, t_slist *action, char type);
t_list *ft_rra_rrb(t_list **list, s_list **process, 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);
t_list *swap(t_list *a, t_list **b, s_list **action, t_list *to_swap); int find_place_for_b(t_list *list, int index, int size_b);
t_list *make_list(int place, t_list *list, s_list *action, char type); int find_nbr(int size_b, t_list *a, t_list *b, int index_b);
t_list *swap_to_a(t_list *a, t_list *b, t_list *start_b, s_list *action); t_list *swap_to_b(t_list *list, t_list *best, t_list *b, int loop);
int find_place_for_b(t_list *list, int index, int size_b); t_list *turn_list(t_list *a, t_slist *action);
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);
/*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 #endif

65
set_a.c
View File

@ -6,13 +6,13 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/07 19:14:03 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" #include "push_swap.h"
void find_loop_index(t_list *list) t_list *find_loop_index(t_list *list)
{ {
t_list *start; t_list *start;
t_list *save; t_list *save;
@ -33,14 +33,14 @@ void find_loop_index(t_list *list)
list = list->next; list = list->next;
} }
find_loop(start, save, 1); 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 find_loop(t_list *start, t_list *list, int setswap)
{ {
int countdown; int countdown;
int loop_size; int loop_size;
t_list *swap; t_list *swap;
loop_size = 0; loop_size = 0;
@ -49,7 +49,7 @@ int find_loop(t_list *start, t_list *list, int setswap)
while (countdown) while (countdown)
{ {
if (swap) if (swap)
{ {
if (swap->index > list->index) if (swap->index > list->index)
{ {
list = swap; list = swap;
@ -66,43 +66,36 @@ int find_loop(t_list *start, t_list *list, int setswap)
return (loop_size); 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;
int lst_size; t_slist *action;
s_list *action;
action = 0; action = 0;
lst_size = ft_lstsize(list); lst_size = ft_lstsize(list);
swap = 0;
while (lst_size) while (lst_size)
{ {
ft_sa_sb(&list, &action, 'a', 0); list = is_swap(list, &action, &loop, best);
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);
if (list->swap) if (list->swap)
ft_pb(&list, &b, &action); ft_pb(&list, &b, &action);
else else
ft_ra_rb(&list, &action, 'a'); ft_ra_rb(&list, &action, 'a');
lst_size--; lst_size--;
} }
list = swap_to_a(list, b, b, action); list = swap_to_a(list, b, b, action);
list = turn_list(list, action); list = turn_list(list, action);
optimise_move(&action, 0, 0);
optimise_move2(&action, 0, 0);
printf_slist(action); printf_slist(action);
s_lstclear(&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; t_list *start;
int place; int place;
start = a; start = a;
place = 0; place = 0;
while (a->index > 1) while (a->index > 1)
@ -111,19 +104,33 @@ t_list *turn_list(t_list *a, s_list *action)
place++; place++;
} }
a = start; a = start;
//printf("place = %d size = %d\n", place, ft_lstsize(a));
if (place > ft_lstsize(a) / 2 && place > 2) if (place > ft_lstsize(a) / 2 && place > 2)
{ {
//printf("if\n");
while (ft_lstsize(start) - (place++)) while (ft_lstsize(start) - (place++))
ft_rra_rrb(&start, &action, 'a'); ft_rra_rrb(&start, &action, 'a');
} }
else else
{ {
//printf("else\n");
while (place--) while (place--)
ft_ra_rb(&start, &action, 'a'); ft_ra_rb(&start, &action, 'a');
} }
//printf_list(start);
return (start); return (start);
} }
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);
}

0
sort_liltte.c Normal file
View File

View File

@ -12,27 +12,27 @@
#include "push_swap.h" #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_for_b;
int place; int place;
t_list *start_b; t_list *start_b;
start_b = *b; start_b = *b;
place = 0; place = 0;
rotate = 0;
while ((*b)->index != to_swap->index) while ((*b)->index != to_swap->index)
{ {
place++; place++;
*b = (*b)->next; *b = (*b)->next;
} }
start_b = make_list(place, start_b, *action, 'b'); 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); *b = ft_pa(&a, &start_b, action);
return (a); 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; int lstsize;
@ -49,7 +49,6 @@ t_list *make_list(int place, t_list *list, s_list *action, char type)
{ {
while (place) while (place)
{ {
ft_ra_rb(&list, &action, type); ft_ra_rb(&list, &action, type);
place--; place--;
} }
@ -57,16 +56,15 @@ t_list *make_list(int place, t_list *list, s_list *action, char type)
return (list); 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 lst_size;
int size_b; int size_b;
int move; int move;
int best_move; int best_move;
t_list *best_b; t_list *to_swap;
int i = 0;
best_b = 0; to_swap = 0;
size_b = ft_lstsize(b); size_b = ft_lstsize(b);
move = size_b + ft_lstsize(a); move = size_b + ft_lstsize(a);
best_move = move; 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); best_move = ft_lstsize(a) + ft_lstsize(b);
b = start_b; b = start_b;
while (b) to_swap = best_b(&b, start_b, a, best_move);
{ a = swap(a, &start_b, &action, to_swap);
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);
lst_size--; lst_size--;
} }
return (a); return (a);
@ -137,36 +126,3 @@ int find_nbr(int size_b, t_list *a, t_list *b, int index_b)
move += place; move += place;
return (move); 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();
}*/

View File

@ -1,7 +1,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* s_list.c :+: :+: :+: */ /* t_slist.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
@ -12,11 +12,11 @@
#include "push_swap.h" #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) if (!new)
return (0); return (0);
new->action = content; new->action = content;
@ -24,9 +24,9 @@ s_list *new_slist(void *content)
return (new); return (new);
} }
void s_lstclear(s_list **lst) void s_lstclear(t_slist **lst)
{ {
s_list *chr; t_slist *chr;
chr = *lst; chr = *lst;
while (*lst) while (*lst)
@ -38,7 +38,7 @@ void s_lstclear(s_list **lst)
lst = 0; 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) if (*alst == 0)
*alst = new; *alst = new;
@ -47,26 +47,24 @@ void s_lstadd_back(s_list **alst, s_list *new)
new->next = 0; new->next = 0;
} }
s_list *s_lstlast(s_list *lst) t_slist *s_lstlast(t_slist *lst)
{ {
if (!lst) if (!lst)
return (0); return (0);
while (lst->next) while (lst->next)
{
//printf("test lst = %s\n", (char*)lst->action);
lst = lst->next; lst = lst->next;
}
return (lst); return (lst);
} }
void printf_slist(s_list *start) void printf_slist(t_slist *start)
{ {
int i = 0; int i;
while(start)
i = 0;
while (start)
{ {
i++; i++;
ft_putstr_fd((char*)start->action, 1); ft_putendl_fd((char *)start->action, 1);
start = start->next; start = start->next;
} }
return;
} }