From 32979952b9e5785733d37a8ebc7c19c9ee63ab69 Mon Sep 17 00:00:00 2001 From: whaffman Date: Mon, 18 Nov 2024 17:02:24 +0100 Subject: [PATCH] sortmore works --- .gitignore | 2 ++ Makefile | 7 ++++--- inc/push_swap.h | 12 ++++++++++++ src/push_swap.c | 4 +--- src/sort/sort5.c | 1 - src/sort/sortmore.c | 42 ++++++++++++++++++++++++++++++++++++------ src/util/utils.c | 36 ++++++++++++++++++++++++++++-------- 7 files changed, 83 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 3b787b8..36b8e97 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ a.out push_swap obj/ Session.vim +test.txt +checker_linux diff --git a/Makefile b/Makefile index e2cd596..b7a3a60 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ OBJ_PATH = obj VPATH = src:src/util:src/moves:src/sort SOURCES = push_swap.c ft_lstat.c new_element.c push.c rotate.c reverse_rotate.c \ - swap.c sort3.c sort5.c stack_min.c stack_max.c utils.c + swap.c sort3.c sort5.c stack_min.c stack_max.c utils.c sortmore.c OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) @@ -63,8 +63,9 @@ fclean: clean re: fclean all run: all - @$(eval ARG = $(shell shuf -i 0-100 -n 5)) - ./$(NAME) $(ARG) + @$(eval ARG = $(shell shuf -i 0-1000 -n 100)) + @./$(NAME) $(ARG) | ./checker_linux $(ARG) + @./$(NAME) $(ARG) | wc -l debug: CFLAGS += -DDEBUG -g debug: fclean diff --git a/inc/push_swap.h b/inc/push_swap.h index 1b4afeb..dfaab6e 100644 --- a/inc/push_swap.h +++ b/inc/push_swap.h @@ -21,6 +21,18 @@ typedef struct s_state t_list *b; } t_state; +int merge_to_a(t_state *state); +void sortmore(t_state *state); + +void push_all_but_3_b(t_state *state); +int ft_abs(int a); +int ft_max(int a, int b); +int ft_min(int a, int b); +int count_gt(int n, t_list *stack); +int put_at_index_asc(int number, t_list *stack); +int put_at_index_desc(int number, t_list *stack); +void rotate_a(int n, t_state *state); +void rotate_b(int n, t_state *state); void rotate_a_to(int index, t_state *state); void rotate_b_to(int index, t_state *state); int index_of(int number, t_list *stack); diff --git a/src/push_swap.c b/src/push_swap.c index a006790..ea4127f 100644 --- a/src/push_swap.c +++ b/src/push_swap.c @@ -70,8 +70,6 @@ int main(int argc, char *argv[]) return (1); } } - print_state(state); - sort5(state); - print_state(state); + sortmore(state); return (0); } diff --git a/src/sort/sort5.c b/src/sort/sort5.c index bf212e3..0902aa3 100644 --- a/src/sort/sort5.c +++ b/src/sort/sort5.c @@ -12,7 +12,6 @@ #include "libft.h" #include "push_swap.h" -#include int rotate_a_to_top(t_state *state) { diff --git a/src/sort/sortmore.c b/src/sort/sortmore.c index f89a999..d3c0d3a 100644 --- a/src/sort/sortmore.c +++ b/src/sort/sortmore.c @@ -22,7 +22,7 @@ int count_moves_merge_a(int idx_b, t_state *state) if (shortest_a * shortest_b > 0) return (ft_max(ft_abs(shortest_a), ft_abs(shortest_b)) + 1); - return (ft_abs(shortest_a + shortest_b) + 1); + return (ft_abs(shortest_a) + ft_abs(shortest_b) + 1); } int best_merge_a(t_state *state) @@ -51,14 +51,44 @@ int best_merge_a(t_state *state) void b_merge_a(int idx_b, t_state *state) { - rotate_b_to(idx_b, state); - merge_to_a(state); + const int number_b = *(int *) ft_lstat(state->b, idx_b)->content; + const int idx_a = put_at_index_asc(number_b, state->a); + int shortest_a; + int shortest_b; + + shortest_a = shortest_rotate(idx_a, ft_lstsize(state->a)); + shortest_b = shortest_rotate(idx_b, ft_lstsize(state->b)); + if (shortest_a * shortest_b > 0) + { + if (shortest_a > 0) + { + while (shortest_a > 0 && shortest_b > 0) + { + rr(state); + shortest_a--; + shortest_b--; + } + } + else + { + while (shortest_a < 0 && shortest_b < 0) + { + rrr(state); + shortest_a++; + shortest_b++; + } + } + } + rotate_a(shortest_a, state); + rotate_b(shortest_b, state); + pa(state); } void sortmore(t_state *state) { + push_all_but_3_b(state); + sort3(state); while (state->b) - b_merge_a(best_merge_a(state)) + b_merge_a(best_merge_a(state), state); + rotate_a_to_top(state); } - - diff --git a/src/util/utils.c b/src/util/utils.c index a49987c..b361923 100644 --- a/src/util/utils.c +++ b/src/util/utils.c @@ -21,11 +21,8 @@ int shortest_rotate(int index, int size) } //TODO -void rotate_a_to(int index, t_state *state) +void rotate_a(int n, t_state *state) { - int n; - - n = shortest_rotate(index, ft_lstsize(state->a)); while (n > 0) { ra(state); @@ -37,11 +34,9 @@ void rotate_a_to(int index, t_state *state) n++; } } -void rotate_b_to(int index, t_state *state) -{ - int n; - n = shortest_rotate(index, ft_lstsize(state->b)); +void rotate_b(int n, t_state *state) +{ while (n > 0) { rb(state); @@ -54,6 +49,21 @@ void rotate_b_to(int index, t_state *state) } } +void rotate_a_to(int index, t_state *state) +{ + int n; + + n = shortest_rotate(index, ft_lstsize(state->a)); + rotate_a(n, state); +} +void rotate_b_to(int index, t_state *state) +{ + int n; + + n = shortest_rotate(index, ft_lstsize(state->b)); + rotate_b(n, state); +} + int index_of(int number, t_list *stack) { int i; @@ -120,3 +130,13 @@ int put_at_index_desc(int number, t_list *stack) return ((i + lt) % size); } + +void push_all_but_3_b(t_state *state) +{ + const int size = ft_lstsize(state->a); + int i; + + i = 0; + while (i++ < size - 3) + pb(state); +}