summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Kaiser2024-04-17 12:21:25 +0200
committerDominik Kaiser2024-04-17 12:21:25 +0200
commitb0a8aaeddfc5feb8a55d16891e199f635291c66a (patch)
treeba947e8efc2420017e2de620fbdb3958d6bcb023
parent26e76f9d7ad46948b0548c41375f81cacf20b47c (diff)
downloadpush_swap-b0a8aaeddfc5feb8a55d16891e199f635291c66a.tar.gz
push_swap-b0a8aaeddfc5feb8a55d16891e199f635291c66a.zip
Add movement (with some bugs)
TODO: Fix sorting direction. Right now the numbers are sorted max -> min instead of min -> max. TODO: Fix sorting bug. Some numbers are not sorted.
-rw-r--r--cmd_optimization.c3
-rw-r--r--sorting.c49
2 files changed, 48 insertions, 4 deletions
diff --git a/cmd_optimization.c b/cmd_optimization.c
index d0928f5..9fcf876 100644
--- a/cmd_optimization.c
+++ b/cmd_optimization.c
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/13 16:42:34 by dkaiser #+# #+# */
-/* Updated: 2024/04/17 09:41:10 by dkaiser ### ########.fr */
+/* Updated: 2024/04/17 12:15:41 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -88,5 +88,6 @@ void optimize_commands(t_psdata *data)
{
optimize_redundant(data, PA, PB);
optimize_redundant(data, RB, RRB);
+ optimize_redundant(data, RA, RRA);
optimize_two_stack_ops(data, RA, RB, RR);
}
diff --git a/sorting.c b/sorting.c
index a682280..c4d3644 100644
--- a/sorting.c
+++ b/sorting.c
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/13 15:04:19 by dkaiser #+# #+# */
-/* Updated: 2024/04/17 11:50:56 by dkaiser ### ########.fr */
+/* Updated: 2024/04/17 12:20:26 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -57,9 +57,51 @@ static int calculate_score(t_psdata *data, int pos)
return moves_to_top + moves_to_spot;
}
-static void move_to_right_spot(t_psdata *data, int idx)
+
+
+static void move_to_top(t_psdata *data, int idx)
+{
+ if (idx < (data->b->size + 1) / 2)
+ {
+ while (idx--)
+ run_command(data, RB);
+ }
+ else
+ {
+ idx = data->b->size - idx;
+ while (idx--)
+ run_command(data, RRB);
+ }
+}
+
+static void move_to_spot(t_psdata *data)
{
+ int pos;
+ int i;
+ pos = 0;
+ while (pos < data->a->size && data->b->stack[0] < data->a->stack[pos])
+ pos++;
+ if (pos < (data->a->size + 1) / 2)
+ {
+ i = pos;
+ while (i--)
+ run_command(data, RA);
+ run_command(data, PA);
+ i = pos;
+ while (i--)
+ run_command(data, RRA);
+ }
+ else
+ {
+ i = data->a->size - pos;
+ while (i--)
+ run_command(data, RRA);
+ run_command(data, PA);
+ i = data->a->size - pos;
+ while (i--)
+ run_command(data, RA);
+ }
}
static void scoresort(t_psdata *data)
@@ -84,7 +126,8 @@ static void scoresort(t_psdata *data)
min_score = i;
i++;
}
- move_to_right_spot(data, min_score);
+ move_to_top(data, min_score);
+ move_to_spot(data);
free(scores);
if (data->b->size > 0)