diff --git a/builds/algo.o b/builds/algo.o index 6082257..abfa522 100644 Binary files a/builds/algo.o and b/builds/algo.o differ diff --git a/builds/print.o b/builds/print.o index 5801c25..e6dba2a 100644 Binary files a/builds/print.o and b/builds/print.o differ diff --git a/builds/push_swap.o b/builds/push_swap.o index 35bf7db..0fd39da 100644 Binary files a/builds/push_swap.o and b/builds/push_swap.o differ diff --git a/includes/push_swap.h b/includes/push_swap.h index 7b921c5..5ecdeb8 100644 --- a/includes/push_swap.h +++ b/includes/push_swap.h @@ -42,7 +42,9 @@ void hugo_sort(t_stack **a, t_stack **b, t_list *lst); /* ** print.c */ +void mark_step(t_list *solution); void fill_solution(t_list *lst, char *s); +char *fill_line(t_stack *list, char *stack); void print_result(t_list *lst, int i); /* diff --git a/push_swap b/push_swap index 4658896..20d2c50 100755 Binary files a/push_swap and b/push_swap differ diff --git a/srcs/algo.c b/srcs/algo.c index 1a3767e..8d48218 100644 --- a/srcs/algo.c +++ b/srcs/algo.c @@ -63,8 +63,14 @@ int divide_a(t_stack **a, t_stack **b, t_list *solution) (void)solution; list_size = sublist_size(*a); pivot = find_pivot(*a, list_size); - ft_putnbrendl(list_size); - ft_putnbrendl(pivot); + while (list_size-- > 0) + { + if ((*a)->n >= pivot) + ra(a, &solution); + else + pb(b, a, &solution); + } + mark_step(solution); return (0); } diff --git a/srcs/print.c b/srcs/print.c index 95fa14f..8d0a4c9 100644 --- a/srcs/print.c +++ b/srcs/print.c @@ -1,5 +1,31 @@ #include "push_swap.h" +char *fill_line(t_stack *list, char *stack) +{ + while (list != NULL) + { + if (list->limit == 1) + stack = ft_strjoinfree(ft_strdup("]"), stack); + stack = ft_strjoinfree(ft_itoa(list->n), stack); + if (list->limit == 1) + stack = ft_strjoinfree(ft_strdup("["), stack); + stack = ft_strjoinfree(ft_strdup(" "), stack); + list = list->next; + } + return (stack); +} + +void mark_step(t_list *solution) +{ + char *line; + + while (solution->next != NULL) + solution = solution->next; + line = solution->content; + line = ft_strjoinfree(line, ft_strdup("!1")); + solution->content = line; +} + /* ** this function is called by actions like sa() ** it fills a new str element on the chained list like this : @@ -29,19 +55,9 @@ void fill_solution(t_list *solution, char *sp) a = *(t_stack **)(solution->content); b = *(t_stack **)(solution->next->content); stack = ft_strjoinfree(ft_strdup("!"), ft_strdup(sp)); - while (a != NULL) - { - stack = ft_strjoinfree(ft_itoa(a->n), stack); - stack = ft_strjoinfree(ft_strdup(" "), stack); - a = a->next; - } + stack = fill_line(a, stack); stack = ft_strjoinfree(ft_strdup("!a:"), stack); - while (b != NULL) - { - stack = ft_strjoinfree(ft_itoa(b->n), stack); - stack = ft_strjoinfree(ft_strdup(" "), stack); - b = b->next; - } + stack = fill_line(b, stack); stack = ft_strjoinfree(ft_strdup("b:"), stack); ft_lstadd_back(&solution, ft_lstnew(stack)); } @@ -58,7 +74,7 @@ void print_result(t_list *result, int flag) i++; part = ft_split(result->content, '!'); ft_printf("%s\n", part[2]); - if (flag) + if (flag == 1 || (flag == 2 && part[3] != NULL)) { ft_printf(" %s\n", part[1]); ft_printf(" %s\n", part[0]); @@ -67,6 +83,7 @@ void print_result(t_list *result, int flag) free(part[0]); free(part[1]); free(part[2]); + free(part[3]); free(part); } if (flag) diff --git a/srcs/push_swap.c b/srcs/push_swap.c index d9fc949..e2a85d4 100644 --- a/srcs/push_swap.c +++ b/srcs/push_swap.c @@ -10,11 +10,19 @@ void is_valid(int ac, char **av) int check_flag(int *ac, char ***av) { - if (ft_strcmp((*av)[1], "-p") != 0) - return (0); - (*av)++; - (*ac)--; - return (1); + if (ft_strcmp((*av)[1], "-p") == 0) + { + (*av)++; + (*ac)--; + return (1); + } + if (ft_strcmp((*av)[1], "-P") == 0) + { + (*av)++; + (*ac)--; + return (2); + } + return (0); } t_stack *init_stack(int ac, char **av) @@ -48,7 +56,10 @@ t_list *launch_algo(t_stack **a, t_stack **b, int flag) solution = ft_lstnew(a); ft_lstadd_back(&solution, ft_lstnew(b)); if (flag) + { fill_solution(solution, "start"); + mark_step(solution); + } hugo_sort(a, b, solution); // bubble_sort(a, b, solution); return (solution);