diff --git a/Makefile b/Makefile index b7a3a60..b0d4e45 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 sortmore.c + swap.c sort3.c stack_min.c utils.c sortmore.c rotate_utils.c OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) diff --git a/inc/push_swap.h b/inc/push_swap.h index 87e6856..ba4865a 100644 --- a/inc/push_swap.h +++ b/inc/push_swap.h @@ -20,40 +20,35 @@ typedef struct s_state t_list *a; t_list *b; } t_state; - -int merge_to_a(t_state *state); -void sortmore(t_state *state); - -void common_rotations(int *shortest_a, int *shortest_b, t_state *state); -void push_all_but_3_b(t_state *state); +t_list *ft_lstat(t_list *list, int n); 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); -int put_at_index_asc(int number, t_list *stack); -int put_at_index_desc(int number, t_list *stack); -int rotate_a_to_top(t_state *state); -int shortest_rotate(int index, int size); -int new_element(t_list **stack, const char *str); -t_list *ft_lstat(t_list *list, int n); + +int stack_min(t_list *lst); void print_stack(const char *name, t_list *stack); void print_state(t_state *state); -int stack_max(t_list *lst); -int stack_min(t_list *lst); +void sortmore(t_state *state); int sort3(t_state *state); -int sort5(t_state *state); +int count_gt(int n, t_list *stack); +int index_of(int number, t_list *stack); +int new_element(t_list **stack, const char *str); + +int put_at_index_asc(int number, t_list *stack); +void push_all_but_3_b(t_state *state); void push(t_list **stack_1, t_list **stack_2); + +int shortest_rotate(int index, int size); +void common_rotations(int *shortest_a, int *shortest_b, t_state *state); +void rotate_b(int n, t_state *state); +void rotate_a(int n, t_state *state); +void rotate_a_to(int index, t_state *state); +int rotate_a_to_top(t_state *state); void rotate(t_list **stack); + void reverse_rotate(t_list **stack); + void swap(t_list **stack); void pa(t_state *state); diff --git a/src/moves/push.c b/src/moves/push.c index 54f40eb..407c6f1 100644 --- a/src/moves/push.c +++ b/src/moves/push.c @@ -13,6 +13,25 @@ #include "libft.h" #include "push_swap.h" +int put_at_index_asc(int number, t_list *stack) +{ + const int i = index_of(stack_min(stack), stack); + const int size = ft_lstsize(stack); + const int gt = count_gt(number, stack); + + return ((i + gt) % 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); +} + void pb(t_state *state) { push(&(state->a), &(state->b)); diff --git a/src/sort/sort5.c b/src/moves/rotate_utils.c similarity index 55% rename from src/sort/sort5.c rename to src/moves/rotate_utils.c index b4ec2cf..9177b9a 100644 --- a/src/sort/sort5.c +++ b/src/moves/rotate_utils.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: o_ :::::: ::: */ -/* sort5.c :+: / :+::+: :+: */ +/* rotate_utils.c :+: / :+::+: :+: */ /* +:+ > +:++:+ +:+ */ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ -/* Created: 2024/11/07 15:17:36 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/11/07 15:57:09 by whaffman ### ### ### ### / \ */ +/* Created: 2024/11/19 15:03:32 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/19 15:03:35 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ @@ -19,29 +19,46 @@ int rotate_a_to_top(t_state *state) return (1); } -//sort5 -int merge_to_a(t_state *state) +int shortest_rotate(int index, int size) { - const int b = *(int *) state->b->content; - - rotate_a_to(put_at_index_asc(b, state->a), state); - pa(state); - return (1); + if (index <= size / 2) + return (index); + return (-1 * (size - index)); } -int sort5(t_state *state) +//TODO +void rotate_a(int n, t_state *state) { - pb(state); - pb(state); - if (*(int *)state->b->content > *(int *)state->b->next->content) - sb(state); - sort3(state); - print_state(state); - merge_to_a(state); - print_state(state); - merge_to_a(state); - print_state(state); - rotate_a_to_top(state); - print_state(state); - return (1); + while (n > 0) + { + ra(state); + n--; + } + while (n < 0) + { + rra(state); + n++; + } +} + +void rotate_b(int n, t_state *state) +{ + while (n > 0) + { + rb(state); + n--; + } + while (n < 0) + { + rrb(state); + n++; + } +} + +void rotate_a_to(int index, t_state *state) +{ + int n; + + n = shortest_rotate(index, ft_lstsize(state->a)); + rotate_a(n, state); } diff --git a/src/sort/sortmore.c b/src/sort/sortmore.c index 6772054..401644e 100644 --- a/src/sort/sortmore.c +++ b/src/sort/sortmore.c @@ -79,7 +79,7 @@ void common_rotations(int *shortest_a, int *shortest_b, t_state *state) } else { - while (shortest_a < 0 && shortest_b < 0) + while (*shortest_a < 0 && *shortest_b < 0) { rrr(state); (*shortest_a)++; diff --git a/src/util/stack_max.c b/src/util/stack_max.c deleted file mode 100644 index 8a97de0..0000000 --- a/src/util/stack_max.c +++ /dev/null @@ -1,29 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: o_ :::::: ::: */ -/* stack_max.c :+: / :+::+: :+: */ -/* +:+ > +:++:+ +:+ */ -/* By: whaffman +#+ +:+ +#++#++:++#++ */ -/* +#+ +#+#+ +#++#+ +#+ \o/ */ -/* Created: 2024/11/07 16:29:41 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/11/07 16:36:40 by whaffman ### ### ### ### / \ */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -int stack_max(t_list *lst) -{ - int n; - - n = *(int *) lst->content; - while (lst->next) - { - if (*(int *) lst->content > n) - n = *(int *) lst->content; - lst = lst->next; - } - if (*(int *) lst->content > n) - n = *(int *) lst->content; - return (n); -} diff --git a/src/util/utils.c b/src/util/utils.c index b361923..67e16e8 100644 --- a/src/util/utils.c +++ b/src/util/utils.c @@ -13,57 +13,6 @@ #include "libft.h" #include "push_swap.h" -int shortest_rotate(int index, int size) -{ - if (index <= size / 2) - return (index); - return (-1 * (size - index)); -} - -//TODO -void rotate_a(int n, t_state *state) -{ - while (n > 0) - { - ra(state); - n--; - } - while (n < 0) - { - rra(state); - n++; - } -} - -void rotate_b(int n, t_state *state) -{ - while (n > 0) - { - rb(state); - n--; - } - while (n < 0) - { - rrb(state); - n++; - } -} - -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; @@ -78,6 +27,7 @@ int index_of(int number, t_list *stack) } return (-1); } + int ft_abs(int a) { if (a >= 0) @@ -92,13 +42,6 @@ int ft_max(int a, int b) return (b); } -int ft_min(int a, int b) -{ - if (a < b) - return (a); - return (b); -} - int count_gt(int n, t_list *stack) { int count; @@ -112,31 +55,3 @@ int count_gt(int n, t_list *stack) } return (count); } - -int put_at_index_asc(int number, t_list *stack) -{ - const int i = index_of(stack_min(stack), stack); - const int size = ft_lstsize(stack); - const int gt = count_gt(number, stack); - - return ((i + gt) % size); -} - -int put_at_index_desc(int number, t_list *stack) -{ - const int i = index_of(stack_max(stack), stack); - const int size = ft_lstsize(stack); - const int lt = size - count_gt(number, 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); -} diff --git a/tester.sh b/tester.sh new file mode 100644 index 0000000..2eec12d --- /dev/null +++ b/tester.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# This script is used to test the program +# test case 1 + +echo "Test case 1" +MAX=0 +MIN=10000 +for i in {1..100} +do + ARG=$(shuf -i 0-1000 -n 500) + N=$(./push_swap $ARG | wc -l) + if [ $N -gt $MAX ] + then + if [ $N -gt 5500 ]; then + echo "Max: $N | $(echo $ARG | tr -d '\n')" + fi + MAX=$N + fi + if [ $N -lt $MIN ] + then + MIN=$N + fi + AVG=$((AVG + N)) +done +AVG=$((AVG / 100)) +echo "Max: $MAX" +echo "Min: $MIN" +echo "Average: $AVG" +