From fb6eefc052b12f07f2954c18c3ab62c6cf1a8fc2 Mon Sep 17 00:00:00 2001 From: umeneses Date: Thu, 20 Jun 2024 12:37:32 -0300 Subject: [PATCH 01/17] improving method --- program_to_test/src/ft_sort_until3.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/program_to_test/src/ft_sort_until3.c b/program_to_test/src/ft_sort_until3.c index 47fe819..6a5f11c 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/19 14:38:08 by umeneses ### ########.fr */ +/* Updated: 2024/06/20 12:31:18 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,15 +16,14 @@ bool ft_is_sorted(t_stack *list) { t_stack *temp; - while (list->next != NULL) + temp = list; + while (temp->next != NULL) { - temp = list->next; - if (list->nbr > temp->nbr) + if (temp->nbr > temp->next->nbr) return (false); - list = list->next; + temp = temp->next; } - ft_lst_goto_head(list); - ft_printf(">>>>>>>>>>>>> list is sorted!\n"); + // ft_printf(">>>>>>>>>>>>> list is sorted!\n"); return (true); } From e2416ef7485c2dcf906ad86b85c935e90f150351 Mon Sep 17 00:00:00 2001 From: umeneses Date: Thu, 20 Jun 2024 12:38:28 -0300 Subject: [PATCH 02/17] protecting if list_size is less than 2 --- program_to_test/src/ft_rotate.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/program_to_test/src/ft_rotate.c b/program_to_test/src/ft_rotate.c index f2c0d08..2ee3c9d 100644 --- a/program_to_test/src/ft_rotate.c +++ b/program_to_test/src/ft_rotate.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/23 11:48:11 by umeneses #+# #+# */ -/* Updated: 2024/06/19 13:45:59 by umeneses ### ########.fr */ +/* Updated: 2024/06/20 12:15:49 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ void ft_rotate(t_stack **stack) { t_stack *last_node; - if (NULL == *stack || NULL== stack) + if (NULL == *stack || NULL== stack || ft_lst_size(*stack) <= 2) return ; last_node = ft_lst_goto_end(*stack); last_node->next = *stack; @@ -42,7 +42,7 @@ void ft_reverse_rotate(t_stack **stack) { t_stack *last_node; - if (NULL == *stack || NULL == (*stack)->next || ft_lst_size(*stack) < 2) + if (NULL == *stack || NULL == (*stack)->next || ft_lst_size(*stack) <= 2) return ; last_node = ft_lst_goto_end(*stack); last_node->prev->next = NULL; From e1bd60bcbc51f42d69bb0485d6551b8636de55f3 Mon Sep 17 00:00:00 2001 From: umeneses Date: Fri, 21 Jun 2024 18:42:58 -0300 Subject: [PATCH 03/17] printing stack content to check, before every moviment --- program_to_test/src/ft_do_push_fts.c | 5 +++- program_to_test/src/ft_do_rev_rotate_fts.c | 9 ++++++- program_to_test/src/ft_do_rotate_fts.c | 7 +++++- program_to_test/src/ft_do_sort.c | 28 +++++++--------------- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/program_to_test/src/ft_do_push_fts.c b/program_to_test/src/ft_do_push_fts.c index d59923e..1c3b113 100644 --- a/program_to_test/src/ft_do_push_fts.c +++ b/program_to_test/src/ft_do_push_fts.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 12:24:04 by umeneses #+# #+# */ -/* Updated: 2024/06/10 14:57:19 by umeneses ### ########.fr */ +/* Updated: 2024/06/21 11:30:11 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,9 @@ void ft_do_push_a(t_stack **stack_a, t_stack **stack_b) { + ft_lst_printf_data_content("after mov stack_a:", *stack_a); + ft_printf("\n"); + ft_lst_printf_data_content("after mov stack_b:", *stack_b); ft_push_a(stack_a, stack_b); ft_putstr_fd("pa\n", 1); } diff --git a/program_to_test/src/ft_do_rev_rotate_fts.c b/program_to_test/src/ft_do_rev_rotate_fts.c index ea418cf..1bd84e6 100644 --- a/program_to_test/src/ft_do_rev_rotate_fts.c +++ b/program_to_test/src/ft_do_rev_rotate_fts.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 12:24:04 by umeneses #+# #+# */ -/* Updated: 2024/06/05 11:36:16 by umeneses ### ########.fr */ +/* Updated: 2024/06/21 11:31:32 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,18 +14,25 @@ void ft_do_reverse_rotate_a(t_stack **stack) { + ft_lst_printf_data_content("after mov stack_a:", *stack); + ft_printf("\n"); ft_reverse_rotate(stack); ft_putstr_fd("rra\n", 1); } void ft_do_reverse_rotate_b(t_stack **stack) { + ft_lst_printf_data_content("after mov stack_b:", *stack); + ft_printf("\n"); ft_reverse_rotate(stack); ft_putstr_fd("rrb\n", 1); } void ft_do_reverse_rotate_ab(t_stack **stack_a, t_stack **stack_b) { + ft_lst_printf_data_content("after mov stack_a:", *stack_a); + ft_printf("\n"); + ft_lst_printf_data_content("after mov stack_b:", *stack_b); ft_reverse_rotate_ab(stack_a, stack_b); ft_putstr_fd("rrr\n", 1); } diff --git a/program_to_test/src/ft_do_rotate_fts.c b/program_to_test/src/ft_do_rotate_fts.c index d5e3a8b..e01de14 100644 --- a/program_to_test/src/ft_do_rotate_fts.c +++ b/program_to_test/src/ft_do_rotate_fts.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 12:24:04 by umeneses #+# #+# */ -/* Updated: 2024/06/05 11:50:14 by umeneses ### ########.fr */ +/* Updated: 2024/06/21 11:30:55 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,18 +14,23 @@ void ft_do_rotate_a(t_stack **stack) { + ft_lst_printf_data_content("after mov stack_a:", *stack); ft_rotate(stack); ft_putstr_fd("ra\n", 1); } void ft_do_rotate_b(t_stack **stack) { + ft_lst_printf_data_content("after mov stack_b:", *stack); ft_rotate(stack); ft_putstr_fd("rb\n", 1); } void ft_do_rotate_ab(t_stack **stack_a, t_stack **stack_b) { + ft_lst_printf_data_content("after mov stack_a:", *stack_a); + ft_printf("\n"); + ft_lst_printf_data_content("after mov stack_b:", *stack_b); ft_rotate_ab(stack_a, stack_b); ft_putstr_fd("rr\n", 1); } diff --git a/program_to_test/src/ft_do_sort.c b/program_to_test/src/ft_do_sort.c index 546a387..aa4c24f 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/19 15:07:03 by umeneses ### ########.fr */ +/* Updated: 2024/06/21 11:54:29 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,27 +23,17 @@ void ft_do_sort(t_stack **stack_a, t_stack **stack_b) ft_sort_two_nbrs(stack_a); else if (stack_size == 3) ft_sort_three_nbrs(stack_a); - else if (stack_size == 4) - ft_sort_four_nbrs(stack_a, stack_b); - else if (stack_size >= 5) - { - ft_sort_five_or_more_nbr(stack_a, stack_b); - } - // else + // else if (stack_size == 4) + // ft_sort_four_nbrs(stack_a, stack_b); + // else if (stack_size >= 5) // { // ft_sort_five_or_more_nbr(stack_a, stack_b); // } + else + { + ft_sort_five_or_more_nbr(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); -} + From c190edc1300db5a96203d82064be810213b4c56e Mon Sep 17 00:00:00 2001 From: umeneses Date: Fri, 21 Jun 2024 18:43:28 -0300 Subject: [PATCH 04/17] fixing indexer --- program_to_test/src/ft_lst_map_indexers.c | 40 ++++++++++++----------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/program_to_test/src/ft_lst_map_indexers.c b/program_to_test/src/ft_lst_map_indexers.c index 4d19562..5959177 100644 --- a/program_to_test/src/ft_lst_map_indexers.c +++ b/program_to_test/src/ft_lst_map_indexers.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/10 10:34:17 by umeneses #+# #+# */ -/* Updated: 2024/06/19 11:51:52 by umeneses ### ########.fr */ +/* Updated: 2024/06/21 11:46:06 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,16 +17,17 @@ void ft_lst_map_actual_position(t_stack **stack) int actual_position; actual_position = 0; + *stack = ft_lst_goto_head(*stack); while ((*stack)->next != NULL) { - actual_position++; (*stack)->origin = actual_position; (*stack) = (*stack)->next; - if ((*stack)->next == NULL) - { - actual_position++; - (*stack)->origin = actual_position; - } + // if ((*stack)->next == NULL) + // { + // actual_position++; + // (*stack)->origin = actual_position; + // } + actual_position++; } *stack = ft_lst_goto_head(*stack); } @@ -34,26 +35,27 @@ void ft_lst_map_actual_position(t_stack **stack) void ft_lst_map_final_pos(t_stack **stack, int stack_size) { int final_pos; - t_stack *target; + t_stack *temp; t_stack *highest; + stack_size++; while (--stack_size > 0) { - target = *stack; + temp = *stack; final_pos = INT_MIN; highest = NULL; - while (target) + while (temp) { - if (target->nbr == INT_MIN && target->final_pos == 0) - target->final_pos = 1; - if (target->nbr > final_pos && target->final_pos == 0) + if (temp->nbr == INT_MIN && temp->final_pos == 0) + temp->final_pos = 1; + if (temp->nbr > final_pos && temp->final_pos == 0) { - final_pos = target->nbr; - highest = target; - target = *stack; + final_pos = temp->nbr; + highest = temp; + temp = *stack; } else - target = target->next; + temp = temp->next; } if (highest != NULL) highest->final_pos = stack_size; @@ -65,7 +67,7 @@ int ft_lst_get_target(t_stack **stack_a, int b_end_pos, { t_stack *temp_a; - temp_a = *stack_a; + temp_a = ft_lst_goto_head(*stack_a); while (temp_a) { if (temp_a->final_pos > b_end_pos && temp_a->final_pos < target_end_pos) @@ -77,7 +79,7 @@ int ft_lst_get_target(t_stack **stack_a, int b_end_pos, } if (target_end_pos != INT_MAX) return (target_pos); - temp_a = ft_lst_goto_head(temp_a); + temp_a = ft_lst_goto_head(*stack_a); while (temp_a) { if (temp_a->final_pos < target_end_pos) From 975f17bb169239cd8e9d659eecd411c6e1a45291 Mon Sep 17 00:00:00 2001 From: umeneses Date: Fri, 21 Jun 2024 18:43:56 -0300 Subject: [PATCH 05/17] fixing NULL pointer --- program_to_test/src/ft_push.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/program_to_test/src/ft_push.c b/program_to_test/src/ft_push.c index da65341..cf34850 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/19 13:52:30 by umeneses ### ########.fr */ +/* Updated: 2024/06/20 11:35:31 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,8 @@ void ft_push_a(t_stack **stack_a, t_stack **stack_b) if (NULL == *stack_a) { *stack_a = to_be_pushed; - to_be_pushed->next = NULL; + (*stack_a)->next = NULL; + (*stack_a)->prev = NULL; } else { @@ -33,6 +34,7 @@ void ft_push_a(t_stack **stack_a, t_stack **stack_b) to_be_pushed->next->prev = to_be_pushed; *stack_a = to_be_pushed; } + (*stack_a) = ft_lst_goto_head(*stack_a); } void ft_push_b(t_stack **stack_a, t_stack **stack_b) @@ -41,7 +43,7 @@ void ft_push_b(t_stack **stack_a, t_stack **stack_b) if (NULL == *stack_a) return ; - to_be_pushed = (*stack_a); + to_be_pushed = (*stack_a); (*stack_a) = (*stack_a)->next; if ((*stack_a)) (*stack_a)->prev = NULL; From a8ce7deef7643d38173af69efc09aa84773b5f55 Mon Sep 17 00:00:00 2001 From: umeneses Date: Fri, 21 Jun 2024 18:44:59 -0300 Subject: [PATCH 06/17] updating logic, after original_pos starts with 1 --- program_to_test/src/ft_sort_until4.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/program_to_test/src/ft_sort_until4.c b/program_to_test/src/ft_sort_until4.c index 0b29d19..427eece 100644 --- a/program_to_test/src/ft_sort_until4.c +++ b/program_to_test/src/ft_sort_until4.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_sort_until5.c :+: :+: :+: */ +/* ft_sort_until4.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/10 12:41:29 by umeneses #+# #+# */ -/* Updated: 2024/06/19 14:56:16 by umeneses ### ########.fr */ +/* Updated: 2024/06/21 11:43:43 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,18 +14,18 @@ void ft_sort_four_nbrs(t_stack **stack_a, t_stack **stack_b) { - int highest_pos; + int highest_pos_b; ft_push_b_all_but_three(stack_a, stack_b); ft_sort_three_nbrs(stack_a); - highest_pos = ft_lst_map_highest_pos(*stack_b); - if ((*stack_b)->final_pos == highest_pos) + highest_pos_b = ft_lst_map_highest_pos(*stack_b); + if ((*stack_b)->final_pos == highest_pos_b) { ft_do_push_a(stack_a, stack_b); ft_do_rotate_a(stack_a); } - if (((*stack_a)->final_pos == 0) && ((*stack_a)->next->final_pos == 2) - && ((*stack_a)->next->next->final_pos == 3)) + if (((*stack_a)->final_pos == 1) && ((*stack_a)->next->final_pos == 3) + && ((*stack_a)->next->next->final_pos == 4)) { ft_do_reverse_rotate_a(stack_a); ft_do_swap_a(stack_a); @@ -69,7 +69,7 @@ void ft_lst_shift_stack(t_stack **stack_a) 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) + if (lowest_final_pos > stack_size / 2) { while (lowest_final_pos < stack_size) { @@ -77,8 +77,8 @@ void ft_lst_shift_stack(t_stack **stack_a) lowest_final_pos++; } } - if (ft_lst_goto_end(*stack_a)->final_pos == 0) - ft_do_reverse_rotate_a(stack_a); + // if (ft_lst_goto_end(*stack_a)->final_pos == 1) + // ft_do_reverse_rotate_a(stack_a); else { while (lowest_final_pos > 0) @@ -95,9 +95,9 @@ int ft_lst_map_lowest_final_position(t_stack **stack) int lowest_position; t_stack *temp; - temp = *stack; - lowest_final_pos = INT_MAX; + temp = ft_lst_goto_head(*stack); ft_lst_map_actual_position(stack); + lowest_final_pos = INT_MAX; lowest_position = temp->origin; while (temp) { From 87af967591d99713b118f15cd5de0b993261d026 Mon Sep 17 00:00:00 2001 From: umeneses Date: Fri, 21 Jun 2024 18:46:06 -0300 Subject: [PATCH 07/17] calling actual position, inside functions --- program_to_test/src/ft_sort_5_or_more.c | 38 +++++++++++++++++++++---- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/program_to_test/src/ft_sort_5_or_more.c b/program_to_test/src/ft_sort_5_or_more.c index ca4ac92..113d61d 100644 --- a/program_to_test/src/ft_sort_5_or_more.c +++ b/program_to_test/src/ft_sort_5_or_more.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/10 12:41:29 by umeneses #+# #+# */ -/* Updated: 2024/06/19 15:06:57 by umeneses ### ########.fr */ +/* Updated: 2024/06/21 11:54:43 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,8 @@ void ft_sort_five_or_more_nbr(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_map_actual_position(stack_a); + ft_lst_map_actual_position(stack_b); while (*stack_b) { ft_lst_get_target_position(stack_a, stack_b); @@ -33,10 +35,12 @@ void ft_lst_get_cost(t_stack **stack_a, t_stack **stack_b) t_stack *temp_a; t_stack *temp_b; - temp_a = *stack_a; - temp_b = *stack_b; + temp_a = ft_lst_goto_head(*stack_a); + temp_b = ft_lst_goto_head(*stack_b); size_a = ft_lst_size(temp_a); size_b = ft_lst_size(temp_b); + ft_lst_map_actual_position(stack_a); + ft_lst_map_actual_position(stack_b); while (temp_b) { temp_b->cost_b = temp_b->origin; @@ -56,13 +60,16 @@ void ft_lst_do_cheapest_move(t_stack **stack_a, t_stack **stack_b) int cheapest; t_stack *temp_b; - temp_b = *stack_b; cheapest = INT_MAX; + ft_lst_map_actual_position(stack_a); + ft_lst_map_actual_position(stack_b); + temp_b = ft_lst_goto_head(*stack_b); while (temp_b) { - if ((temp_b->cost_a + temp_b->cost_b) < cheapest) + if ((abs_nbr(temp_b->cost_a) + abs_nbr(temp_b->cost_b)) + < abs_nbr(cheapest)) { - cheapest = temp_b->cost_b + temp_b->cost_a; + cheapest = abs_nbr(temp_b->cost_b) + abs_nbr(temp_b->cost_a); cost_a = temp_b->cost_a; cost_b = temp_b->cost_b; } @@ -70,3 +77,22 @@ void ft_lst_do_cheapest_move(t_stack **stack_a, t_stack **stack_b) } ft_do_move_after_cheapest(stack_a, stack_b, cost_a, cost_b); } + +int abs_nbr(int nbr) +{ + if (nbr < 0) + return (nbr * -1); + return (nbr); +} + +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); +} From 391c3ac5871cd79a505142c6dbf7675e9420698e Mon Sep 17 00:00:00 2001 From: umeneses Date: Fri, 21 Jun 2024 18:46:38 -0300 Subject: [PATCH 08/17] swap, with new method --- program_to_test/src/ft_swap.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/program_to_test/src/ft_swap.c b/program_to_test/src/ft_swap.c index 7332814..413eff8 100644 --- a/program_to_test/src/ft_swap.c +++ b/program_to_test/src/ft_swap.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/13 14:49:58 by umeneses #+# #+# */ -/* Updated: 2024/06/19 17:09:51 by umeneses ### ########.fr */ +/* Updated: 2024/06/20 12:30:31 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,14 +14,6 @@ void ft_swap(t_stack **stack) { - // int temp; - - // if (NULL == stack || NULL == (*stack)) - // return ; - // temp = (*stack)->nbr; - // (*stack)->nbr = (*stack)->next->nbr; - // (*stack)->next->nbr = temp; - if (NULL == stack || NULL == (*stack)) return ; (*stack) = (*stack)->next; From 5403734dbd8159d180aa2c08ea40d4025161be81 Mon Sep 17 00:00:00 2001 From: umeneses Date: Fri, 21 Jun 2024 18:47:04 -0300 Subject: [PATCH 09/17] updating user feedback --- program_to_test/src/push_main.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/program_to_test/src/push_main.c b/program_to_test/src/push_main.c index d2c4ddf..9b55c9e 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/19 16:27:43 by umeneses ### ########.fr */ +/* Updated: 2024/06/21 11:51:44 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,10 +30,10 @@ int main(int argc, char **argv) stack_b = NULL; stack_a = ft_lts_buildstack_argv(&stack_a, argv); ft_lst_map_all_indexers(&stack_a); - ft_lst_printf_data_content("original main stack_a:", stack_a); + ft_lst_printf_data_content("origin stack_a:", stack_a); ft_do_sort(&stack_a, &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_lst_printf_data_content("// final stack_a:", stack_a); + ft_lst_printf_data_content("// final stack_b:", stack_b); ft_lstclear_single_ptr(stack_a); ft_lstclear_single_ptr(stack_b); return (0); From 7c0899cbb5cfd13c2a00cf04107430c9aa0a1718 Mon Sep 17 00:00:00 2001 From: umeneses Date: Tue, 25 Jun 2024 17:27:54 -0300 Subject: [PATCH 10/17] adding absolute number function --- program_to_test/headers/push_swap.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/program_to_test/headers/push_swap.h b/program_to_test/headers/push_swap.h index 2ba240b..db972f1 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/19 15:07:14 by umeneses ### ########.fr */ +/* Updated: 2024/06/20 12:06:30 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -80,6 +80,7 @@ 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); +int abs_nbr(int nbr); void ft_lst_do_cheapest_move(t_stack **stack_a, t_stack **stack_b); void ft_do_move_after_cheapest(t_stack **stack_a, t_stack **stack_b, int cost_a, int cost_b); From 31203ac59f809c80e677a79cbc1d8069777a51b7 Mon Sep 17 00:00:00 2001 From: umeneses Date: Tue, 25 Jun 2024 17:28:49 -0300 Subject: [PATCH 11/17] updating numbers to test --- _ci_tdd/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_ci_tdd/Makefile b/_ci_tdd/Makefile index 16bbff2..19f4c22 100644 --- a/_ci_tdd/Makefile +++ b/_ci_tdd/Makefile @@ -6,7 +6,7 @@ # By: umeneses +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/05/10 10:09:44 by umeneses #+# #+# # -# Updated: 2024/06/19 17:28:23 by umeneses ### ########.fr # +# Updated: 2024/06/20 12:50:07 by umeneses ### ########.fr # # # # **************************************************************************** # @@ -75,7 +75,7 @@ COMP_EXE = $(CC) $(LDFLAGS) $(OBJ_FILES) $(LDLIBS) -o $(NAME) # **************************************************************************** # define arguments - 42 1 10 21 + 42 1 10 21 11 77 666 endef define bonus From ce6321f10eb976abeba7b5ce5fc39da7948d1b04 Mon Sep 17 00:00:00 2001 From: umeneses Date: Tue, 25 Jun 2024 17:30:34 -0300 Subject: [PATCH 12/17] adding extra increment count if reader arrives at the end of the list --- program_to_test/src/ft_lst_map_indexers.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/program_to_test/src/ft_lst_map_indexers.c b/program_to_test/src/ft_lst_map_indexers.c index 5959177..3d5fe23 100644 --- a/program_to_test/src/ft_lst_map_indexers.c +++ b/program_to_test/src/ft_lst_map_indexers.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/10 10:34:17 by umeneses #+# #+# */ -/* Updated: 2024/06/21 11:46:06 by umeneses ### ########.fr */ +/* Updated: 2024/06/25 13:43:41 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,11 +22,11 @@ void ft_lst_map_actual_position(t_stack **stack) { (*stack)->origin = actual_position; (*stack) = (*stack)->next; - // if ((*stack)->next == NULL) - // { - // actual_position++; - // (*stack)->origin = actual_position; - // } + if ((*stack)->next == NULL) + { + actual_position++; + (*stack)->origin = actual_position; + } actual_position++; } *stack = ft_lst_goto_head(*stack); From 60b3570350822814a21fc5cc57b901e7b43a8eca Mon Sep 17 00:00:00 2001 From: umeneses Date: Tue, 25 Jun 2024 17:31:01 -0300 Subject: [PATCH 13/17] fixing tester --- _ci_tdd/test_files/test_main_miunit.c | 122 ++++++++++++++++++-------- 1 file changed, 87 insertions(+), 35 deletions(-) diff --git a/_ci_tdd/test_files/test_main_miunit.c b/_ci_tdd/test_files/test_main_miunit.c index f2987fb..606df1e 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/19 17:10:18 by umeneses ### ########.fr */ +/* Updated: 2024/06/25 17:26:40 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,19 +66,20 @@ MU_TEST(test_3rd_miastep_get_cost) 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[8]); + expected_top_a = ft_atoi(argv_simulation[5]); expected_bottom_a = ft_atoi(argv_simulation[2]); stack_a = ft_lts_buildstack_argv(&stack_a, argv_simulation); - expected_cost = 0; // stack_b->nbr[8] + expected_cost = 2; // stack_b[3]->nbr = 4 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_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; - actual_cost = stack_b->next->cost_b; + actual_bottom_a = ft_lst_goto_end(stack_a)->nbr; + actual_cost = stack_b->next->next->cost_b; + // ASSERT mu_assert_int_eq(expected_cost, actual_cost); mu_assert_int_eq(expected_size, actual_size); @@ -98,12 +99,12 @@ MU_TEST(test_3rd_miastep_map_get_target_position) char *userinput; int expected_size; int actual_size; - int expected_top_a; - int actual_top_a; + int expected_top_b; + int actual_top_b; 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; @@ -111,25 +112,25 @@ MU_TEST(test_3rd_miastep_map_get_target_position) 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[8]); + expected_top_b = ft_atoi(argv_simulation[7]); expected_bottom_a = ft_atoi(argv_simulation[2]); stack_a = ft_lts_buildstack_argv(&stack_a, argv_simulation); ft_lst_map_all_indexers(&stack_a); - // expected_target_pos = 5; // stack_b->origin[5] + expected_target_pos = 2; // should be stack_b->target 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; + + actual_bottom_a = ft_lst_goto_end(stack_a)->nbr; + actual_top_b = ft_lst_goto_head(stack_b)->nbr; + 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_top_b, actual_top_b); mu_assert_int_eq(expected_bottom_a, actual_bottom_a); ft_lstclear_single_ptr(stack_a); ft_lstclear_single_ptr(stack_b); @@ -164,15 +165,24 @@ MU_TEST(test_3rd_miastep_map_lowest_final_pos) stack = ft_lts_buildstack_argv(&stack, argv_simulation); 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]); + expected_lowest_final_pos_pos = 2; //should be stack->origin[2] + expected_lowest_final_pos_nbr = ft_atoi(argv_simulation[3]); // argv[3] = 1; actual_size = ft_lst_size(stack); actual_top_a = ft_lst_goto_head(stack)->nbr; actual_lowest_final_pos_pos = ft_lst_map_lowest_final_position(&stack); actual_bottom_a = ft_lst_goto_end(stack)->nbr; - actual_lowest_final_pos_nbr = stack->next->next->nbr; - + actual_lowest_final_pos_nbr = 0; + while (stack) + { + if (stack->origin == 2) + { + actual_lowest_final_pos_pos = stack->origin; + actual_lowest_final_pos_nbr = stack->nbr; + break ; + } + stack = stack->next; + } // ASSERT mu_assert_int_eq(expected_lowest_final_pos_pos, actual_lowest_final_pos_pos); @@ -184,7 +194,44 @@ MU_TEST(test_3rd_miastep_map_lowest_final_pos) ft_array_clear(argv_simulation, expected_size); } +MU_TEST(test_sort_four_nbrs_v05) +{ + // 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 expected_bottom_a; + int actual_top_a; + int actual_bottom_a; + + // ACT + stack_a = NULL; + stack_b = NULL; + userinput = "./push_swap 2 3 1 0"; + argv_simulation = ft_split(userinput, ' '); + expected_size = ft_minunit_array_counter(argv_simulation); + expected_top_a = ft_atoi(argv_simulation[4]); + expected_bottom_a = ft_atoi(argv_simulation[2]); + stack_a = ft_lts_buildstack_argv(&stack_a, argv_simulation); + ft_lst_map_all_indexers(&stack_a); + ft_do_sort(&stack_a, &stack_b); + actual_top_a = ft_lst_goto_head(stack_a)->nbr; + actual_bottom_a = ft_lst_goto_end(stack_a)->nbr; + actual_size = ft_lst_size(stack_a); + + // ASSERT + mu_assert_int_eq(expected_top_a, actual_top_a); + mu_assert_int_eq(expected_bottom_a, actual_bottom_a); + mu_assert_int_eq(expected_size, actual_size); + ft_lstclear_single_ptr(stack_a); + ft_lstclear_single_ptr(stack_b); + ft_array_clear(argv_simulation, expected_size); +} MU_TEST(test_sort_four_nbrs_v04) { @@ -211,12 +258,10 @@ MU_TEST(test_sort_four_nbrs_v04) stack_a = ft_lts_buildstack_argv(&stack_a, argv_simulation); ft_lst_map_all_indexers(&stack_a); - ft_lst_printf_data_content("stack_a before sort_four_nbrs:", stack_a); ft_sort_four_nbrs(&stack_a, &stack_b); actual_top_a = ft_lst_goto_head(stack_a)->nbr; actual_bottom_a = ft_lst_goto_end(stack_a)->nbr; actual_size = ft_lst_size(stack_a); - ft_lst_printf_data_content("stack_a after sort_four_nbrs:", stack_a); // ASSERT mu_assert_int_eq(expected_top_a, actual_top_a); @@ -488,12 +533,12 @@ MU_TEST(test_1st_miastep_final_pos_index) // ACT stack = NULL; - userinput = "./push_swap 2 42 0 4 8 3 31 27"; + userinput = "./push_swap 1 2 42 0 4 8 3 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]); - expected_highest_final_pos = expected_size - 1; + expected_bottom_a = ft_atoi(argv_simulation[9]); + expected_highest_final_pos = expected_size; stack = ft_lts_buildstack_argv(&stack, argv_simulation); ft_lst_map_actual_position(&stack); @@ -501,12 +546,15 @@ MU_TEST(test_1st_miastep_final_pos_index) actual_size = ft_lst_size(stack); actual_top_a = ft_lst_goto_head(stack)->nbr; - - while (stack->origin <= 2) + actual_highest_final_pos = 0; + while (stack) { - stack = stack->next; if (stack->origin == 2) + { actual_highest_final_pos = stack->final_pos; + break ; + } + stack = stack->next; } actual_bottom_a = ft_lst_goto_end(stack)->nbr; @@ -536,23 +584,26 @@ MU_TEST(test_1st_miastep_pos_index) // ACT stack = NULL; - userinput = "./push_swap 2 42 0 4 8 3 31 27"; + userinput = "./push_swap 2 42 0 4 8 3 31 27 1 100 200 150 80"; 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]); - expected_fith_position = ft_atoi(argv_simulation[5]); + expected_bottom_a = ft_atoi(argv_simulation[13]); + expected_fith_position = ft_atoi(argv_simulation[6]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); ft_lst_map_actual_position(&stack); - actual_size = ft_lst_size(stack); actual_top_a = ft_lst_goto_head(stack)->nbr; - while (stack->origin <= 5) + actual_fith_position = 0; + while (stack) { - stack = stack->next; if (stack->origin == 5) + { actual_fith_position = stack->nbr; + break ; + } + stack = stack->next; } actual_bottom_a = ft_lst_goto_end(stack)->nbr; // ASSERT @@ -1676,6 +1727,7 @@ MU_TEST_SUITE(sorting_4_nbrs_tests) MU_RUN_TEST(test_sort_four_nbrs_v02); MU_RUN_TEST(test_sort_four_nbrs_v03); MU_RUN_TEST(test_sort_four_nbrs_v04); + MU_RUN_TEST(test_sort_four_nbrs_v05); } MU_TEST_SUITE(miacombeau_3rd_step_tests) From 1fa69fca5bb134e52448c6d7dfd7253061e926d4 Mon Sep 17 00:00:00 2001 From: umeneses Date: Tue, 25 Jun 2024 17:35:10 -0300 Subject: [PATCH 14/17] applying norminette --- program_to_test/src/ft_do_sort.c | 4 +--- program_to_test/src/ft_lst_map_all.c | 4 ++-- program_to_test/src/ft_lst_map_indexers.c | 3 +-- program_to_test/src/ft_push.c | 4 ++-- program_to_test/src/ft_rotate.c | 4 ++-- 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/program_to_test/src/ft_do_sort.c b/program_to_test/src/ft_do_sort.c index aa4c24f..1006764 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/21 11:54:29 by umeneses ### ########.fr */ +/* Updated: 2024/06/25 17:32:23 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,5 +35,3 @@ void ft_do_sort(t_stack **stack_a, t_stack **stack_b) } } } - - diff --git a/program_to_test/src/ft_lst_map_all.c b/program_to_test/src/ft_lst_map_all.c index 6bdde69..763ac3b 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/18 13:53:02 by umeneses ### ########.fr */ +/* Updated: 2024/06/25 17:34:20 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,5 +18,5 @@ void ft_lst_map_all_indexers(t_stack **stack) stack_size = ft_lst_size(*stack); ft_lst_map_actual_position(stack); - ft_lst_map_final_pos(stack, stack_size); + ft_lst_map_final_pos(stack, stack_size + 1); } diff --git a/program_to_test/src/ft_lst_map_indexers.c b/program_to_test/src/ft_lst_map_indexers.c index 3d5fe23..c0af000 100644 --- a/program_to_test/src/ft_lst_map_indexers.c +++ b/program_to_test/src/ft_lst_map_indexers.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/10 10:34:17 by umeneses #+# #+# */ -/* Updated: 2024/06/25 13:43:41 by umeneses ### ########.fr */ +/* Updated: 2024/06/25 17:34:25 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,7 +38,6 @@ void ft_lst_map_final_pos(t_stack **stack, int stack_size) t_stack *temp; t_stack *highest; - stack_size++; while (--stack_size > 0) { temp = *stack; diff --git a/program_to_test/src/ft_push.c b/program_to_test/src/ft_push.c index cf34850..955b78a 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/20 11:35:31 by umeneses ### ########.fr */ +/* Updated: 2024/06/25 17:32:45 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,7 +43,7 @@ void ft_push_b(t_stack **stack_a, t_stack **stack_b) if (NULL == *stack_a) return ; - to_be_pushed = (*stack_a); + to_be_pushed = (*stack_a); (*stack_a) = (*stack_a)->next; if ((*stack_a)) (*stack_a)->prev = NULL; diff --git a/program_to_test/src/ft_rotate.c b/program_to_test/src/ft_rotate.c index 2ee3c9d..e3f0d2e 100644 --- a/program_to_test/src/ft_rotate.c +++ b/program_to_test/src/ft_rotate.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/23 11:48:11 by umeneses #+# #+# */ -/* Updated: 2024/06/20 12:15:49 by umeneses ### ########.fr */ +/* Updated: 2024/06/25 17:32:02 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ void ft_rotate(t_stack **stack) { t_stack *last_node; - if (NULL == *stack || NULL== stack || ft_lst_size(*stack) <= 2) + if (NULL == *stack || NULL == stack || ft_lst_size(*stack) <= 2) return ; last_node = ft_lst_goto_end(*stack); last_node->next = *stack; From 65efca30db71901f1d7f3512e2d5e9f55a70a387 Mon Sep 17 00:00:00 2001 From: umeneses Date: Wed, 26 Jun 2024 13:54:59 -0300 Subject: [PATCH 15/17] adding moviment using the cheapest cost and applying into the program's flow --- program_to_test/Makefile | 3 +- program_to_test/headers/push_swap.h | 10 +++- program_to_test/src/ft_do_move_with_cost.c | 69 ++++++++++++++++++++++ program_to_test/src/ft_sort_5_or_more.c | 13 ++-- 4 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 program_to_test/src/ft_do_move_with_cost.c diff --git a/program_to_test/Makefile b/program_to_test/Makefile index 7d7deb3..4bdaa79 100644 --- a/program_to_test/Makefile +++ b/program_to_test/Makefile @@ -6,7 +6,7 @@ # By: umeneses +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/05/09 17:59:28 by umeneses #+# #+# # -# Updated: 2024/06/19 15:06:51 by umeneses ### ########.fr # +# Updated: 2024/06/26 12:23:15 by umeneses ### ########.fr # # # # **************************************************************************** # @@ -51,6 +51,7 @@ SRC_FILES += ft_do_push_fts.c SRC_FILES += ft_do_rev_rotate_fts.c SRC_FILES += ft_do_rotate_fts.c SRC_FILES += ft_do_swap_fts.c +SRC_FILES += ft_do_move_with_cost.c SRC_FILES += ft_lst_addto.c SRC_FILES += ft_lst_clear.c SRC_FILES += ft_lst_deletes.c diff --git a/program_to_test/headers/push_swap.h b/program_to_test/headers/push_swap.h index db972f1..6f49d22 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/20 12:06:30 by umeneses ### ########.fr */ +/* Updated: 2024/06/26 12:21:53 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -86,6 +86,14 @@ void ft_do_move_after_cheapest(t_stack **stack_a, t_stack **stack_b, int cost_a, int cost_b); /* sorting functions if list has 4 or more numbers */ +void ft_rev_rotate_ab_with_cost(t_stack **stack_a, t_stack **stack_b, + int *cost_a, int *cost_b); +void ft_rotate_ab_with_cost(t_stack **stack_a, t_stack **stack_b, + int *cost_a, int *cost_b); +void ft_rotate_a_with_cost(t_stack **stack_a, int *cost_a); +void ft_rotate_b_with_cost(t_stack **stack_b, int *cost_b); +/* sorting functions take takes the cheapest cost and do the move */ + void ft_do_swap_a(t_stack **stack); void ft_do_swap_b(t_stack **stack); void ft_do_swap_ab(t_stack **stack_a, t_stack **stack_b); diff --git a/program_to_test/src/ft_do_move_with_cost.c b/program_to_test/src/ft_do_move_with_cost.c new file mode 100644 index 0000000..cf197b0 --- /dev/null +++ b/program_to_test/src/ft_do_move_with_cost.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_do_move_with_cost.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: umeneses +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/26 12:16:53 by umeneses #+# #+# */ +/* Updated: 2024/06/26 12:20:49 by umeneses ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void ft_rev_rotate_ab_with_cost(t_stack **stack_a, t_stack **stack_b, + int *cost_a, int *cost_b) +{ + while (*cost_a < 0 && *cost_b < 0) + { + (*cost_a)++; + (*cost_b)++; + ft_do_reverse_rotate_ab(stack_a, stack_b); + } +} + +void ft_rotate_ab_with_cost(t_stack **stack_a, t_stack **stack_b, + int *cost_a, int *cost_b) +{ + while (*cost_a > 0 && *cost_b > 0) + { + (*cost_a)--; + (*cost_b)--; + ft_do_rotate_ab(stack_a, stack_b); + } +} + +void ft_rotate_a_with_cost(t_stack **stack_a, int *cost_a) +{ + while (*cost_a) + { + if (*cost_a > 0) + { + ft_do_rotate_a(stack_a); + (*cost_a)--; + } + else if (*cost_a < 0) + { + ft_do_reverse_rotate_a(stack_a); + (*cost_a)++; + } + } +} + +void ft_rotate_b_with_cost(t_stack **stack_b, int *cost_b) +{ + while (*cost_b) + { + if (*cost_b > 0) + { + ft_do_rotate_a(stack_b); + (*cost_b)--; + } + else if (*cost_b < 0) + { + ft_do_reverse_rotate_a(stack_b); + (*cost_b)++; + } + } +} diff --git a/program_to_test/src/ft_sort_5_or_more.c b/program_to_test/src/ft_sort_5_or_more.c index 113d61d..b200e22 100644 --- a/program_to_test/src/ft_sort_5_or_more.c +++ b/program_to_test/src/ft_sort_5_or_more.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/10 12:41:29 by umeneses #+# #+# */ -/* Updated: 2024/06/21 11:54:43 by umeneses ### ########.fr */ +/* Updated: 2024/06/26 13:52:17 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,8 +16,6 @@ void ft_sort_five_or_more_nbr(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_map_actual_position(stack_a); - ft_lst_map_actual_position(stack_b); while (*stack_b) { ft_lst_get_target_position(stack_a, stack_b); @@ -72,6 +70,7 @@ void ft_lst_do_cheapest_move(t_stack **stack_a, t_stack **stack_b) cheapest = abs_nbr(temp_b->cost_b) + abs_nbr(temp_b->cost_a); cost_a = temp_b->cost_a; cost_b = temp_b->cost_b; + ft_printf("\t\tb_nbr[%d]\tcheapest[%d]\tcost_a[%d]\tcost_b[%d]\n", temp_b->nbr, cheapest, cost_a, cost_b); } temp_b = temp_b->next; } @@ -89,10 +88,10 @@ 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); + ft_rev_rotate_ab_with_cost(stack_a, stack_b, &cost_a, &cost_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_rotate_ab_with_cost(stack_a, stack_b, &cost_a, &cost_b); + ft_rotate_a_with_cost(stack_a, &cost_a); + ft_rotate_b_with_cost(stack_b, &cost_b); ft_do_push_a(stack_a, stack_b); } From b7a36baf2cc6d986986ace4dd8dd2f00d3ce50a3 Mon Sep 17 00:00:00 2001 From: umeneses Date: Wed, 26 Jun 2024 14:04:46 -0300 Subject: [PATCH 16/17] clearing printer feedback --- program_to_test/src/ft_do_push_fts.c | 5 +---- program_to_test/src/ft_do_rev_rotate_fts.c | 9 +-------- program_to_test/src/ft_do_rotate_fts.c | 7 +------ program_to_test/src/ft_do_sort.c | 8 +------- program_to_test/src/ft_sort_5_or_more.c | 3 +-- program_to_test/src/ft_sort_until3.c | 3 +-- program_to_test/src/ft_sort_until4.c | 4 +--- 7 files changed, 7 insertions(+), 32 deletions(-) diff --git a/program_to_test/src/ft_do_push_fts.c b/program_to_test/src/ft_do_push_fts.c index 1c3b113..6e0b082 100644 --- a/program_to_test/src/ft_do_push_fts.c +++ b/program_to_test/src/ft_do_push_fts.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 12:24:04 by umeneses #+# #+# */ -/* Updated: 2024/06/21 11:30:11 by umeneses ### ########.fr */ +/* Updated: 2024/06/26 14:01:22 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,6 @@ void ft_do_push_a(t_stack **stack_a, t_stack **stack_b) { - ft_lst_printf_data_content("after mov stack_a:", *stack_a); - ft_printf("\n"); - ft_lst_printf_data_content("after mov stack_b:", *stack_b); ft_push_a(stack_a, stack_b); ft_putstr_fd("pa\n", 1); } diff --git a/program_to_test/src/ft_do_rev_rotate_fts.c b/program_to_test/src/ft_do_rev_rotate_fts.c index 1bd84e6..feb392f 100644 --- a/program_to_test/src/ft_do_rev_rotate_fts.c +++ b/program_to_test/src/ft_do_rev_rotate_fts.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 12:24:04 by umeneses #+# #+# */ -/* Updated: 2024/06/21 11:31:32 by umeneses ### ########.fr */ +/* Updated: 2024/06/26 14:00:55 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,25 +14,18 @@ void ft_do_reverse_rotate_a(t_stack **stack) { - ft_lst_printf_data_content("after mov stack_a:", *stack); - ft_printf("\n"); ft_reverse_rotate(stack); ft_putstr_fd("rra\n", 1); } void ft_do_reverse_rotate_b(t_stack **stack) { - ft_lst_printf_data_content("after mov stack_b:", *stack); - ft_printf("\n"); ft_reverse_rotate(stack); ft_putstr_fd("rrb\n", 1); } void ft_do_reverse_rotate_ab(t_stack **stack_a, t_stack **stack_b) { - ft_lst_printf_data_content("after mov stack_a:", *stack_a); - ft_printf("\n"); - ft_lst_printf_data_content("after mov stack_b:", *stack_b); ft_reverse_rotate_ab(stack_a, stack_b); ft_putstr_fd("rrr\n", 1); } diff --git a/program_to_test/src/ft_do_rotate_fts.c b/program_to_test/src/ft_do_rotate_fts.c index e01de14..f99ad8b 100644 --- a/program_to_test/src/ft_do_rotate_fts.c +++ b/program_to_test/src/ft_do_rotate_fts.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 12:24:04 by umeneses #+# #+# */ -/* Updated: 2024/06/21 11:30:55 by umeneses ### ########.fr */ +/* Updated: 2024/06/26 14:01:11 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,23 +14,18 @@ void ft_do_rotate_a(t_stack **stack) { - ft_lst_printf_data_content("after mov stack_a:", *stack); ft_rotate(stack); ft_putstr_fd("ra\n", 1); } void ft_do_rotate_b(t_stack **stack) { - ft_lst_printf_data_content("after mov stack_b:", *stack); ft_rotate(stack); ft_putstr_fd("rb\n", 1); } void ft_do_rotate_ab(t_stack **stack_a, t_stack **stack_b) { - ft_lst_printf_data_content("after mov stack_a:", *stack_a); - ft_printf("\n"); - ft_lst_printf_data_content("after mov stack_b:", *stack_b); ft_rotate_ab(stack_a, stack_b); ft_putstr_fd("rr\n", 1); } diff --git a/program_to_test/src/ft_do_sort.c b/program_to_test/src/ft_do_sort.c index 1006764..a6b0e79 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/25 17:32:23 by umeneses ### ########.fr */ +/* Updated: 2024/06/26 14:01:57 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,12 +23,6 @@ void ft_do_sort(t_stack **stack_a, t_stack **stack_b) ft_sort_two_nbrs(stack_a); else if (stack_size == 3) ft_sort_three_nbrs(stack_a); - // else if (stack_size == 4) - // ft_sort_four_nbrs(stack_a, stack_b); - // else if (stack_size >= 5) - // { - // ft_sort_five_or_more_nbr(stack_a, stack_b); - // } else { ft_sort_five_or_more_nbr(stack_a, stack_b); diff --git a/program_to_test/src/ft_sort_5_or_more.c b/program_to_test/src/ft_sort_5_or_more.c index b200e22..5d7778a 100644 --- a/program_to_test/src/ft_sort_5_or_more.c +++ b/program_to_test/src/ft_sort_5_or_more.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/10 12:41:29 by umeneses #+# #+# */ -/* Updated: 2024/06/26 13:52:17 by umeneses ### ########.fr */ +/* Updated: 2024/06/26 14:01:00 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,7 +70,6 @@ void ft_lst_do_cheapest_move(t_stack **stack_a, t_stack **stack_b) cheapest = abs_nbr(temp_b->cost_b) + abs_nbr(temp_b->cost_a); cost_a = temp_b->cost_a; cost_b = temp_b->cost_b; - ft_printf("\t\tb_nbr[%d]\tcheapest[%d]\tcost_a[%d]\tcost_b[%d]\n", temp_b->nbr, cheapest, cost_a, cost_b); } temp_b = temp_b->next; } diff --git a/program_to_test/src/ft_sort_until3.c b/program_to_test/src/ft_sort_until3.c index 6a5f11c..da99c2f 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/20 12:31:18 by umeneses ### ########.fr */ +/* Updated: 2024/06/26 14:02:16 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,6 @@ bool ft_is_sorted(t_stack *list) return (false); temp = temp->next; } - // ft_printf(">>>>>>>>>>>>> list is sorted!\n"); return (true); } diff --git a/program_to_test/src/ft_sort_until4.c b/program_to_test/src/ft_sort_until4.c index 427eece..115651c 100644 --- a/program_to_test/src/ft_sort_until4.c +++ b/program_to_test/src/ft_sort_until4.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/10 12:41:29 by umeneses #+# #+# */ -/* Updated: 2024/06/21 11:43:43 by umeneses ### ########.fr */ +/* Updated: 2024/06/26 13:47:22 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,8 +77,6 @@ void ft_lst_shift_stack(t_stack **stack_a) lowest_final_pos++; } } - // if (ft_lst_goto_end(*stack_a)->final_pos == 1) - // ft_do_reverse_rotate_a(stack_a); else { while (lowest_final_pos > 0) From a29b49568afd03ed38159a6778a63ad05cdc31c6 Mon Sep 17 00:00:00 2001 From: umeneses Date: Wed, 26 Jun 2024 14:06:47 -0300 Subject: [PATCH 17/17] adding ft_do_move_with_costs file into tests --- _ci_tdd/test_files/test_main_miunit.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/_ci_tdd/test_files/test_main_miunit.c b/_ci_tdd/test_files/test_main_miunit.c index 606df1e..327297a 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/25 17:26:40 by umeneses ### ########.fr */ +/* Updated: 2024/06/26 14:06:06 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,7 @@ #include "../../program_to_test/src/ft_do_rotate_fts.c" #include "../../program_to_test/src/ft_do_swap_fts.c" #include "../../program_to_test/src/ft_do_sort.c" +#include "../../program_to_test/src/ft_do_move_with_cost.c" #include "../../program_to_test/src/ft_sort_until3.c" #include "../../program_to_test/src/ft_sort_until4.c" #include "../../program_to_test/src/ft_sort_5_or_more.c" @@ -542,8 +543,7 @@ MU_TEST(test_1st_miastep_final_pos_index) stack = ft_lts_buildstack_argv(&stack, argv_simulation); ft_lst_map_actual_position(&stack); - ft_lst_map_final_pos(&stack, expected_size); - + ft_lst_map_final_pos(&stack, expected_size + 1); actual_size = ft_lst_size(stack); actual_top_a = ft_lst_goto_head(stack)->nbr; actual_highest_final_pos = 0;