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
obj/
Session.vim
test.txt
checker_linux

View File

@ -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

View File

@ -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);

View File

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

View File

@ -12,7 +12,6 @@
#include "libft.h"
#include "push_swap.h"
#include <stdatomic.h>
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)
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);
}

View File

@ -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);
}