sortmore works

This commit is contained in:
whaffman 2024-11-18 17:02:24 +01:00
parent f28600d8a6
commit 32979952b9
7 changed files with 83 additions and 21 deletions

2
.gitignore vendored
View File

@ -5,3 +5,5 @@ a.out
push_swap push_swap
obj/ obj/
Session.vim Session.vim
test.txt
checker_linux

View File

@ -21,7 +21,7 @@ OBJ_PATH = obj
VPATH = src:src/util:src/moves:src/sort 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 \ 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)) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))
@ -63,8 +63,9 @@ fclean: clean
re: fclean all re: fclean all
run: all run: all
@$(eval ARG = $(shell shuf -i 0-100 -n 5)) @$(eval ARG = $(shell shuf -i 0-1000 -n 100))
./$(NAME) $(ARG) @./$(NAME) $(ARG) | ./checker_linux $(ARG)
@./$(NAME) $(ARG) | wc -l
debug: CFLAGS += -DDEBUG -g debug: CFLAGS += -DDEBUG -g
debug: fclean debug: fclean

View File

@ -21,6 +21,18 @@ typedef struct s_state
t_list *b; t_list *b;
} t_state; } 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_a_to(int index, t_state *state);
void rotate_b_to(int index, t_state *state); void rotate_b_to(int index, t_state *state);
int index_of(int number, t_list *stack); int index_of(int number, t_list *stack);

View File

@ -70,8 +70,6 @@ int main(int argc, char *argv[])
return (1); return (1);
} }
} }
print_state(state); sortmore(state);
sort5(state);
print_state(state);
return (0); return (0);
} }

View File

@ -12,7 +12,6 @@
#include "libft.h" #include "libft.h"
#include "push_swap.h" #include "push_swap.h"
#include <stdatomic.h>
int rotate_a_to_top(t_state *state) int rotate_a_to_top(t_state *state)
{ {

View File

@ -22,7 +22,7 @@ int count_moves_merge_a(int idx_b, t_state *state)
if (shortest_a * shortest_b > 0) if (shortest_a * shortest_b > 0)
return (ft_max(ft_abs(shortest_a), ft_abs(shortest_b)) + 1); 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) 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) void b_merge_a(int idx_b, t_state *state)
{ {
rotate_b_to(idx_b, state); const int number_b = *(int *) ft_lstat(state->b, idx_b)->content;
merge_to_a(state); 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) void sortmore(t_state *state)
{ {
push_all_but_3_b(state);
sort3(state);
while (state->b) while (state->b)
b_merge_a(best_merge_a(state)) b_merge_a(best_merge_a(state), state);
rotate_a_to_top(state);
} }

View File

@ -21,11 +21,8 @@ int shortest_rotate(int index, int size)
} }
//TODO //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) while (n > 0)
{ {
ra(state); ra(state);
@ -37,11 +34,9 @@ void rotate_a_to(int index, t_state *state)
n++; 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) while (n > 0)
{ {
rb(state); 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 index_of(int number, t_list *stack)
{ {
int i; int i;
@ -120,3 +130,13 @@ int put_at_index_desc(int number, t_list *stack)
return ((i + lt) % size); 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);
}