diff options
| author | Dominik Kaiser | 2024-04-24 15:04:53 +0200 |
|---|---|---|
| committer | Dominik Kaiser | 2024-04-24 15:04:53 +0200 |
| commit | fe350c968c35c2f1c2df9aaf76c7736ac096f344 (patch) | |
| tree | a5e846d082b249f5fd629ac51d12dce9a149e151 | |
| parent | 2d6275bad3467b01db64a5d6dcf74d746d10d657 (diff) | |
| download | push_swap-scoresort.tar.gz push_swap-scoresort.zip | |
Add sorting_utils.cscoresort
| -rw-r--r-- | sorting_utils.c | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/sorting_utils.c b/sorting_utils.c new file mode 100644 index 0000000..61badd3 --- /dev/null +++ b/sorting_utils.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sorting_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/24 13:37:07 by dkaiser #+# #+# */ +/* Updated: 2024/04/24 13:42:32 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +int calculate_score(t_psdata *data, int pos) +{ + int moves_to_top; + int moves_to_spot; + int i; + + if (pos < (data->b->size + 1) / 2) + moves_to_top = pos; + else + moves_to_top = data->b->size - pos; + i = 0; + while (i < data->a->size && data->a->stack[i] > data->b->stack[pos]) + i++; + if (i < (data->a->size + 1) / 2) + moves_to_spot = 2 * i + 1; + else + moves_to_spot = 2 * ((data->a->size - i) + 1); + return (moves_to_top + moves_to_spot); +} + +void move_to_top(t_psdata *data, int idx) +{ + if (idx < (data->b->size + 1) / 2) + { + while (--idx > 0) + run_command(data, RB); + } + else + { + idx = data->b->size - idx; + while (idx--) + run_command(data, RRB); + } + run_command(data, PA); +} + +static int get_pos(t_psdata *data) +{ + int pos; + + pos = 0; + while (pos < data->a->size && data->b->stack[0] > data->a->stack[pos]) + pos++; + return (pos); +} + +void move_to_spot(t_psdata *data, int idx) +{ + int pos; + int i; + + pos = get_pos(data); + if (pos < (data->a->size + 1) / 2) + { + i = pos; + while (i--) + run_command(data, RA); + move_to_top(data, idx); + i = pos; + while (i--) + run_command(data, RRA); + } + else + { + i = data->a->size - pos; + while (i--) + run_command(data, RRA); + move_to_top(data, idx); + i = data->a->size - pos; + while (i--) + run_command(data, RA); + } +} |
