]> git.dkaiser.de - 42/push_swap.git/commitdiff
Add cmd optimization
authorDominik Kaiser <dkaiser@2-E-4.42heilbronn.de>
Sat, 13 Apr 2024 15:49:06 +0000 (17:49 +0200)
committerDominik Kaiser <dkaiser@2-E-4.42heilbronn.de>
Sat, 13 Apr 2024 15:49:06 +0000 (17:49 +0200)
After the sorting algorithm was executed remove unneccessary commands.

Makefile
cmd_optimization.c [new file with mode: 0644]
main.c
push_swap.h
sorting.c

index 5aeb05971981e3247532a51041a50102f00e3dc9..5de184bd739078a5f300727b8ab7e40cfe5e5ec3 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ LIBFT = libft
 CC = cc
 CFLAGS = -Wall -Wextra -Werror
 
-SRC_FILES = main.c input_handling.c stack_utils.c command_handling.c sorting.c stack_optimization.c
+SRC_FILES = main.c input_handling.c stack_utils.c command_handling.c sorting.c stack_optimization.c cmd_optimization.c
 OBJ_FILES = $(SRC_FILES:%.c=%.o)
 
 LIB_DIR = $(LIBFT)
diff --git a/cmd_optimization.c b/cmd_optimization.c
new file mode 100644 (file)
index 0000000..2d93252
--- /dev/null
@@ -0,0 +1,85 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   cmd_optimization.c                                 :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/04/13 16:42:34 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/04/13 17:31:18 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft/ft_printf.h"
+#include "libft/libft.h"
+#include "push_swap.h"
+
+/* void        optimize_commands(t_list **cmds) */
+/* { */
+/*     int             optimizations; */
+/*     t_list  *cur; */
+/*     t_list *last; */
+
+/*     optimizations = 0; */
+/*     cur = *cmds; */
+/*     last = NULL; */
+/*     while (cur->next) */
+/*     { */
+/*             if ((*(enum e_pscmd *)cur->content == PA */
+/*                             && *(enum e_pscmd *)cur->next->content == PB) */
+/*                     || (*(enum e_pscmd *)cur->content == PA */
+/*                             && *(enum e_pscmd *)cur->next->content == PB)) */
+/*             { */
+/*             if (last) */
+/*                 last->next = cur->next->next; */
+/*             else */
+/*                 (*cmds)->next = cur->next->next; */
+/*             ft_lstdelone(cur->next, free); */
+/*             ft_lstdelone(cur, free); */
+/*             optimizations++; */
+/*         } */
+/*         if (!optimizations) */
+/*         { */
+/*             last = cur; */
+/*             cur = cur->next; */
+/*         } */
+/*         else */
+/*             break; */
+/*     } */
+/*     if (optimizations) */
+/*             optimize_commands(cmds); */
+/* } */
+
+static enum e_pscmd get_cmd(t_list *cmd)
+{
+    if (cmd)
+        return (*(enum e_pscmd*)cmd->content);
+    else
+        return NO_CMD;
+}
+
+void optimize_commands(t_list **cmds)
+{
+    t_list *cur;
+    t_list *last;
+    int optimizations;
+
+    cur = *cmds;
+    last = cur;
+    optimizations = 0;
+
+    while (cur->next)
+    {
+        if ((get_cmd(cur) == PA && get_cmd(cur->next) == PB) || (get_cmd(cur) == PB && get_cmd(cur->next) == PA))
+        {
+            last->next = cur->next->next;
+            ft_lstdelone(cur->next, free);
+            ft_lstdelone(cur, free);
+            optimizations++;
+        }
+        last = last->next;
+        cur = last->next;
+    }
+    if (optimizations)
+        optimize_commands(cmds);
+}
diff --git a/main.c b/main.c
index fbeee27f048843e88c711df0d2c443604993e978..a3e1375bf6f1077133f124a0655bd9a9909ef727 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/04/12 17:03:30 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/04/13 15:32:03 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/04/13 16:58:36 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -33,12 +33,12 @@ int main(int argc, char *argv[])
        pscmds = NULL;
        stack_optimize(&stack_a);
        stack_sort(&stack_a, &stack_b, &pscmds);
-       // TODO: Optimize commands
+       optimize_commands(&pscmds);
        print_commands(pscmds);
        ft_printf("A:");
        ft_lstiter(stack_a, print_content);
        ft_printf("\nB:");
        ft_lstiter(stack_b, print_content);
-       ft_printf("\n");
+       ft_printf("\nExecuted %d push_swap commands.\n", ft_lstsize(pscmds));
        return (0);
 }
index 978fb1df05e274b313e6515084821b11b0c03ad3..beadcd0ea3ecbd0043efd790bfa411941b92e0d4 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/04/12 16:59:09 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/04/13 15:27:07 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/04/13 17:18:58 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -27,7 +27,8 @@ enum  e_pscmd
        RR,
        RRA,
        RRB,
-       RRR
+       RRR,
+       NO_CMD
 };
 
 t_list *create_stack(int argc, char *argv[]);
@@ -42,6 +43,7 @@ void  run_command(t_list **stack_a, t_list **stack_b, t_list **cmds,
 void   print_commands(t_list *cmds);
 
 void   stack_optimize(t_list **stack);
+void   optimize_commands(t_list **cmds);
 
 void   stack_sort(t_list **stack_a, t_list **stack_b, t_list **cmds);
 
index 135a94d1884da6c74f88392eac6b02c9272d2463..ff825b6eabe1bb234df39ae62c386639b8fab884 100644 (file)
--- a/sorting.c
+++ b/sorting.c
@@ -6,29 +6,34 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/04/13 15:04:19 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/04/13 15:17:26 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/04/13 17:43:18 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "libft/libft.h"
 #include "push_swap.h"
 
+
+
 static void stack_radixsort(t_list **stack_a, t_list **stack_b, t_list **cmds)
 {
     int bit;
     int max;
     int i;
+    int max_bits;
 
     bit = 0;
-    while (bit < 9)
+    max_bits = 9;
+
+    while (bit < max_bits)
     {
         i = 0;
         max = ft_lstsize(*stack_a);
         while (i < max)
         {
-            if ((*(int *)(*stack_a)->content >> bit) % 2 == 0)
+            if (*stack_a && (*(int *)(*stack_a)->content >> bit) % 2 == 0)
                 run_command(stack_a, stack_b, cmds, PB);
-            else
+            else if ((*stack_a)->next)
                 run_command(stack_a, stack_b, cmds, RA);
             i++;
         }