done
This commit is contained in:
parent
eeb366d97e
commit
1f5c92e0b0
@ -68,7 +68,7 @@ void ft_rra_rrb(t_list **list, t_slist **process, char type)
|
||||
{
|
||||
t_list *swap;
|
||||
|
||||
if(!(*list)->next)
|
||||
if (!(*list)->next)
|
||||
return ;
|
||||
swap = ft_lstlast(*list);
|
||||
ft_lstbeforelast(*list)->next = 0;
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/25 23:15:17 by apommier #+# #+# */
|
||||
/* Updated: 2022/01/17 15:46:29 by apommier ### ########.fr */
|
||||
/* Updated: 2022/01/18 21:45:38 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,7 +15,8 @@
|
||||
|
||||
# include <stdio.h>
|
||||
# include <unistd.h>
|
||||
# include "./libft/libft.h"
|
||||
# include "../libft/libft.h"
|
||||
# include "../libft/get_next_line.h"
|
||||
|
||||
typedef struct procest_slist
|
||||
{
|
||||
@ -23,13 +24,11 @@ typedef struct procest_slist
|
||||
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);
|
||||
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);
|
||||
@ -59,6 +58,13 @@ 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);
|
||||
|
||||
/*sort little*/
|
||||
t_list *sort3(t_list *list, t_slist **action);
|
||||
void sort_little(t_list *list);
|
||||
t_list *sort5(t_list *list, t_slist **action, t_list *b);
|
||||
int is_sorted(t_list *list);
|
||||
t_list *end_sort3(t_list *list, 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);
|
||||
@ -69,4 +75,13 @@ 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);
|
||||
|
||||
/*checker*/
|
||||
int checker(t_slist *action, t_list **a);
|
||||
void do_move(t_list **lst, t_list **lst_b, t_slist *a, t_slist *delete);
|
||||
void do_move2(t_list **lst, t_list **lst_b, t_slist *a, t_slist **delete);
|
||||
t_slist *verify_list(t_slist *a);
|
||||
void big_slstclear(t_slist **lst);
|
||||
|
||||
void printf_llist(t_list *start);
|
||||
|
||||
#endif
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/18 03:31:31 by apommier #+# #+# */
|
||||
/* Updated: 2022/01/18 06:46:07 by apommier ### ########.fr */
|
||||
/* Updated: 2022/01/18 21:59:23 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -19,17 +19,18 @@
|
||||
|
||||
void printf_llist(t_list *start)
|
||||
{
|
||||
int i = 0;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
printf("----printf_list----\n");
|
||||
while(start)
|
||||
while (start)
|
||||
{
|
||||
i++;
|
||||
printf("lst%d --- nbr: %d index: %d\n", i,*(int*)start->nbr, start->index);
|
||||
printf("lst%d--nbr:%d indx: %d\n", i,*(int *)start->nbr, start->index);
|
||||
start = start->next;
|
||||
}
|
||||
printf("----end_print----");
|
||||
return;
|
||||
return ;
|
||||
}
|
||||
|
||||
void sort_little(t_list *list)
|
||||
@ -81,11 +82,9 @@ t_list *sort3(t_list *a, t_slist **action)
|
||||
|
||||
t_list *sort5(t_list *list, t_slist **action, t_list *b)
|
||||
{
|
||||
|
||||
ft_pb(&list, &b, action);
|
||||
ft_pb(&list, &b, action);
|
||||
list = sort3(list, action);
|
||||
//printf_llist(list);
|
||||
if (b->index < b->next->index)
|
||||
ft_sa_sb(&b, action, 'b', 1);
|
||||
if (b->index == 5)
|
||||
@ -94,12 +93,9 @@ t_list *sort5(t_list *list, t_slist **action, t_list *b)
|
||||
{
|
||||
list = swap(list, &b, action, b);
|
||||
}
|
||||
//printf_llist(list);
|
||||
return (list);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int is_sorted(t_list *list)
|
||||
{
|
||||
while (list->next)
|
||||
@ -111,4 +107,3 @@ int is_sorted(t_list *list)
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
||||
BIN
checker_folder/a.out
Executable file
BIN
checker_folder/a.out
Executable file
Binary file not shown.
29
checker_folder/checker_utils.c
Normal file
29
checker_folder/checker_utils.c
Normal file
@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* checker_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/18 02:14:22 by apommier #+# #+# */
|
||||
/* Updated: 2022/01/18 06:49:36 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../Utils/push_swap.h"
|
||||
|
||||
void do_move2(t_list **lst, t_list **lst_b, t_slist *a, t_slist **delete)
|
||||
{
|
||||
if (!ft_strcmp("rr\n", a))
|
||||
{
|
||||
if ((*lst)->next)
|
||||
ft_ra_rb(lst, delete, 'a');
|
||||
if ((*lst_b)->next)
|
||||
ft_ra_rb(lst_b, delete, 'b');
|
||||
}
|
||||
if (!ft_strcmp("rrr\n", a))
|
||||
{
|
||||
ft_rra_rrb(lst, delete, 'a');
|
||||
ft_rra_rrb(lst_b, delete, 'a');
|
||||
}
|
||||
}
|
||||
125
checker_folder/main.c
Normal file
125
checker_folder/main.c
Normal file
@ -0,0 +1,125 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/17 21:29:45 by apommier #+# #+# */
|
||||
/* Updated: 2022/01/18 06:49:49 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../Utils/push_swap.h"
|
||||
|
||||
void big_slstclear(t_slist **lst)
|
||||
{
|
||||
t_slist *chr;
|
||||
|
||||
chr = *lst;
|
||||
while (*lst)
|
||||
{
|
||||
chr = (*lst)->next;
|
||||
free((*lst)->action);
|
||||
free(*lst);
|
||||
*lst = chr;
|
||||
}
|
||||
lst = 0;
|
||||
}
|
||||
|
||||
t_slist *verify_list(t_slist *a)
|
||||
{
|
||||
t_slist *start;
|
||||
|
||||
start = a;
|
||||
while (a && a->action)
|
||||
{
|
||||
if (!ft_strcmp("sa\n", a) || !ft_strcmp("rb\n", a)
|
||||
|| !ft_strcmp("rr\n", a) || !ft_strcmp("rra\n", a)
|
||||
|| !ft_strcmp("rrb\n", a) || !ft_strcmp("rrr\n", a)
|
||||
|| !ft_strcmp("ra\n", a) || !ft_strcmp("pa\n", a)
|
||||
|| !ft_strcmp("pb\n", a) || !ft_strcmp("sb\n", a))
|
||||
a = a->next;
|
||||
else
|
||||
{
|
||||
ft_putstr_fd("Error\n", 2);
|
||||
big_slstclear(&start);
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
return (start);
|
||||
}
|
||||
|
||||
void do_move(t_list **lst, t_list **lst_b, t_slist *a, t_slist *delete)
|
||||
{
|
||||
if (!ft_strcmp("pa\n", a))
|
||||
ft_pa(lst, lst_b, &delete);
|
||||
else if (!ft_strcmp("pb\n", a))
|
||||
ft_pb(lst, lst_b, &delete);
|
||||
else if (!ft_strcmp("sa\n", a))
|
||||
ft_sa_sb(lst, &delete, 'a', 0);
|
||||
else if (!ft_strcmp("sb\n", a))
|
||||
ft_sa_sb(lst_b, &delete, 'b', 0);
|
||||
else if (!ft_strcmp("ra\n", a))
|
||||
ft_ra_rb(lst, &delete, 'a');
|
||||
else if (!ft_strcmp("rb\n", a))
|
||||
ft_ra_rb(lst_b, &delete, 'b');
|
||||
else if (!ft_strcmp("rra\n", a))
|
||||
ft_rra_rrb(lst, &delete, 'a');
|
||||
else if (!ft_strcmp("rrb\n", a))
|
||||
ft_rra_rrb(lst_b, &delete, 'b');
|
||||
else
|
||||
do_move2(lst, lst_b, a, &delete);
|
||||
s_lstclear(&delete);
|
||||
}
|
||||
|
||||
int checker(t_slist *action, t_list **a)
|
||||
{
|
||||
t_list *lst_b;
|
||||
int lst_size;
|
||||
|
||||
lst_size = ft_lstsize(*a);
|
||||
lst_b = 0;
|
||||
action = verify_list(action);
|
||||
if (!(*a))
|
||||
return (0);
|
||||
while (action)
|
||||
{
|
||||
do_move(a, &lst_b, action, 0);
|
||||
action = action->next;
|
||||
}
|
||||
if (is_sorted(*a) && lst_size == ft_lstsize(*a))
|
||||
ft_putendl_fd("OK", 1);
|
||||
else
|
||||
ft_putendl_fd("KO", 1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
t_list *start;
|
||||
t_slist *start_action;
|
||||
char *line;
|
||||
|
||||
start_action = 0;
|
||||
if (argc == 1)
|
||||
return (1);
|
||||
if (!is_nbr(argc, argv) || !is_double(argc, argv))
|
||||
{
|
||||
ft_putstr_fd("Error\n", 2);
|
||||
return (0);
|
||||
}
|
||||
start = set_list(--argc, argv);
|
||||
lst_indexing(start);
|
||||
line = get_next_line(0);
|
||||
while (line)
|
||||
{
|
||||
if (line)
|
||||
s_lstadd_back(&start_action, new_slist(line));
|
||||
line = get_next_line(0);
|
||||
}
|
||||
checker(start_action, &start);
|
||||
ft_lstclear(&start, &free);
|
||||
big_slstclear(&start_action);
|
||||
return (1);
|
||||
}
|
||||
19
libft/get_next_line.h
Normal file
19
libft/get_next_line.h
Normal file
@ -0,0 +1,19 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/12/14 05:58:49 by apommier #+# #+# */
|
||||
/* Updated: 2022/01/17 21:45:15 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef GET_NEXT_LINE_H
|
||||
# define GET_NEXT_LINE_H
|
||||
|
||||
char *get_next_line(int fd);
|
||||
char *ft_strjoin(char *save, char *s2);
|
||||
|
||||
#endif
|
||||
124
optimise_move.c
124
optimise_move.c
@ -1,124 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
@ -1,83 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
135
push_swap.c
135
push_swap.c
@ -1,135 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* push_swap.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/25 23:10:37 by apommier #+# #+# */
|
||||
/* Updated: 2022/01/17 15:44:11 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
int is_nbr(int nbrarg, char **list)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (--nbrarg)
|
||||
{
|
||||
j++;
|
||||
i = 0;
|
||||
while (list[j][i])
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int is_double(int nbrarg, char **list)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int len;
|
||||
|
||||
i = 1;
|
||||
j = 1;
|
||||
nbrarg--;
|
||||
while (nbrarg - j)
|
||||
{
|
||||
i = 1;
|
||||
while (list[j + i])
|
||||
{
|
||||
if (ft_strlen(list[j]) > ft_strlen(list[j + i]))
|
||||
len = ft_strlen(list[j]);
|
||||
else
|
||||
len = ft_strlen(list[j + i]);
|
||||
if (ft_strncmp(list[j], list[j + i], len) == 0)
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int push_swap(int nbrarg, char **list)
|
||||
{
|
||||
t_list *start;
|
||||
|
||||
start = 0;
|
||||
if (!is_nbr(nbrarg, list) || !is_double(nbrarg, list))
|
||||
{
|
||||
ft_putstr_fd("Error\n", 2);
|
||||
return (0);
|
||||
}
|
||||
start = set_list(--nbrarg, list);
|
||||
if (ft_lstsize(start) == 1)
|
||||
return (1);
|
||||
lst_indexing(start);
|
||||
start = find_loop_index(start);
|
||||
ft_lstclear(&start, &free);
|
||||
return (1);
|
||||
}
|
||||
|
||||
t_list *set_list(int nbrarg, char **srcs)
|
||||
{
|
||||
int *tmp;
|
||||
int i;
|
||||
int j;
|
||||
t_list *swap;
|
||||
|
||||
j = 1;
|
||||
tmp = 0;
|
||||
i = 0;
|
||||
swap = 0;
|
||||
while (nbrarg--)
|
||||
{
|
||||
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 (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);
|
||||
}
|
||||
136
set_a.c
136
set_a.c
@ -1,136 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* set_a.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/07 19:14:03 by apommier #+# #+# */
|
||||
/* Updated: 2022/01/17 15:07:09 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
t_list *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);
|
||||
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;
|
||||
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);
|
||||
}
|
||||
|
||||
t_list *swap_to_b(t_list *list, t_list *best, t_list *b, int loop)
|
||||
{
|
||||
int lst_size;
|
||||
t_slist *action;
|
||||
|
||||
action = 0;
|
||||
lst_size = ft_lstsize(list);
|
||||
while (lst_size)
|
||||
{
|
||||
list = is_swap(list, &action, &loop, best);
|
||||
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);
|
||||
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, t_slist *action)
|
||||
{
|
||||
t_list *start;
|
||||
int place;
|
||||
|
||||
start = a;
|
||||
place = 0;
|
||||
while (a->index > 1)
|
||||
{
|
||||
a = a->next;
|
||||
place++;
|
||||
}
|
||||
a = start;
|
||||
if (place > ft_lstsize(a) / 2 && place > 2)
|
||||
{
|
||||
while (ft_lstsize(start) - (place++))
|
||||
ft_rra_rrb(&start, &action, 'a');
|
||||
}
|
||||
else
|
||||
{
|
||||
while (place--)
|
||||
ft_ra_rb(&start, &action, 'a');
|
||||
}
|
||||
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);
|
||||
}
|
||||
128
sorting.c
128
sorting.c
@ -1,128 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sorting.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/07 19:14:03 by apommier #+# #+# */
|
||||
/* Updated: 2022/01/07 19:14:03 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
t_list *swap(t_list *a, t_list **b, t_slist **action, t_list *to_swap)
|
||||
{
|
||||
int place_for_b;
|
||||
int place;
|
||||
t_list *start_b;
|
||||
|
||||
start_b = *b;
|
||||
place = 0;
|
||||
while ((*b)->index != to_swap->index)
|
||||
{
|
||||
place++;
|
||||
*b = (*b)->next;
|
||||
}
|
||||
start_b = make_list(place, start_b, *action, 'b');
|
||||
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, t_slist *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, t_slist *action)
|
||||
{
|
||||
int lst_size;
|
||||
int size_b;
|
||||
int move;
|
||||
int best_move;
|
||||
t_list *to_swap;
|
||||
|
||||
to_swap = 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;
|
||||
to_swap = best_b(&b, start_b, a, best_move);
|
||||
a = swap(a, &start_b, &action, to_swap);
|
||||
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;
|
||||
|
||||
start = list;
|
||||
save_index = ft_lstsize(list) + size_b;
|
||||
count = 0;
|
||||
while (list)
|
||||
{
|
||||
if (list->index > index && list->index < save_index)
|
||||
save_index = list->index;
|
||||
list = list->next;
|
||||
}
|
||||
while (start->index != save_index)
|
||||
{
|
||||
count++;
|
||||
start = start->next;
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
int find_nbr(int size_b, t_list *a, t_list *b, int index_b)
|
||||
{
|
||||
int move;
|
||||
int place;
|
||||
int size_a;
|
||||
|
||||
size_a = ft_lstsize(a);
|
||||
place = 1;
|
||||
move = 0;
|
||||
while (b->index != index_b)
|
||||
{
|
||||
place++;
|
||||
b = b->next;
|
||||
}
|
||||
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);
|
||||
}
|
||||
70
t_slist.c
70
t_slist.c
@ -1,70 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* t_slist.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/09 18:46:50 by apommier #+# #+# */
|
||||
/* Updated: 2022/01/09 18:46:50 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
t_slist *new_slist(void *content)
|
||||
{
|
||||
t_slist *new;
|
||||
|
||||
new = (t_slist *)malloc(sizeof(t_slist));
|
||||
if (!new)
|
||||
return (0);
|
||||
new->action = content;
|
||||
new->next = 0;
|
||||
return (new);
|
||||
}
|
||||
|
||||
void s_lstclear(t_slist **lst)
|
||||
{
|
||||
t_slist *chr;
|
||||
|
||||
chr = *lst;
|
||||
while (*lst)
|
||||
{
|
||||
chr = (*lst)->next;
|
||||
free(*lst);
|
||||
*lst = chr;
|
||||
}
|
||||
lst = 0;
|
||||
}
|
||||
|
||||
void s_lstadd_back(t_slist **alst, t_slist *new)
|
||||
{
|
||||
if (*alst == 0)
|
||||
*alst = new;
|
||||
else
|
||||
s_lstlast(*alst)->next = new;
|
||||
new->next = 0;
|
||||
}
|
||||
|
||||
t_slist *s_lstlast(t_slist *lst)
|
||||
{
|
||||
if (!lst)
|
||||
return (0);
|
||||
while (lst->next)
|
||||
lst = lst->next;
|
||||
return (lst);
|
||||
}
|
||||
|
||||
void printf_slist(t_slist *start)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (start)
|
||||
{
|
||||
i++;
|
||||
ft_putendl_fd((char *)start->action, 1);
|
||||
start = start->next;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user