After the sorting algorithm was executed remove unneccessary commands.
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)
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* 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);
+}
/* 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 */
/* */
/* ************************************************************************** */
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);
}
/* 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 */
/* */
/* ************************************************************************** */
RR,
RRA,
RRB,
- RRR
+ RRR,
+ NO_CMD
};
t_list *create_stack(int argc, char *argv[]);
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);
/* 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++;
}