diff --git a/Makefile b/Makefile index d5da6ca..57c4cc1 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: apommier +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2021/11/13 13:06:47 by apommier #+# #+# # -# Updated: 2022/01/08 18:27:15 by apommier ### ########.fr # +# Updated: 2022/01/14 20:19:54 by apommier ### ########.fr # # # # **************************************************************************** # @@ -15,9 +15,11 @@ SRCS = push_swap.c \ process.c\ main.c\ sorting.c\ + s_list.c\ + set_a.c\ indexing.c OBJS = ${SRCS:.c=.o} -CFLAGS = +CFLAGS = RM = rm -rf LIBFT = ./libft diff --git a/libft/Makefile b/libft/Makefile index 092636c..626fa36 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -35,7 +35,7 @@ SRCS = ft_memset.c \ ft_putnbr_fd.c OBJS = ${SRCS:.c=.o} BONUS_C = ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c ft_lstadd_back.c \ - ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c + 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 diff --git a/sort5.c b/libft/ft_lstbeforelast.c similarity index 64% rename from sort5.c rename to libft/ft_lstbeforelast.c index d99e8f9..7218e24 100644 --- a/sort5.c +++ b/libft/ft_lstbeforelast.c @@ -1,13 +1,28 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* sort5.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2021/11/25 23:15:27 by apommier #+# #+# */ -/* Updated: 2021/11/25 23:15:27 by apommier ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" \ No newline at end of file +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstbeforelast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/09 19:16:55 by apommier #+# #+# */ +/* Updated: 2022/01/13 19:00:16 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstbeforelast(t_list *lst) +{ + t_list *save; + + save = 0; + if (!lst) + return (0); + while (lst->next) + { + save = lst; + lst = lst->next; + } + return (save); +} \ No newline at end of file diff --git a/libft/ft_lstnew.c b/libft/ft_lstnew.c index 346f1d5..1acbb5b 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/06 01:35:14 by apommier ### ########.fr */ +/* Updated: 2022/01/15 23:45:37 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ t_list *ft_lstnew(void *content) new = (t_list*)malloc(sizeof(t_list)); if (!new) return (0); + new->swap = 0; new->nbr = content; new->next = 0; return (new); diff --git a/libft/libft.h b/libft/libft.h index 805d6d5..1a76de7 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/09 18:49:47 by apommier ### ########.fr */ +/* Updated: 2022/01/13 18:56:56 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,5 +70,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); #endif diff --git a/main.c b/main.c index 250f21a..e5e2774 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/25 23:15:43 by apommier #+# #+# */ -/* Updated: 2022/01/08 22:12:05 by apommier ### ########.fr */ +/* Updated: 2022/01/15 20:51:55 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,6 @@ int main(int argc, char *argv[]) ft_putstr_fd("Error\n", 2); return (1); } - if (argc == 2) - return (1); push_swap(argc, argv); - printf("------end------\n"); return (1); } \ No newline at end of file diff --git a/process.c b/process.c index f37d2e9..88b64a9 100644 --- a/process.c +++ b/process.c @@ -22,10 +22,10 @@ t_list *ft_sa_sb(t_list **list, s_list **process, char type, int set) (*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")); + if (type == 'a') + s_lstadd_back(process, new_slist("sa\n")); + else if (type == 'b') + s_lstadd_back(process, new_slist("sb\n")); } } @@ -37,10 +37,10 @@ t_list *ft_ra_rb(t_list **list, s_list **process, char type) (ft_lstlast(*list))->next = *list; *list = (*list)->next; swap->next = 0; - if (type = 'a') - s_lstadd_back(process, new_slist("ra")); - else if (type = 'b') - s_lstadd_back(process, new_slist("rb")); + if (type == 'a') + s_lstadd_back(process, new_slist("ra\n")); + else if (type == 'b') + s_lstadd_back(process, new_slist("rb\n")); } t_list *ft_pa(t_list **list_a, t_list **list_b, s_list **process) @@ -50,7 +50,8 @@ t_list *ft_pa(t_list **list_a, t_list **list_b, s_list **process) swap = *list_b; *list_b = (*list_b)->next; ft_lstadd_front(list_a, swap); - s_lstadd_back(process, new_slist("pa")); + s_lstadd_back(process, new_slist("pa\n")); + return (*list_b); } t_list *ft_pb(t_list **list_a, t_list **list_b, s_list **process) @@ -60,12 +61,18 @@ t_list *ft_pb(t_list **list_a, t_list **list_b, s_list **process) swap = *list_a; *list_a = (*list_a)->next; ft_lstadd_front(list_b, swap); - s_lstadd_back(process, new_slist("pb")); + s_lstadd_back(process, new_slist("pb\n")); } t_list * ft_rra_rrb(t_list **list, s_list **process, char type) { t_list *swap; - swap = *list; + 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")); } diff --git a/push_swap.c b/push_swap.c index 47ed205..cf4b1a0 100644 --- a/push_swap.c +++ b/push_swap.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/25 23:10:37 by apommier #+# #+# */ -/* Updated: 2022/01/08 18:25:27 by apommier ### ########.fr */ +/* Updated: 2022/01/15 23:28:47 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,32 +23,35 @@ void printf_list(t_list *start) printf("lst%d --- nbr: %d index: %d\n", i,*(int*)start->nbr, start->index); start = start->next; } - printf("----end_print----"); + printf("----end_print----\n"); return; } int is_nbr(int nbrarg, char **list) { - int i; - int j; + int i; + int j; i = 0; - j = 0; - while (++j < nbrarg) + j = 0; + while (--nbrarg) { + j++; i = 0; - if (list[j][i] == '-' && !list[j][i++]) - return (0); while (list[j][i]) { - if (list[j][i] < '0' || list[j][i] > '9') - { + while (list[j][i] == ' ') + i++; + if (list[j][i] == '-' || (list[j][i] == '+')) + i++; + if (list[j][i] && (list[j][i] < '0' || list[j][i] > '9')) + return (0); + while (list[j][i] && list[j][i] >= '0' && list[j][i] <= '9') + i++; + if (list[j][i] && list[j][i] != ' ') return (0); - } - i++; } } - return (1); } @@ -90,19 +93,15 @@ int push_swap(int nbrarg, char **list) return (0); } start = set_list(--nbrarg, list); + if (ft_lstsize(start) == 1) + return(1); lst_indexing(start); - printf_list(start); find_loop_index(start); - if (is_sorted(start)) - { - ft_lstclear(&start, &free); - return (1); - } - ft_lstclear(&start, &free); + ft_lstclear(&start, &free); return (1); } -t_list *set_list(int nbrarg, char **srcs) +/*t_list *set_list(int nbrarg, char **srcs) { int *tmp; t_list *start; @@ -112,37 +111,45 @@ t_list *set_list(int nbrarg, char **srcs) swap = 0; *(srcs)++; tmp = ft_calloc(sizeof(int), 1); - *tmp = ft_atoi(*srcs); + *tmp = ft_atoi*(srcs); start = ft_lstnew(tmp); swap = start; while (--nbrarg) { *(srcs)++; tmp = ft_calloc(sizeof(int), 1); - *tmp = ft_atoi(*srcs); + *tmp = ft_atoi*(srcs); swap->next = ft_lstnew(tmp); swap = swap->next; } return (start); -} +}*/ -int is_sorted(t_list *list) +t_list *set_list(int nbrarg, char **srcs) { - while (list->next) + int *tmp; + int i; + int j; + t_list *swap; + + j = 1; + tmp = 0; + i = 0; + swap = 0; + while (nbrarg--) { - if (*(int*)list->nbr > *(int*)list->next->nbr) - return (0); - else - list = list->next; + i = 0; + while (srcs[j][i]) + { + tmp = ft_calloc(sizeof(int), 1); + *tmp = ft_atoi(&srcs[j][i]); + ft_lstadd_back(&swap, ft_lstnew(tmp)); + while (srcs[j][i] == ' ') + i++; + while (srcs[j][i] && srcs[j][i] != ' ') + i++; + } + j++; } - return (1); -} - -/*void choose_algo(t_list *list) -{ - int size; - - size = ft_lstsize(list); - if (size == 3) - sort3(list); -}*/ \ No newline at end of file + return (swap); +} \ No newline at end of file diff --git a/push_swap.h b/push_swap.h index 48b0d44..d537a87 100644 --- a/push_swap.h +++ b/push_swap.h @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/25 23:15:17 by apommier #+# #+# */ -/* Updated: 2022/01/09 18:51:38 by apommier ### ########.fr */ +/* Updated: 2022/01/14 23:03:47 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,18 +33,27 @@ int is_sorted(t_list *list); void find_loop_index(t_list *list); int find_loop(t_list *start, t_list *list, int setswap); t_list *ft_lstnew(void *content); -void s_lstclear(s_list **lst, void (*del)(void*)); -void swap_to_b(t_list *list, t_list *best, int loop); + + 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); 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_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); diff --git a/s_list.c b/s_list.c index 91d2f34..6689042 100644 --- a/s_list.c +++ b/s_list.c @@ -24,7 +24,7 @@ s_list *new_slist(void *content) return (new); } -void s_lstclear(s_list **lst, void (*del)(void*)) +void s_lstclear(s_list **lst) { s_list *chr; @@ -32,7 +32,6 @@ void s_lstclear(s_list **lst, void (*del)(void*)) while (*lst) { chr = (*lst)->next; - del((*lst)->action); free(*lst); *lst = chr; } @@ -44,7 +43,8 @@ void s_lstadd_back(s_list **alst, s_list *new) if (*alst == 0) *alst = new; else - ft_lstlast(*alst)->next = new; + s_lstlast(*alst)->next = new; + new->next = 0; } s_list *s_lstlast(s_list *lst) @@ -52,6 +52,21 @@ s_list *s_lstlast(s_list *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) +{ + int i = 0; + while(start) + { + i++; + ft_putstr_fd((char*)start->action, 1); + start = start->next; + } + return; +} diff --git a/set_a.c b/set_a.c new file mode 100644 index 0000000..3a123a6 --- /dev/null +++ b/set_a.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* set_a.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/07 19:14:03 by apommier #+# #+# */ +/* Updated: 2022/01/15 23:30:15 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void find_loop_index(t_list *list) +{ + t_list *start; + t_list *save; + int loop_size; + int loop_size2; + + loop_size2 = 0; + loop_size = 0; + start = list; + while (list) + { + loop_size = find_loop(start, list, 0); + if (loop_size > loop_size2) + { + loop_size2 = loop_size; + save = list; + } + list = list->next; + } + find_loop(start, save, 1); + swap_to_b(start, save, 0, loop_size2); + +} + +int find_loop(t_list *start, t_list *list, int setswap) +{ + int countdown; + int loop_size; + t_list *swap; + + loop_size = 0; + swap = list->next; + countdown = ft_lstsize(start) - 1; + while (countdown) + { + if (swap) + { + if (swap->index > list->index) + { + list = swap; + loop_size++; + } + else if (setswap) + swap->swap = 1; + countdown--; + swap = swap->next; + } + else + swap = start; + } + return (loop_size); +} + +void swap_to_b(t_list *list, t_list *best, t_list *b, int loop) +{ + int swap; + int lst_size; + s_list *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); + if (list->swap) + ft_pb(&list, &b, &action); + else + ft_ra_rb(&list, &action, 'a'); + lst_size--; + } + list = swap_to_a(list, b, b, action); + list = turn_list(list, action); + printf_slist(action); + s_lstclear(&action); +} + +t_list *turn_list(t_list *a, s_list *action) +{ + t_list *start; + int place; + + start = a; + place = 0; + while (a->index > 1) + { + a = a->next; + 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 diff --git a/sort3.c b/sort3.c deleted file mode 100644 index db94417..0000000 --- a/sort3.c +++ /dev/null @@ -1,41 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* sort3.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2021/11/25 23:15:22 by apommier #+# #+# */ -/* Updated: 2021/11/25 23:15:22 by apommier ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -void sort3(t_list *list) -{ - if (list->nbr < list->next->nbr && list->nbr < list->next->next->nbr) - { - ft_sa_sb(&list, process, 'a'); - ft_ra_rb(&list, process, 'a'); - } - if (list->nbr > list->next->nbr && list->nbr > list->next->next->nbr) - { - if (list->next->nbr > list->next->next->nbr) - { - ft_sa_sb(&list, process, 'a'); - ft_ra_rb(&list, process, 'a'); - ft_ra_rb(&list, process, 'a'); - } - else - ft_ra_rb(&list, process, 'a'); - } - if (list->nbr > list->next->nbr && list->nbr < list->next->next->nbr) - ft_sa_sb(&list, process, 'a'); - if (list->nbr < list->next->nbr && list->next->next->nbr) - { - ft_ra_rb(&list, process, 'a'); - ft_ra_rb(&list, process, 'a'); - } - return ; -} \ No newline at end of file diff --git a/sorting.c b/sorting.c index 1450d33..0566601 100644 --- a/sorting.c +++ b/sorting.c @@ -12,108 +12,161 @@ #include "push_swap.h" - -void find_loop_index(t_list *list) +t_list *swap(t_list *a, t_list **b, s_list **action, t_list *to_swap) { - t_list *start; - t_list *save; - int loop_size; - int loop_size2; + int rotate; + 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'); + *b = ft_pa(&a, &start_b, action); + return (a); +} + +t_list *make_list(int place, t_list *list, s_list *action, char type) +{ + int lstsize; + + lstsize = ft_lstsize(list); + if (place > lstsize / 2) + { + while (lstsize - place) + { + ft_rra_rrb(&list, &action, type); + place++; + } + } + else + { + while (place) + { + + ft_ra_rb(&list, &action, type); + place--; + } + } + return (list); +} + +t_list *swap_to_a(t_list *a, t_list *b, t_list *start_b, s_list *action) +{ + int lst_size; + int size_b; + int move; + int best_move; + t_list *best_b; + int i = 0; + + best_b = 0; + size_b = ft_lstsize(b); + move = size_b + ft_lstsize(a); + best_move = move; + lst_size = size_b; + while (lst_size) + { + 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); + lst_size--; + } + return (a); +} + +int find_place_for_b(t_list *list, int index, int size_b) +{ + int count; + int save_index; + t_list *start; - loop_size2 = 0; - loop_size = 0; start = list; + save_index = ft_lstsize(list) + size_b; + count = 0; while (list) { - loop_size = find_loop(start, list, 0); - if (loop_size > loop_size2) - { - loop_size2 = loop_size; - save = list; - } + if (list->index > index && list->index < save_index) + save_index = list->index; list = list->next; } - find_loop(start, save, 1); - swap_to_b(start, save, loop_size2); - printf("loop_size = %d\n", loop_size2); + while (start->index != save_index) + { + count++; + start = start->next; + } + return (count); } -int find_loop(t_list *start, t_list *list, int setswap) +int find_nbr(int size_b, t_list *a, t_list *b, int index_b) { - int countdown; - int loop_size; - t_list *swap; + int move; + int place; + int size_a; - loop_size = 0; - swap = list->next; - countdown = ft_lstsize(start) - 1; - while (countdown) - { - if (swap) - { - if (list->index == swap->index - 1 - loop_size) - loop_size++; - else if (setswap) - swap->swap = 1; - countdown--; - swap = swap->next; - } - else - swap = start; + size_a = ft_lstsize(a); + place = 1; + move = 0; + while (b->index != index_b) + { + place++; + b = b->next; } - printf("------------return= %d\n", loop_size); - return (loop_size); -} + move = find_place_for_b(a, b->index, size_b); + if (move > size_a / 2) + move = size_a - move; + if (place > size_b / 2) + move += size_b - place; + else + move += place; + return (move); +} -void swap_to_b(t_list *list, t_list *best, int loop) +/*void choose_swap(t_list *a, t_list *b, s_list *action, int lst_size) { - s_list *action; - int swap; - int lst_size; - t_list *b; + int size_a; + int size_b; + t_list *save_b; + int move; + int save_move; - b = 0; - action = 0; - lst_size = ft_lstsize(list); - swap = 0; - printf_list(list); + size_b = lst_size; + size_a = ft_lstsize(a); + printf("swapto a\n"); + move = 0; + save_move = size_b + size_a; 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")); - } + 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 - ft_sa_sb(&list, &action, 'a', 0); - if(list->swap) - ft_pb(&list, &b, &action); - else if (!list->swap) - ft_ra_rb(&list, &action, 'a'); + move += lst_size; + if (move < save_move) + { + save_move = move; + save_b = b; + } + b = b->next; lst_size--; - printf_list(list); } - printf("\nswap done\n"); -} - - - -/*while (countdown - 1) - { - if (swap) - { - if (list->index == swap->index - 1 - loop_size) - { - printf("swap= %d index= %d so ++++++\n", *(int*)swap->nbr, swap->index); - loop_size++; - } - else - printf("nop swap = %d index = %d\n", *(int*)swap->nbr, swap->index); - countdown--; - swap = swap->next; - } - else - swap = start; - }*/ \ No newline at end of file + find_nbr(); +}*/ \ No newline at end of file