From f28600d8a67027d705e3181be0328012a464e31b Mon Sep 17 00:00:00 2001 From: whaffman Date: Sun, 17 Nov 2024 18:14:53 +0100 Subject: [PATCH] fix sort 3 and start sortmore --- .vscode/launch.json | 2 +- src/push_swap.c | 1 + src/sort/sort3.c | 2 +- src/sort/sort5.c | 4 +++ src/sort/sortmore.c | 64 +++++++++++++++++++++++++++++++++++++++++++++ src/util/utils.c | 17 ++++++++++-- 6 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 src/sort/sortmore.c diff --git a/.vscode/launch.json b/.vscode/launch.json index e7f190e..4017ffb 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,7 +6,7 @@ "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/push_swap", - "args": ["20", "86", "63", "13", "84"], + "args": ["89", "35", "82", "97", "46"], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], diff --git a/src/push_swap.c b/src/push_swap.c index 4f097f7..a006790 100644 --- a/src/push_swap.c +++ b/src/push_swap.c @@ -70,6 +70,7 @@ int main(int argc, char *argv[]) return (1); } } + print_state(state); sort5(state); print_state(state); return (0); diff --git a/src/sort/sort3.c b/src/sort/sort3.c index 73ef666..ab9b58b 100644 --- a/src/sort/sort3.c +++ b/src/sort/sort3.c @@ -35,7 +35,7 @@ int sort3(t_state *state) rra(state); sa(state); } - else if (!(a > c && b < c)) + else if ((a > b && a < c) || (a > b && b > c)) sa(state); return (1); } diff --git a/src/sort/sort5.c b/src/sort/sort5.c index b547dde..bf212e3 100644 --- a/src/sort/sort5.c +++ b/src/sort/sort5.c @@ -36,8 +36,12 @@ int sort5(t_state *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); } diff --git a/src/sort/sortmore.c b/src/sort/sortmore.c new file mode 100644 index 0000000..f89a999 --- /dev/null +++ b/src/sort/sortmore.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* sortmore.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/17 17:25:40 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/17 17:39:12 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" +#include + +int count_moves_merge_a(int idx_b, t_state *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); + const int shortest_a = shortest_rotate(idx_a, ft_lstsize(state->a)); + const int shortest_b = shortest_rotate(idx_b, ft_lstsize(state->b)); + + 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); +} + +int best_merge_a(t_state *state) +{ + const int size = ft_lstsize(state->b); + int moves; + int best_b; + int min_moves; + int i; + + min_moves = INT_MAX; + best_b = 0; + i = 0; + while (i < size) + { + moves = count_moves_merge_a(i, state); + if (moves < min_moves) + { + min_moves = moves; + best_b = i; + } + i++; + } + return (best_b); +} + +void b_merge_a(int idx_b, t_state *state) +{ + rotate_b_to(idx_b, state); + merge_to_a(state); +} + +void sortmore(t_state *state) +{ + while (state->b) + b_merge_a(best_merge_a(state)) +} + + diff --git a/src/util/utils.c b/src/util/utils.c index 43fcc90..a49987c 100644 --- a/src/util/utils.c +++ b/src/util/utils.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/11/10 14:44:51 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/11/10 16:35:59 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/11/17 17:41:19 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ @@ -68,6 +68,19 @@ int index_of(int number, t_list *stack) } return (-1); } +int ft_abs(int a) +{ + if (a >= 0) + return (a); + return (-a); +} + +int ft_max(int a, int b) +{ + if (a > b) + return (a); + return (b); +} int ft_min(int a, int b) { @@ -83,7 +96,7 @@ int count_gt(int n, t_list *stack) count = 0; while (stack) { - if (*(int *) stack->content > n) + if (*(int *) stack->content < n) count++; stack = stack->next; }