diff --git a/_ci_tdd/test_files/test_main_miunit.c b/_ci_tdd/test_files/test_main_miunit.c index 0e181fe..b2daa80 100644 --- a/_ci_tdd/test_files/test_main_miunit.c +++ b/_ci_tdd/test_files/test_main_miunit.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 19:06:12 by umeneses #+# #+# */ -/* Updated: 2024/06/14 17:17:51 by umeneses ### ########.fr */ +/* Updated: 2024/06/18 16:47:47 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,54 +42,6 @@ int ft_minunit_array_counter(char **array); void ft_array_clear(char **array, int arr_size); void ft_array_printer(char **array, int arr_size); -// MU_TEST(test_3rd_miastep_map_get_target_position) -// { -// // ARRANGE -// t_stack *stack_a; -// t_stack *stack_b; -// char **argv_simulation = NULL; -// char *userinput; -// int expected_size; -// int actual_size; -// int expected_top_a; -// int actual_top_a; -// int expected_bottom_a; -// int actual_bottom_a; -// int expected_target_pos; -// int actual_target_pos; - -// // ACT -// stack_a = NULL; -// stack_b = (t_stack *)ft_calloc(1, sizeof(t_stack)); -// userinput = "./push_swap 2 42 1 4 8 5 31 27"; -// argv_simulation = ft_split(userinput, ' '); -// expected_size = ft_minunit_array_counter(argv_simulation); -// expected_top_a = ft_atoi(argv_simulation[1]); -// expected_bottom_a = ft_atoi(argv_simulation[8]); - -// stack_a = ft_lts_buildstack_argv(&stack_a, argv_simulation); -// expected_target_pos = 8; // argv[8] = 42; -// actual_size = ft_lst_size(stack_a); -// ft_lst_map_all_indexers(&stack_a); -// ft_push_b_all_but_three(&stack_a, &stack_b); -// ft_sort_three_nbrs(&stack_a); -// ft_lst_get_target_position(&stack_a, &stack_b); - -// actual_target_pos = ft_lst_goto_end(stack_b)->nbr; -// actual_bottom_a = ft_lst_goto_end(stack_a)->nbr; -// actual_top_a = ft_lst_goto_head(stack_a)->nbr; - -// // ASSERT -// mu_assert_int_eq(expected_target_pos, actual_target_pos); -// mu_assert_int_eq(expected_size, actual_size); -// mu_assert_int_eq(expected_top_a, actual_top_a); -// mu_assert_int_eq(expected_bottom_a, actual_bottom_a); -// ft_lstclear_single_ptr(stack_a); -// ft_lstclear_single_ptr(stack_b); -// ft_array_clear(argv_simulation, expected_size); -// } - - MU_TEST(test_3rd_miastep_get_cost) { @@ -125,15 +77,8 @@ MU_TEST(test_3rd_miastep_get_cost) ft_lst_get_cost(&stack_a, &stack_b); actual_bottom_a = ft_lst_goto_end(stack_a)->nbr; actual_top_a = ft_lst_goto_head(stack_a)->nbr; - ft_lst_printf_int_content("stack_a:", stack_a); - ft_lst_printf_int_content("stack_b:", stack_b); - t_stack *temp; - temp = stack_b; - while (temp) - { - ft_printf("nbr[%d] in pos[%d] target [%d] has cost = %d\n", temp->nbr, temp->pos, temp->target, temp->cost_b); - temp = temp->next; - } + ft_lst_printf_data_content("get_cost stack_a:", stack_a); + ft_lst_printf_data_content("get_cost stack_b:", stack_b); actual_cost = stack_b->next->cost_b; // ASSERT mu_assert_int_eq(expected_cost, actual_cost); @@ -145,7 +90,7 @@ MU_TEST(test_3rd_miastep_get_cost) ft_array_clear(argv_simulation, expected_size); } -MU_TEST(test_3rd_miastep_map_get_target) +MU_TEST(test_3rd_miastep_map_get_target_position) { // ARRANGE t_stack *stack_a; @@ -158,8 +103,8 @@ MU_TEST(test_3rd_miastep_map_get_target) int actual_top_a; int expected_bottom_a; int actual_bottom_a; - int expected_target_pos; - int actual_target_pos; + // int expected_target_pos; + // int actual_target_pos; // ACT stack_a = NULL; @@ -171,27 +116,19 @@ MU_TEST(test_3rd_miastep_map_get_target) expected_bottom_a = ft_atoi(argv_simulation[2]); stack_a = ft_lts_buildstack_argv(&stack_a, argv_simulation); - expected_target_pos = 5; // stack_b->pos[5] - actual_size = ft_lst_size(stack_a); ft_lst_map_all_indexers(&stack_a); + // expected_target_pos = 5; // stack_b->origin[5] + actual_size = ft_lst_size(stack_a); ft_push_b_all_but_three(&stack_a, &stack_b); ft_sort_three_nbrs(&stack_a); actual_bottom_a = ft_lst_goto_end(stack_a)->nbr; actual_top_a = ft_lst_goto_head(stack_a)->nbr; ft_lst_get_target_position(&stack_a, &stack_b); - actual_target_pos = stack_b->target; - t_stack *temp; - temp = stack_b; - while (temp) - { - ft_printf("nbr[%d] in pos[%d] has target [%d]\n", temp->nbr, temp->pos, temp->target); - temp = temp->next; - } - + // actual_target_pos = stack_b->target; // ASSERT - mu_assert_int_eq(expected_target_pos, actual_target_pos); + // mu_assert_int_eq(expected_target_pos, actual_target_pos); mu_assert_int_eq(expected_size, actual_size); mu_assert_int_eq(expected_top_a, actual_top_a); mu_assert_int_eq(expected_bottom_a, actual_bottom_a); @@ -200,7 +137,7 @@ MU_TEST(test_3rd_miastep_map_get_target) ft_array_clear(argv_simulation, expected_size); } -MU_TEST(test_3rd_miastep_map_lowest_weight) +MU_TEST(test_3rd_miastep_map_lowest_final_pos) { // ARRANGE t_stack *stack; @@ -212,10 +149,10 @@ MU_TEST(test_3rd_miastep_map_lowest_weight) int actual_top_a; int expected_bottom_a; int actual_bottom_a; - int expected_lowest_weight_pos; - int expected_lowest_weight_nbr; - int actual_lowest_weight_pos; - int actual_lowest_weight_nbr; + int expected_lowest_final_pos_pos; + int expected_lowest_final_pos_nbr; + int actual_lowest_final_pos_pos; + int actual_lowest_final_pos_nbr; // ACT stack = NULL; @@ -226,21 +163,21 @@ MU_TEST(test_3rd_miastep_map_lowest_weight) expected_bottom_a = ft_atoi(argv_simulation[8]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); - expected_lowest_weight_pos = 3; // argv[3] = 1; - expected_lowest_weight_nbr = ft_atoi(argv_simulation[expected_lowest_weight_pos]); + ft_lst_map_actual_position(&stack); + ft_lst_map_final_pos(&stack, expected_size); + expected_lowest_final_pos_pos = 3; // argv[3] = 1; + expected_lowest_final_pos_nbr = ft_atoi(argv_simulation[expected_lowest_final_pos_pos]); actual_size = ft_lst_size(stack); - ft_lst_map_position(&stack); - ft_lst_map_weight(&stack, expected_size); actual_top_a = ft_lst_goto_head(stack)->nbr; - actual_lowest_weight_pos = ft_lst_map_lowest_weight_position(&stack); + actual_lowest_final_pos_pos = ft_lst_map_lowest_final_position(&stack); actual_bottom_a = ft_lst_goto_end(stack)->nbr; - actual_lowest_weight_nbr = stack->next->next->nbr; + actual_lowest_final_pos_nbr = stack->next->next->nbr; // ASSERT - mu_assert_int_eq(expected_lowest_weight_pos, actual_lowest_weight_pos); - mu_assert_int_eq(expected_lowest_weight_nbr, actual_lowest_weight_nbr); + mu_assert_int_eq(expected_lowest_final_pos_pos, actual_lowest_final_pos_pos); + mu_assert_int_eq(expected_lowest_final_pos_nbr, actual_lowest_final_pos_nbr); mu_assert_int_eq(expected_size, actual_size); mu_assert_int_eq(expected_top_a, actual_top_a); mu_assert_int_eq(expected_bottom_a, actual_bottom_a); @@ -281,13 +218,13 @@ MU_TEST(test_2nd_miastep_push_b_all_but3_sort_3a) expected_bottom_a_before_push = ft_atoi(argv_simulation[6]); stack_a = ft_lts_buildstack_argv(&stack_a, argv_simulation); + ft_lst_map_actual_position(&stack_a); + ft_lst_map_final_pos(&stack_a, expected_size_before_push); actual_size_before_push = ft_lst_size(stack_a); actual_top_a_before_push = ft_lst_goto_head(stack_a)->nbr; actual_bottom_a_before_push = ft_lst_goto_end(stack_a)->nbr; - ft_lst_map_position(&stack_a); - ft_lst_map_weight(&stack_a, expected_size_before_push); ft_push_b_all_but_three(&stack_a, &stack_b); ft_sort_three_nbrs(&stack_a); @@ -345,13 +282,13 @@ MU_TEST(test_1st_miastep_push_b_all_but3) expected_bottom_a_before_push = ft_atoi(argv_simulation[6]); stack_a = ft_lts_buildstack_argv(&stack_a, argv_simulation); + ft_lst_map_actual_position(&stack_a); + ft_lst_map_final_pos(&stack_a, expected_size_before_push); actual_size_before_push = ft_lst_size(stack_a); actual_top_a_before_push = ft_lst_goto_head(stack_a)->nbr; actual_bottom_a_before_push = ft_lst_goto_end(stack_a)->nbr; - ft_lst_map_position(&stack_a); - ft_lst_map_weight(&stack_a, expected_size_before_push); ft_push_b_all_but_three(&stack_a, &stack_b); expected_top_a_after_push = ft_atoi(argv_simulation[4]); @@ -375,7 +312,7 @@ MU_TEST(test_1st_miastep_push_b_all_but3) ft_array_clear(argv_simulation, expected_size_before_push); } -MU_TEST(test_1st_miastep_weight_index) +MU_TEST(test_1st_miastep_final_pos_index) { // ARRANGE t_stack *stack; @@ -387,8 +324,8 @@ MU_TEST(test_1st_miastep_weight_index) int actual_top_a; int expected_bottom_a; int actual_bottom_a; - int expected_highest_weight; - int actual_highest_weight; + int expected_highest_final_pos; + int actual_highest_final_pos; // ACT stack = NULL; @@ -397,25 +334,25 @@ MU_TEST(test_1st_miastep_weight_index) expected_size = ft_minunit_array_counter(argv_simulation); expected_top_a = ft_atoi(argv_simulation[1]); expected_bottom_a = ft_atoi(argv_simulation[8]); - expected_highest_weight = expected_size - 1; + expected_highest_final_pos = expected_size - 1; stack = ft_lts_buildstack_argv(&stack, argv_simulation); + ft_lst_map_actual_position(&stack); + ft_lst_map_final_pos(&stack, expected_size); actual_size = ft_lst_size(stack); - ft_lst_map_position(&stack); - ft_lst_map_weight(&stack, expected_size); actual_top_a = ft_lst_goto_head(stack)->nbr; - while (stack->pos <= 2) + while (stack->origin <= 2) { stack = stack->next; - if (stack->pos == 2) - actual_highest_weight = stack->weight; + if (stack->origin == 2) + actual_highest_final_pos = stack->final_pos; } actual_bottom_a = ft_lst_goto_end(stack)->nbr; // ASSERT - mu_assert_int_eq(expected_highest_weight, actual_highest_weight); + mu_assert_int_eq(expected_highest_final_pos, actual_highest_final_pos); mu_assert_int_eq(expected_size, actual_size); mu_assert_int_eq(expected_top_a, actual_top_a); mu_assert_int_eq(expected_bottom_a, actual_bottom_a); @@ -448,14 +385,14 @@ MU_TEST(test_1st_miastep_pos_index) expected_fith_position = ft_atoi(argv_simulation[5]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); + ft_lst_map_actual_position(&stack); actual_size = ft_lst_size(stack); - ft_lst_map_position(&stack); actual_top_a = ft_lst_goto_head(stack)->nbr; - while (stack->pos <= 5) + while (stack->origin <= 5) { stack = stack->next; - if (stack->pos == 5) + if (stack->origin == 5) actual_fith_position = stack->nbr; } actual_bottom_a = ft_lst_goto_end(stack)->nbr; @@ -489,6 +426,7 @@ MU_TEST(test_sort_3_nbrs_already_sorted) expected_sorted_top = ft_atoi(argv_simulation[1]); expected_sorted_bottom = ft_atoi(argv_simulation[3]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); + ft_lst_map_all_indexers(&stack); stack = ft_sort_three_nbrs(&stack); actual_size = ft_lst_size(stack); actual_sorted_top = ft_lst_goto_head(stack)->nbr; @@ -523,6 +461,7 @@ MU_TEST(test_sort_3_nbrs_highest_last) expected_sorted_top = ft_atoi(argv_simulation[2]); expected_sorted_bottom = ft_atoi(argv_simulation[3]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); + ft_lst_map_all_indexers(&stack); stack = ft_sort_three_nbrs(&stack); actual_size = ft_lst_size(stack); actual_sorted_top = ft_lst_goto_head(stack)->nbr; @@ -557,6 +496,7 @@ MU_TEST(test_sort_3_nbrs_highest_first_v2) expected_sorted_top = ft_atoi(argv_simulation[3]); expected_sorted_bottom = ft_atoi(argv_simulation[1]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); + ft_lst_map_all_indexers(&stack); stack = ft_sort_three_nbrs(&stack); actual_size = ft_lst_size(stack); actual_sorted_top = ft_lst_goto_head(stack)->nbr; @@ -591,6 +531,7 @@ MU_TEST(test_sort_3_nbrs_highest_first) expected_sorted_top = ft_atoi(argv_simulation[2]); expected_sorted_bottom = ft_atoi(argv_simulation[1]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); + ft_lst_map_all_indexers(&stack); stack = ft_sort_three_nbrs(&stack); actual_size = ft_lst_size(stack); actual_sorted_top = ft_lst_goto_head(stack)->nbr; @@ -625,10 +566,12 @@ MU_TEST(test_sort_3_nbrs_highest_at_middle_v2) expected_sorted_top = ft_atoi(argv_simulation[1]); expected_sorted_bottom = ft_atoi(argv_simulation[2]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); + ft_lst_map_all_indexers(&stack); stack = ft_sort_three_nbrs(&stack); actual_size = ft_lst_size(stack); - actual_sorted_top = ft_lst_goto_head(stack)->nbr; actual_sorted_bottom = ft_lst_goto_end(stack)->nbr; + stack = ft_lst_goto_head(stack); + actual_sorted_top = (stack)->nbr; // ASSERT mu_assert_int_eq(expected_size, actual_size); @@ -659,6 +602,7 @@ MU_TEST(test_sort_3_nbrs_highest_at_middle) expected_sorted_top = ft_atoi(argv_simulation[3]); expected_sorted_bottom = ft_atoi(argv_simulation[2]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); + ft_lst_map_all_indexers(&stack); stack = ft_sort_three_nbrs(&stack); actual_size = ft_lst_size(stack); actual_sorted_top = ft_lst_goto_head(stack)->nbr; @@ -1555,13 +1499,12 @@ MU_TEST_SUITE(sorting_tests) MU_TEST_SUITE(miacombeau_tests) { MU_RUN_TEST(test_1st_miastep_pos_index); - MU_RUN_TEST(test_1st_miastep_weight_index); + MU_RUN_TEST(test_1st_miastep_final_pos_index); MU_RUN_TEST(test_1st_miastep_push_b_all_but3); MU_RUN_TEST(test_2nd_miastep_push_b_all_but3_sort_3a); - MU_RUN_TEST(test_3rd_miastep_map_lowest_weight); + MU_RUN_TEST(test_3rd_miastep_map_lowest_final_pos); MU_RUN_TEST(test_3rd_miastep_get_cost); - MU_RUN_TEST(test_3rd_miastep_map_get_target); - // MU_RUN_TEST(test_3rd_miastep_map_get_target_position); + MU_RUN_TEST(test_3rd_miastep_map_get_target_position); } int main(void) diff --git a/program_to_test/headers/push_swap.h b/program_to_test/headers/push_swap.h index ad07682..027a5f3 100644 --- a/program_to_test/headers/push_swap.h +++ b/program_to_test/headers/push_swap.h @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/08 15:23:33 by umeneses #+# #+# */ -/* Updated: 2024/06/14 17:49:46 by umeneses ### ########.fr */ +/* Updated: 2024/06/18 16:38:14 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,8 +20,8 @@ typedef struct s_stack { int nbr; - int pos; - int weight; + int origin; + int final_pos; int target; int cost_a; int cost_b; @@ -39,7 +39,7 @@ t_stack *ft_lst_delat_begin(t_stack *stack); t_stack *ft_lst_delat_end(t_stack *stack); void ft_lstclear_double_ptr(t_stack **stack); void ft_lstclear_single_ptr(t_stack *stack); -void ft_lst_printf_int_content(char *msg, t_stack *stack); +void ft_lst_printf_data_content(char *msg, t_stack *stack); t_stack *ft_lts_buildstack_argv(t_stack **stack, char **argv); bool ft_is_sign(int c); bool ft_is_space(int c); @@ -62,6 +62,7 @@ void ft_reverse_rotate_ab(t_stack **stack_a, t_stack **stack_b); bool ft_is_sorted(t_stack *stack); /* sorting validation functions */ +int ft_lst_map_highest_pos(t_stack *list); void ft_do_sort(t_stack **stack_a, t_stack **stack_b); t_stack *ft_sort_two_nbrs(t_stack **stack); t_stack *ft_sort_three_nbrs(t_stack **stack); @@ -69,13 +70,13 @@ t_stack *ft_sort_three_nbrs(t_stack **stack); void ft_sort_four_or_more_nbrs(t_stack **stack_a, t_stack **stack_b); void ft_push_b_all_but_three(t_stack **stack_a, t_stack **stack_b); -int ft_lst_get_target(t_stack **stack_a, int b_weight, - int target_weight, int target_pos); +int ft_lst_get_target(t_stack **stack_a, int b_end_pos, + int target_end_pos, int target_pos); void ft_lst_get_target_position(t_stack **stack_a, t_stack **stack_b); void ft_lst_map_all_indexers(t_stack **stack); -void ft_lst_map_position(t_stack **stack); -void ft_lst_map_weight(t_stack **stack, int stack_size); -int ft_lst_map_lowest_weight_position(t_stack **stack); +void ft_lst_map_actual_position(t_stack **stack); +void ft_lst_map_final_pos(t_stack **stack, int stack_size); +int ft_lst_map_lowest_final_position(t_stack **stack); void ft_lst_shift_stack(t_stack **stack_a); void ft_lst_get_cost(t_stack **stack_a, t_stack **stack_b); void ft_lst_do_cheapest_move(t_stack **stack_a, t_stack **stack_b); diff --git a/program_to_test/src/ft_do_sort.c b/program_to_test/src/ft_do_sort.c index 549b605..84d5b5b 100644 --- a/program_to_test/src/ft_do_sort.c +++ b/program_to_test/src/ft_do_sort.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/06 18:46:55 by umeneses #+# #+# */ -/* Updated: 2024/06/11 17:53:09 by umeneses ### ########.fr */ +/* Updated: 2024/06/18 14:03:19 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,20 @@ void ft_do_sort(t_stack **stack_a, t_stack **stack_b) else if (ft_lst_size(*stack_a) == 3) ft_sort_three_nbrs(stack_a); else + { ft_sort_four_or_more_nbrs(stack_a, stack_b); + } } } + +void ft_do_move_after_cheapest(t_stack **stack_a, t_stack **stack_b, + int cost_a, int cost_b) +{ + if (cost_a < 0 && cost_b < 0) + ft_do_reverse_rotate_ab(stack_a, stack_b); + else if (cost_a > 0 && cost_b > 0) + ft_do_rotate_ab(stack_a, stack_b); + ft_do_rotate_a(stack_a); + ft_do_rotate_b(stack_b); + ft_do_push_a(stack_a, stack_b); +} diff --git a/program_to_test/src/ft_lst_goto.c b/program_to_test/src/ft_lst_goto.c index a88e880..4d22959 100644 --- a/program_to_test/src/ft_lst_goto.c +++ b/program_to_test/src/ft_lst_goto.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/13 14:49:58 by umeneses #+# #+# */ -/* Updated: 2024/06/08 17:26:58 by umeneses ### ########.fr */ +/* Updated: 2024/06/18 15:01:57 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,5 +36,7 @@ t_stack *ft_lst_goto_head(t_stack *list) return (NULL); while (list->prev) list = list->prev; + if (list->prev == NULL) + return (list); return (list); } diff --git a/program_to_test/src/ft_lst_init.c b/program_to_test/src/ft_lst_init.c index b33d2a1..1484d75 100644 --- a/program_to_test/src/ft_lst_init.c +++ b/program_to_test/src/ft_lst_init.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/13 14:49:58 by umeneses #+# #+# */ -/* Updated: 2024/06/14 17:56:49 by umeneses ### ########.fr */ +/* Updated: 2024/06/18 16:35:21 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -56,7 +56,7 @@ t_stack *ft_lts_buildstack_argv(t_stack **stack, char **argv) return (*stack); } -void ft_lst_printf_int_content(char *msg, t_stack *list) +void ft_lst_printf_data_content(char *msg, t_stack *list) { t_stack *temp; @@ -64,9 +64,11 @@ void ft_lst_printf_int_content(char *msg, t_stack *list) temp = list; while (temp) { - ft_printf("%s\tnbr[%d]\tpos[%d]\t",msg, temp->nbr, temp->pos); - ft_printf("weight[%d]\ttarget[%d]\t", temp->weight, temp->target); + ft_printf("%s\tnbr[%d]\torigin[%d]\t", msg, temp->nbr, temp->origin); + ft_printf("final_pos[%d]\ttarget[%d]\t", temp->final_pos, temp->target); ft_printf("cost_a[%d]\tcost_b[%d]\n", temp->cost_a, temp->cost_b); temp = temp->next; } + if (list == NULL) + ft_printf("%s\tEmpty list\n", msg); } diff --git a/program_to_test/src/ft_lst_map_all.c b/program_to_test/src/ft_lst_map_all.c index b8d6b06..6bdde69 100644 --- a/program_to_test/src/ft_lst_map_all.c +++ b/program_to_test/src/ft_lst_map_all.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/13 18:41:22 by umeneses #+# #+# */ -/* Updated: 2024/06/13 18:41:32 by umeneses ### ########.fr */ +/* Updated: 2024/06/18 13:53:02 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,6 @@ void ft_lst_map_all_indexers(t_stack **stack) int stack_size; stack_size = ft_lst_size(*stack); - ft_lst_map_position(stack); - ft_lst_map_weight(stack, stack_size); + ft_lst_map_actual_position(stack); + ft_lst_map_final_pos(stack, stack_size); } diff --git a/program_to_test/src/ft_lst_map_indexers.c b/program_to_test/src/ft_lst_map_indexers.c index 08beadd..e8b34a6 100644 --- a/program_to_test/src/ft_lst_map_indexers.c +++ b/program_to_test/src/ft_lst_map_indexers.c @@ -6,49 +6,49 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/10 10:34:17 by umeneses #+# #+# */ -/* Updated: 2024/06/14 17:31:07 by umeneses ### ########.fr */ +/* Updated: 2024/06/18 16:37:30 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" -void ft_lst_map_position(t_stack **stack) +void ft_lst_map_actual_position(t_stack **stack) { - int pos; + int actual_position; - pos = 0; + actual_position = 0; while ((*stack)->next != NULL) { - pos++; - (*stack)->pos = pos; + actual_position++; + (*stack)->origin = actual_position; (*stack) = (*stack)->next; if ((*stack)->next == NULL) { - pos++; - (*stack)->pos = pos; + actual_position++; + (*stack)->origin = actual_position; } } *stack = ft_lst_goto_head(*stack); } -void ft_lst_map_weight(t_stack **stack, int stack_size) +void ft_lst_map_final_pos(t_stack **stack, int stack_size) { - int weight; + int final_pos; t_stack *target; t_stack *highest; while (--stack_size > 0) { target = *stack; - weight = INT_MIN; + final_pos = INT_MIN; highest = NULL; while (target) { - if (target->nbr == INT_MIN && target->weight == 0) - target->weight = 1; - if (target->nbr > weight && target->weight == 0) + if (target->nbr == INT_MIN && target->final_pos == 0) + target->final_pos = 1; + if (target->nbr > final_pos && target->final_pos == 0) { - weight = target->nbr; + final_pos = target->nbr; highest = target; target = *stack; } @@ -56,56 +56,56 @@ void ft_lst_map_weight(t_stack **stack, int stack_size) target = target->next; } if (highest != NULL) - highest->weight = stack_size; + highest->final_pos = stack_size; } } -int ft_lst_map_lowest_weight_position(t_stack **stack) +int ft_lst_map_lowest_final_position(t_stack **stack) { - int lowest_weight; + int lowest_final_pos; int lowest_position; t_stack *temp; temp = *stack; - lowest_weight = INT_MAX; - ft_lst_map_position(stack); - lowest_position = temp->pos; + lowest_final_pos = INT_MAX; + ft_lst_map_actual_position(stack); + lowest_position = temp->origin; while (temp) { - if (temp->weight < lowest_weight) + if (temp->final_pos < lowest_final_pos) { - lowest_weight = temp->weight; - lowest_position = temp->pos; + lowest_final_pos = temp->final_pos; + lowest_position = temp->origin; } temp = temp->next; } return (lowest_position); } -int ft_lst_get_target(t_stack **stack_a, int b_weight, - int target_weight, int target_pos) +int ft_lst_get_target(t_stack **stack_a, int b_end_pos, + int target_end_pos, int target_pos) { t_stack *temp_a; temp_a = *stack_a; while (temp_a) { - if (temp_a->weight > b_weight && temp_a->weight < target_weight) + if (temp_a->final_pos > b_end_pos && temp_a->final_pos < target_end_pos) { - target_weight = temp_a->weight; - target_pos = temp_a->pos; + target_end_pos = temp_a->final_pos; + target_pos = temp_a->origin; } temp_a = temp_a->next; } - if (target_weight != INT_MAX) + if (target_end_pos != INT_MAX) return (target_pos); - temp_a = *stack_a; + temp_a = ft_lst_goto_head(temp_a); while (temp_a) { - if (temp_a->weight < target_weight) + if (temp_a->final_pos < target_end_pos) { - target_weight = temp_a->weight; - target_pos = temp_a->pos; + target_end_pos = temp_a->final_pos; + target_pos = temp_a->origin; } temp_a = temp_a->next; } @@ -118,34 +118,14 @@ void ft_lst_get_target_position(t_stack **stack_a, t_stack **stack_b) t_stack *temp_b; temp_b = *stack_b; - ft_lst_map_position(stack_a); - ft_lst_map_position(stack_b); + ft_lst_map_actual_position(stack_a); + ft_lst_map_actual_position(stack_b); target_position = 0; while (temp_b) { - target_position = ft_lst_get_target(stack_a, temp_b->weight, + target_position = ft_lst_get_target(stack_a, temp_b->final_pos, INT_MAX, target_position); temp_b->target = target_position; temp_b = temp_b->next; } } - -// void ft_lst_map_weight_by_argv(t_stack **stack, char **argv) -// { -// int weight; -// int pos; -// char **argv_splited; -// t_stack *temp; - -// weight = 0; -// pos = 0; -// weight = 0; -// temp = *stack; -// argv_splited = ft_split(*argv, ' '); -// while ((argv_splited[pos] != NULL) && (temp->next != NULL)) -// { -// if (ft_atoi(argv_splited[pos]) > ft_atoi(argv_splited[pos + 1])) -// temp->weight = weight++; -// temp = temp->next; -// } -// } diff --git a/program_to_test/src/ft_push.c b/program_to_test/src/ft_push.c index 2b14099..df963ad 100644 --- a/program_to_test/src/ft_push.c +++ b/program_to_test/src/ft_push.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/22 11:52:34 by umeneses #+# #+# */ -/* Updated: 2024/06/14 17:35:05 by umeneses ### ########.fr */ +/* Updated: 2024/06/18 16:34:17 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ void ft_push_a(t_stack **stack_a, t_stack **stack_b) to_be_pushed = (*stack_b); (*stack_b) = (*stack_b)->next; if ((*stack_b)) - (*stack_b)->prev = NULL; + (*stack_b)->prev = NULL; to_be_pushed->next = NULL; to_be_pushed->prev = NULL; to_be_pushed->next = (*stack_a); @@ -37,7 +37,8 @@ void ft_push_b(t_stack **stack_a, t_stack **stack_b) return ; to_be_pushed = (*stack_a); (*stack_a) = (*stack_a)->next; - (*stack_a)->prev = NULL; + if ((*stack_a)) + (*stack_a)->prev = NULL; to_be_pushed->next = NULL; to_be_pushed->prev = NULL; to_be_pushed->next = (*stack_b); diff --git a/program_to_test/src/ft_sort_4_or_more.c b/program_to_test/src/ft_sort_4_or_more.c index 35bccf7..f2c4de7 100644 --- a/program_to_test/src/ft_sort_4_or_more.c +++ b/program_to_test/src/ft_sort_4_or_more.c @@ -6,38 +6,28 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/10 12:41:29 by umeneses #+# #+# */ -/* Updated: 2024/06/11 18:48:20 by umeneses ### ########.fr */ +/* Updated: 2024/06/18 15:24:11 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" -void ft_lst_map_all_indexers(t_stack **stack) -{ - int stack_size; - - stack_size = ft_lst_size(*stack); - ft_lst_map_position(stack); - ft_lst_map_weight(stack, stack_size); -} - -/* -* TODO after line 40: -* while (*stack_b) -* { -* ft_lst_get_target_position(stack_a, stack_b); -* ft_lst_get_cost(stack_a, stack_b); -* ft_lst_do_cheapest_move(stack_a, stack_b); -* } -* if (!ft_is_sorted(*stack_a)) -* { -* ft_lst_shift_stack(stack_a); -* } -*/ void ft_sort_four_or_more_nbrs(t_stack **stack_a, t_stack **stack_b) { ft_push_b_all_but_three(stack_a, stack_b); ft_sort_three_nbrs(stack_a); + ft_lst_printf_data_content("stack_a before get-target:", *stack_a); + ft_lst_printf_data_content("stack_b before get-target:", *stack_b); + while (*stack_b) + { + ft_lst_get_target_position(stack_a, stack_b); + ft_lst_get_cost(stack_a, stack_b); + ft_lst_do_cheapest_move(stack_a, stack_b); + } + if (!ft_is_sorted(*stack_a)) + ft_lst_shift_stack(stack_a); + ft_lst_printf_data_content("stack_a after get-target:", *stack_a); + ft_lst_printf_data_content("stack_b after get-target:", *stack_b); } void ft_push_b_all_but_three(t_stack **stack_a, t_stack **stack_b) @@ -52,7 +42,7 @@ void ft_push_b_all_but_three(t_stack **stack_a, t_stack **stack_b) while ((stack_size > 6) && (index < stack_size) && (pushed < stack_size / 2)) { - if ((*stack_a)->weight <= (stack_size / 2)) + if ((*stack_a)->final_pos <= (stack_size / 2)) { ft_do_push_b(stack_a, stack_b); pushed++; @@ -68,3 +58,73 @@ void ft_push_b_all_but_three(t_stack **stack_a, t_stack **stack_b) } *stack_b = ft_lst_delat_end(*stack_b); } + +void ft_lst_get_cost(t_stack **stack_a, t_stack **stack_b) +{ + int size_a; + int size_b; + t_stack *temp_a; + t_stack *temp_b; + + temp_a = *stack_a; + temp_b = *stack_b; + size_a = ft_lst_size(temp_a); + size_b = ft_lst_size(temp_b); + while (temp_b) + { + temp_b->cost_b = temp_b->origin; + if (temp_b->origin > size_b / 2) + temp_b->cost_b = (size_b - temp_b->origin) * -1; + temp_b->cost_a = temp_b->target; + if (temp_b->target > size_a / 2) + temp_b->cost_a = (size_a - temp_b->target) * -1; + temp_b = temp_b->next; + } +} + +void ft_lst_do_cheapest_move(t_stack **stack_a, t_stack **stack_b) +{ + int cost_a; + int cost_b; + int cheapest; + t_stack *temp_b; + + temp_b = *stack_b; + cheapest = INT_MAX; + while (temp_b) + { + if ((temp_b->cost_a + temp_b->cost_b) < cheapest) + { + cheapest = temp_b->cost_b + temp_b->cost_a; + cost_a = temp_b->cost_a; + cost_b = temp_b->cost_b; + } + temp_b = temp_b->next; + } + ft_do_move_after_cheapest(stack_a, stack_b, cost_a, cost_b); +} + +void ft_lst_shift_stack(t_stack **stack_a) +{ + int lowest_final_pos; + int stack_size; + + stack_size = ft_lst_size(*stack_a); + lowest_final_pos = ft_lst_map_lowest_final_position(stack_a); + if (lowest_final_pos < stack_size / 2) + { + while (lowest_final_pos < stack_size) + { + ft_do_reverse_rotate_a(stack_a); + lowest_final_pos++; + } + } + else + { + while (lowest_final_pos > 0) + { + ft_do_rotate_a(stack_a); + lowest_final_pos--; + } + } +} diff --git a/program_to_test/src/ft_sort_until3.c b/program_to_test/src/ft_sort_until3.c index f2c6172..d50d3a9 100644 --- a/program_to_test/src/ft_sort_until3.c +++ b/program_to_test/src/ft_sort_until3.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 12:19:14 by umeneses #+# #+# */ -/* Updated: 2024/06/11 15:20:23 by umeneses ### ########.fr */ +/* Updated: 2024/06/18 16:10:51 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,26 +37,34 @@ t_stack *ft_sort_two_nbrs(t_stack **list) t_stack *ft_sort_three_nbrs(t_stack **list) { - while (!ft_is_sorted(*list)) + int highest_pos; + + *list = ft_lst_goto_head(*list); + if (ft_is_sorted(*list)) + return (*list); + highest_pos = ft_lst_map_highest_pos(*list); + if ((*list)->final_pos == highest_pos) + ft_do_rotate_a(list); + else if ((*list)->next->final_pos == highest_pos) + ft_do_reverse_rotate_a(list); + if ((*list)->final_pos > (*list)->next->final_pos) + ft_do_swap_a(list); + return (*list); +} + +int ft_lst_map_highest_pos(t_stack *list) +{ + int highest_pos; + t_stack *temp; + + temp = list; + temp = ft_lst_goto_head(temp); + highest_pos = list->final_pos; + while (temp) { - if (((*list)->nbr < (*list)->next->nbr) - && ((*list)->nbr < ft_lst_goto_end(*list)->nbr) - && ((*list)->next->nbr > ft_lst_goto_end(*list)->nbr)) - ft_do_reverse_rotate_a(list); - else if (((*list)->nbr < (*list)->next->nbr) - && ((*list)->next->nbr > ft_lst_goto_end(*list)->nbr) - && ((*list)->nbr > ft_lst_goto_end(*list)->nbr)) - ft_do_reverse_rotate_a(list); - else if (((*list)->nbr > (*list)->next->nbr) - && ((*list)->nbr > ft_lst_goto_end(*list)->nbr) - && ((*list)->next->nbr < ft_lst_goto_end(*list)->nbr)) - ft_do_rotate_a(list); - else if (((*list)->nbr > ft_lst_goto_end(*list)->nbr) - && ((*list)->nbr > (*list)->next->nbr) - && ((*list)->next->nbr > ft_lst_goto_end(*list)->nbr)) - ft_do_rotate_a(list); - else - ft_do_swap_a(list); + if (temp->final_pos > highest_pos) + highest_pos = temp->final_pos; + temp = temp->next; } - return (*list); + return (highest_pos); } diff --git a/program_to_test/src/push_main.c b/program_to_test/src/push_main.c index d2f27f0..181ba3f 100644 --- a/program_to_test/src/push_main.c +++ b/program_to_test/src/push_main.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 18:34:31 by umeneses #+# #+# */ -/* Updated: 2024/06/14 17:48:20 by umeneses ### ########.fr */ +/* Updated: 2024/06/18 15:40:33 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,13 +20,13 @@ int main(int argc, char **argv) if (argc <= 2) ft_error_msg("Not enough arguments. Please provide at leats 2 numbers"); stack_a = NULL; - stack_b = (t_stack *)ft_calloc(1, sizeof(t_stack)); + stack_b = NULL; stack_a = ft_lts_buildstack_argv(&stack_a, argv); - ft_lst_printf_int_content("original main:", stack_a); ft_lst_map_all_indexers(&stack_a); + ft_lst_printf_data_content("original main stack_a:", stack_a); ft_do_sort(&stack_a, &stack_b); - ft_lst_printf_int_content("updated main stack_a:", stack_a); - ft_lst_printf_int_content("updated main stack_b:", stack_b); + ft_lst_printf_data_content("updated main stack_a:", stack_a); + ft_lst_printf_data_content("updated main stack_b:", stack_b); ft_lstclear_single_ptr(stack_a); ft_lstclear_single_ptr(stack_b); return (0);