diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | cmd_optimization.c | 85 | ||||
| -rw-r--r-- | main.c | 6 | ||||
| -rw-r--r-- | push_swap.h | 6 | ||||
| -rw-r--r-- | sorting.c | 13 |
5 files changed, 102 insertions, 10 deletions
@@ -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 index 0000000..2d93252 --- /dev/null +++ b/cmd_optimization.c @@ -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); +} @@ -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); } diff --git a/push_swap.h b/push_swap.h index 978fb1d..beadcd0 100644 --- a/push_swap.h +++ b/push_swap.h @@ -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); @@ -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++; } |
