norm && started mini mouv
This commit is contained in:
1 parent
b2729dc1f4
commit
d0303c2e04
51 files changed
+507
-184
No files matched your search
@@ -1 +1,2 @@
|
||||
visualizer-push-swap/
|
||||
push_swap_visualizer/
|
||||
@@ -6,7 +6,7 @@
|
||||
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/11/01 11:03:22 by adjoly #+# #+# #
|
||||
# Updated: 2024/02/22 11:51:16 by adjoly ### ########.fr #
|
||||
# Updated: 2024/03/11 15:45:33 by adjoly ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@@ -14,9 +14,11 @@ NAME = push_swap
|
||||
|
||||
CC = cc
|
||||
|
||||
OBJSDIR = obj/
|
||||
|
||||
SRCDIR = src/
|
||||
|
||||
SRCS = main.c \
|
||||
algo.c \
|
||||
median.c \
|
||||
check_error.c \
|
||||
print_stack.c \
|
||||
parsing.c \
|
||||
@@ -31,8 +33,13 @@ SRCS = main.c \
|
||||
operations/ft_pushstack.c \
|
||||
operations/ft_rotatestack.c \
|
||||
operations/ft_reverserotate.c \
|
||||
algo/get_min_max.c \
|
||||
algo/algo.c \
|
||||
algo/insertion.c \
|
||||
algo/median.c \
|
||||
utils/is_sorted.c \
|
||||
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.c=.o))
|
||||
|
||||
FLAGS = -Werror -Wall -Wextra -g
|
||||
|
||||
@@ -41,21 +48,25 @@ HEADERS = so_long.h
|
||||
LIB = libft/libft.a \
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
make -C libft
|
||||
$(CC) $(FLAGS) $(OBJS) $(LIB) -o $(NAME)
|
||||
@make -s -C libft
|
||||
@$(CC) $(FLAGS) $(OBJS) $(LIB) -o $(NAME)
|
||||
@echo "[✔] Compiled"
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(FLAGS) -I $(HEADERS) $< -c -o $@
|
||||
$(OBJSDIR)%.o: $(SRCDIR)%.c
|
||||
@mkdir -p $(@D)
|
||||
@$(CC) $(FLAGS) -I $(HEADERS) $< -c -o $@
|
||||
@echo "[✔] $< compiled"
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
clean:
|
||||
make -C libft clean
|
||||
rm -f $(OBJS)
|
||||
@make -s -C libft clean
|
||||
@rm -f $(OBJS)
|
||||
|
||||
fclean: clean
|
||||
make -C libft fclean
|
||||
rm -f $(NAME)
|
||||
@make -s -C libft fclean
|
||||
@rm -f $(NAME)
|
||||
@echo "[X] Cleaned"
|
||||
|
||||
re: fclean all
|
||||
|
||||
|
||||
Executable
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Submodule
+1
Submodule push_swap-testeur-max added at 84c59f165a.
-57
@@ -1,57 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* push_swap.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 12:18:29 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/22 11:50:24 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PUSH_SWAP_H
|
||||
# define PUSH_SWAP_H
|
||||
|
||||
typedef struct s_stack
|
||||
{
|
||||
int nb;
|
||||
struct s_stack *next;
|
||||
} t_stack;
|
||||
|
||||
#include "libft/libft.h"
|
||||
|
||||
void ft_check_args_format(char **av);
|
||||
void ft_check_double(t_stack **stack);
|
||||
|
||||
t_stack *ft_parsing(int ac, char **av);
|
||||
void ft_print_stack(t_stack *stack);
|
||||
|
||||
void ft_stackadd_back(t_stack **stack, t_stack *new);
|
||||
void ft_stackadd_front(t_stack **stack, t_stack *new);
|
||||
void ft_stackclear(t_stack **stack);
|
||||
void ft_stackdelone(t_stack *stack);
|
||||
t_stack *ft_stacklast(t_stack *stack);
|
||||
t_stack *ft_stacknew(int content);
|
||||
size_t ft_stacksize(t_stack *stack);
|
||||
t_stack *ft_stackbeforelast(t_stack *stack);
|
||||
|
||||
void ft_swap_a(t_stack *stack_a);
|
||||
void ft_swap_b(t_stack *stack_b);
|
||||
void ft_stack_ss(t_stack *stack_a, t_stack *stack_b);
|
||||
|
||||
void ft_push_a(t_stack **stack_a, t_stack **stack_b);
|
||||
void ft_push_b(t_stack **stack_a, t_stack **stack_b);
|
||||
|
||||
void ft_rotatestack_a(t_stack **stack_a);
|
||||
void ft_rotatestack_b(t_stack **stack_b);
|
||||
void ft_rotatestack_r(t_stack **stack_a, t_stack **stack_b);
|
||||
|
||||
void ft_reverserotate_a(t_stack **stack_a);
|
||||
void ft_reverserotate_b(t_stack **stack_b);
|
||||
void ft_reverserotate_r(t_stack **stack_a, t_stack **stack_b);
|
||||
|
||||
void ft_algo(t_stack **stack_a, t_stack **stack_b);
|
||||
int find_median(t_stack **stack);
|
||||
|
||||
#endif
|
||||
Submodule
+1
Submodule push_swap_tester added at 317339a8b6.
+25
-57
@@ -6,87 +6,55 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/21 17:29:26 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/22 13:29:52 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/03/11 16:06:26 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "../push_swap.h"
|
||||
|
||||
void send_to_stack_b(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
size_t stack_size;
|
||||
int q_one;
|
||||
int median;
|
||||
|
||||
while (*stack_a)
|
||||
{
|
||||
median = find_median(stack_a);
|
||||
q_one = find_q_one(stack_a);
|
||||
stack_size = ft_stacksize(*stack_a);
|
||||
while (*stack_a && stack_size != 0)
|
||||
while (stack_size != 0)
|
||||
{
|
||||
if ((*stack_a)->nb < median)
|
||||
if ((*stack_a)->nb <= median)
|
||||
{
|
||||
ft_push_b(stack_a, stack_b);
|
||||
if (*stack_b && (*stack_b)->nb <= q_one)
|
||||
ft_rotatestack_b(stack_b);
|
||||
}
|
||||
else
|
||||
ft_rotatestack_a(stack_a);
|
||||
stack_size--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t get_stack_max(t_stack **stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
size_t i;
|
||||
size_t i_max;
|
||||
int max;
|
||||
|
||||
tmp = *stack;
|
||||
max = tmp->nb;
|
||||
i_max = 0;
|
||||
i = 0;
|
||||
while (tmp)
|
||||
{
|
||||
if (tmp->nb > max)
|
||||
{
|
||||
max = tmp->nb;
|
||||
i_max = i;
|
||||
}
|
||||
tmp = tmp->next;
|
||||
i++;
|
||||
}
|
||||
return (i_max);
|
||||
if (*stack_a)
|
||||
ft_push_b(stack_a, stack_b);
|
||||
}
|
||||
|
||||
void ft_algo(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
size_t max;
|
||||
size_t i;
|
||||
size_t stack_size;
|
||||
|
||||
i = 0;
|
||||
send_to_stack_b(stack_a, stack_b);
|
||||
stack_size = ft_stacksize(*stack_b);
|
||||
while (*stack_b)
|
||||
if (ft_stacksize(*stack_a) == 2)
|
||||
{
|
||||
max = get_stack_max(stack_b);
|
||||
i = 0;
|
||||
if (max < stack_size / 2)
|
||||
{
|
||||
while (i < max)
|
||||
{
|
||||
ft_rotatestack_b(stack_b);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
i = stack_size;
|
||||
while (i > max)
|
||||
{
|
||||
ft_reverserotate_b(stack_b);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
ft_push_a(stack_a, stack_b);
|
||||
stack_size--;
|
||||
ft_swap_a(*stack_a);
|
||||
return ;
|
||||
}
|
||||
if (ft_stacksize(*stack_a) == 3)
|
||||
{
|
||||
}
|
||||
if (ft_stacksize(*stack_a) == 5)
|
||||
{
|
||||
}
|
||||
send_to_stack_b(stack_a, stack_b);
|
||||
while_insert(stack_a, stack_b);
|
||||
while (ft_is_sort(stack_a) == FALSE)
|
||||
ft_rotatestack_a(stack_a);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_min_max.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/11 15:23:26 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 15:59:26 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
size_t get_stack_max(t_stack **stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
size_t i;
|
||||
size_t i_max;
|
||||
int max;
|
||||
|
||||
tmp = *stack;
|
||||
max = tmp->nb;
|
||||
i_max = 0;
|
||||
i = 0;
|
||||
while (tmp)
|
||||
{
|
||||
if (tmp->nb > max)
|
||||
{
|
||||
max = tmp->nb;
|
||||
i_max = i;
|
||||
}
|
||||
tmp = tmp->next;
|
||||
i++;
|
||||
}
|
||||
return (i_max);
|
||||
}
|
||||
|
||||
size_t get_stack_min(t_stack **stack)
|
||||
{
|
||||
size_t i;
|
||||
size_t i_min;
|
||||
int nb_min;
|
||||
t_stack *tmp;
|
||||
|
||||
i = 0;
|
||||
i_min = 0;
|
||||
nb_min = INT_MAX;
|
||||
tmp = *stack;
|
||||
while (tmp)
|
||||
{
|
||||
if (tmp->nb < nb_min)
|
||||
{
|
||||
i_min = i;
|
||||
nb_min = tmp->nb;
|
||||
}
|
||||
tmp = tmp->next;
|
||||
i++;
|
||||
}
|
||||
return (i_min);
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* insertion.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/23 14:49:14 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 16:03:14 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
void prepare_to_send_a(t_stack **stack_b, size_t lowest_cost_b)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (lowest_cost_b < (ft_stacksize(*stack_b) / 2))
|
||||
{
|
||||
while (stack_b && *stack_b && lowest_cost_b > 0)
|
||||
{
|
||||
ft_rotatestack_b(stack_b);
|
||||
lowest_cost_b--;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
i = 0;
|
||||
while (stack_b && *stack_b && lowest_cost_b < ft_stacksize(*stack_b))
|
||||
{
|
||||
ft_reverserotate_b(stack_b);
|
||||
lowest_cost_b++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t get_index(t_stack **stack, int nb)
|
||||
{
|
||||
t_stack *tmp;
|
||||
size_t i;
|
||||
|
||||
i = 1;
|
||||
tmp = (*stack);
|
||||
while (tmp && tmp->next)
|
||||
{
|
||||
if (tmp->nb < nb && tmp->next->nb > nb)
|
||||
return (i);
|
||||
tmp = tmp->next;
|
||||
i++;
|
||||
}
|
||||
if (tmp && nb > tmp->nb && nb < (*stack)->nb)
|
||||
return (0);
|
||||
return (get_stack_min(stack));
|
||||
}
|
||||
|
||||
size_t get_lowest_cost(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
t_stack *tmp;
|
||||
size_t i_lowest_cost;
|
||||
size_t lowest_cost_b;
|
||||
size_t actual_cost_b;
|
||||
|
||||
i_lowest_cost = get_index(stack_a, (*stack_b)->nb);
|
||||
tmp = *stack_b;
|
||||
actual_cost_b = 0;
|
||||
lowest_cost_b = 0;
|
||||
while (tmp)
|
||||
{
|
||||
if ((get_index(stack_a, tmp->nb) < i_lowest_cost))
|
||||
{
|
||||
i_lowest_cost = get_index(stack_a, tmp->nb);
|
||||
lowest_cost_b = actual_cost_b;
|
||||
}
|
||||
actual_cost_b++;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
prepare_to_send_a(stack_b, lowest_cost_b);
|
||||
return (i_lowest_cost);
|
||||
}
|
||||
|
||||
void insert_nb(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
size_t i;
|
||||
size_t new_i_elem;
|
||||
size_t stack_size;
|
||||
|
||||
i = 0;
|
||||
new_i_elem = get_lowest_cost(stack_a, stack_b);
|
||||
stack_size = ft_stacksize(*stack_a);
|
||||
if (new_i_elem < (stack_size / 2))
|
||||
{
|
||||
while (i < new_i_elem)
|
||||
{
|
||||
ft_rotatestack_a(stack_a);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
i = stack_size;
|
||||
while (i > new_i_elem)
|
||||
{
|
||||
ft_reverserotate_a(stack_a);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
ft_push_a(stack_a, stack_b);
|
||||
}
|
||||
|
||||
void while_insert(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
while (*stack_b)
|
||||
{
|
||||
insert_nb(stack_a, stack_b);
|
||||
}
|
||||
}
|
||||
@@ -6,11 +6,11 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/22 11:27:04 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/22 13:27:41 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/03/11 16:05:40 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "../push_swap.h"
|
||||
|
||||
t_boolean is_median(t_stack **stack, int nb)
|
||||
{
|
||||
@@ -42,3 +42,36 @@ int find_median(t_stack **stack)
|
||||
}
|
||||
return (ERROR);
|
||||
}
|
||||
|
||||
t_boolean is_q_one(t_stack **stack, int nb)
|
||||
{
|
||||
t_stack *tmp;
|
||||
int tmp_count;
|
||||
int count;
|
||||
|
||||
tmp = *stack;
|
||||
count = 0;
|
||||
while (tmp)
|
||||
{
|
||||
tmp_count = (tmp->nb < nb) + (tmp->nb < nb) + (tmp->nb < nb);
|
||||
count += tmp_count - (tmp->nb > nb);
|
||||
tmp = tmp->next;
|
||||
}
|
||||
if (count == -1 || count == 0 || count == 1)
|
||||
return (TRUE);
|
||||
return (FALSE);
|
||||
}
|
||||
|
||||
int find_q_one(t_stack **stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
|
||||
tmp = *stack;
|
||||
while (tmp)
|
||||
{
|
||||
if (is_q_one(stack, tmp->nb) == TRUE)
|
||||
return (tmp->nb);
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return (ERROR);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* small_mouv.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/11 16:12:47 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 16:21:54 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
void sort_three(t_stack **stack_a)
|
||||
{
|
||||
if ((*stack_a)->nb > (*stack_a)->next->nb)
|
||||
ft_swap_a(*stack_a);
|
||||
if (get_stack_max(stack_a) == 2)
|
||||
ft_reverserotate_a(stack_a);
|
||||
}
|
||||
@@ -6,40 +6,34 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/15 13:45:25 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/18 18:30:08 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/03/11 15:56:15 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft/libft.h"
|
||||
#include "push_swap.h"
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void ft_check_args_format(char **av)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
char **tmp;
|
||||
char *tmp_av;
|
||||
|
||||
i = 0;
|
||||
av++;
|
||||
while (*av)
|
||||
tmp = av;
|
||||
tmp++;
|
||||
while (*tmp)
|
||||
{
|
||||
j = 0;
|
||||
while (**av)
|
||||
tmp_av = *tmp;
|
||||
while (*tmp_av)
|
||||
{
|
||||
if (!ft_isdigit(**av) && **av != 32 && **av != '-' && **av != '+')
|
||||
if (!ft_isdigit(*tmp_av) && *tmp_av != 32 \
|
||||
&& *tmp_av != '-' && *tmp_av != '+')
|
||||
{
|
||||
ft_putendl_fd("Error", STDERR_FILENO);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
j++;
|
||||
(*av)++;
|
||||
tmp_av++;
|
||||
}
|
||||
(*av) -= j;
|
||||
i++;
|
||||
av++;
|
||||
tmp++;
|
||||
}
|
||||
av -= i;
|
||||
}
|
||||
|
||||
void ft_check_double(t_stack **stack)
|
||||
+40
-4
@@ -6,12 +6,43 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 12:14:22 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/22 13:28:28 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/03/08 14:17:09 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
void ft_check_args(char **av)
|
||||
{
|
||||
char **tmp;
|
||||
char *tmp_av;
|
||||
|
||||
tmp = av;
|
||||
tmp++;
|
||||
while (*tmp)
|
||||
{
|
||||
tmp_av = *tmp;
|
||||
while (*tmp_av)
|
||||
{
|
||||
if ((*tmp_av == '+' || *tmp_av == '-'))
|
||||
{
|
||||
if (!ft_isdigit(*tmp_av + 1))
|
||||
{
|
||||
ft_putendl_fd("Error", STDERR_FILENO);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
else
|
||||
{
|
||||
while (!*tmp_av && ft_isdigit(*tmp_av))
|
||||
tmp_av++;
|
||||
}
|
||||
}
|
||||
tmp_av++;
|
||||
}
|
||||
tmp++;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_stack *stack_a;
|
||||
@@ -23,12 +54,18 @@ int main(int ac, char **av)
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
ft_check_args_format(av);
|
||||
ft_check_args(av);
|
||||
stack_b = NULL;
|
||||
stack_a = ft_parsing(ac, av);
|
||||
stack_a = ft_parsing(av);
|
||||
if (ft_stacksize(stack_a) <= 1)
|
||||
{
|
||||
ft_stackclear(&stack_a);
|
||||
exit(EXIT_FAILURE);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
if (ft_is_sort(&stack_a) == TRUE)
|
||||
{
|
||||
ft_stackclear(&stack_a);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
ft_check_double(&stack_a);
|
||||
ft_algo(&stack_a, &stack_b);
|
||||
@@ -36,4 +73,3 @@ int main(int ac, char **av)
|
||||
ft_stackclear(&stack_b);
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
1 2 3 nope
|
||||
1 3 2 sa ra
|
||||
|
||||
3 2 1 sa rra
|
||||
3 1 2 ra
|
||||
|
||||
2 1 3 sa
|
||||
2 3 1 rra
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/08 18:08:51 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/21 17:55:12 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/03/11 15:10:03 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
void ft_push_a(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
t_stack *tmp_a;
|
||||
|
||||
|
||||
if (!stack_b || !(*stack_b))
|
||||
return ;
|
||||
ft_putendl_fd("pa", STDOUT_FILENO);
|
||||
@@ -38,4 +38,3 @@ void ft_push_b(t_stack **stack_a, t_stack **stack_b)
|
||||
tmp_b->next = *stack_b;
|
||||
*stack_b = tmp_b;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 13:41:25 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/15 13:30:20 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/03/11 15:10:13 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -18,8 +18,6 @@ void ft_reverserotate(t_stack **stack)
|
||||
t_stack *last;
|
||||
t_stack *stack_start;
|
||||
|
||||
if (!stack || !(*stack) || !(*stack)->next)
|
||||
return ;
|
||||
bfrlast = ft_stackbeforelast(*stack);
|
||||
last = ft_stacklast(*stack);
|
||||
bfrlast->next = NULL;
|
||||
@@ -54,4 +52,3 @@ void ft_reverserotate_r(t_stack **stack_a, t_stack **stack_b)
|
||||
ft_reverserotate(stack_a);
|
||||
ft_reverserotate(stack_b);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:53:55 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/15 13:32:27 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/03/11 15:09:40 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -16,7 +16,7 @@ void ft_rotatestack(t_stack **stack)
|
||||
{
|
||||
t_stack *tmp_last;
|
||||
t_stack *start;
|
||||
|
||||
|
||||
if (!stack || !(*stack))
|
||||
return ;
|
||||
start = (*stack)->next;
|
||||
@@ -28,7 +28,7 @@ void ft_rotatestack(t_stack **stack)
|
||||
|
||||
void ft_rotatestack_a(t_stack **stack_a)
|
||||
{
|
||||
if (!stack_a || !(*stack_a))
|
||||
if (!stack_a || !(*stack_a) || !(*stack_a)->next)
|
||||
return ;
|
||||
ft_putendl_fd("ra", STDOUT_FILENO);
|
||||
ft_rotatestack(stack_a);
|
||||
@@ -36,7 +36,7 @@ void ft_rotatestack_a(t_stack **stack_a)
|
||||
|
||||
void ft_rotatestack_b(t_stack **stack_b)
|
||||
{
|
||||
if (!stack_b || !(*stack_b))
|
||||
if (!stack_b || !(*stack_b) || !(*stack_b)->next)
|
||||
return ;
|
||||
ft_putendl_fd("rb", STDOUT_FILENO);
|
||||
ft_rotatestack(stack_b);
|
||||
@@ -44,12 +44,11 @@ void ft_rotatestack_b(t_stack **stack_b)
|
||||
|
||||
void ft_rotatestack_r(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
if (!stack_a || !(*stack_a))
|
||||
if (!stack_a || !(*stack_a) || !(*stack_a)->next)
|
||||
return ;
|
||||
if (!stack_b || !(*stack_b))
|
||||
if (!stack_b || !(*stack_b) || !(*stack_b)->next)
|
||||
return ;
|
||||
ft_putendl_fd("rr", STDOUT_FILENO);
|
||||
ft_rotatestack(stack_a);
|
||||
ft_rotatestack(stack_b);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/08 17:55:21 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/15 13:36:44 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/03/11 15:09:06 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
void ft_swapstack(t_stack *stack)
|
||||
{
|
||||
int tmp;
|
||||
|
||||
|
||||
if (!stack)
|
||||
return ;
|
||||
tmp = stack->nb;
|
||||
+32
-15
@@ -6,12 +6,26 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 12:05:43 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/08 17:41:41 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/03/11 15:58:28 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
size_t ft_nbrlen(char *s)
|
||||
{
|
||||
char *tmp;
|
||||
char *nbrlen;
|
||||
|
||||
tmp = s;
|
||||
while (*tmp || *tmp == '-' || *tmp == '+' || *tmp == '0')
|
||||
tmp++;
|
||||
nbrlen = tmp;
|
||||
while (*nbrlen)
|
||||
nbrlen++;
|
||||
return (nbrlen - tmp);
|
||||
}
|
||||
|
||||
void ft_freearr(char **arr)
|
||||
{
|
||||
size_t i;
|
||||
@@ -25,27 +39,30 @@ void ft_freearr(char **arr)
|
||||
free(arr);
|
||||
}
|
||||
|
||||
t_stack *ft_parsing(int ac, char **av)
|
||||
t_stack *ft_parsing(char **av)
|
||||
{
|
||||
t_stack *parsed_data;
|
||||
t_stack *parsed_data;
|
||||
char **tmp_split;
|
||||
size_t i;
|
||||
char **split;
|
||||
char **tmp_av;
|
||||
|
||||
i = 0;
|
||||
(void)ac;
|
||||
av++;
|
||||
tmp_av = av;
|
||||
tmp_av++;
|
||||
parsed_data = NULL;
|
||||
while (*av)
|
||||
while (*tmp_av)
|
||||
{
|
||||
i = 0;
|
||||
tmp_split = ft_split(*av, ' ');
|
||||
while (tmp_split[i])
|
||||
split = ft_split(*tmp_av, ' ');
|
||||
tmp_split = split;
|
||||
while (*tmp_split)
|
||||
{
|
||||
ft_stackadd_back(&parsed_data, ft_stacknew(ft_atoi(tmp_split[i])));
|
||||
i++;
|
||||
if (ft_nbrlen(*tmp_split) > 11 || ft_atoll(*tmp_split) > 2147483647 \
|
||||
|| ft_atoll(*tmp_split) < -2147483648)
|
||||
ft_senderror();
|
||||
ft_stackadd_back(&parsed_data, ft_stacknew(ft_atoi(*tmp_split)));
|
||||
tmp_split++;
|
||||
}
|
||||
ft_freearr(tmp_split);
|
||||
av++;
|
||||
ft_freearr(split);
|
||||
tmp_av++;
|
||||
}
|
||||
return (parsed_data);
|
||||
}
|
||||
@@ -6,11 +6,10 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/15 11:05:52 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/15 11:07:42 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/03/04 12:36:31 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft/libft.h"
|
||||
#include "push_swap.h"
|
||||
|
||||
void ft_print_stack(t_stack *stack)
|
||||
@@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* push_swap.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 12:18:29 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 16:07:50 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PUSH_SWAP_H
|
||||
# define PUSH_SWAP_H
|
||||
|
||||
# include "../libft/libft.h"
|
||||
|
||||
typedef struct s_stack
|
||||
{
|
||||
int nb;
|
||||
struct s_stack *next;
|
||||
} t_stack;
|
||||
|
||||
// stack
|
||||
void ft_stackadd_back(t_stack **stack, t_stack *new);
|
||||
void ft_stackadd_front(t_stack **stack, t_stack *new);
|
||||
void ft_stackclear(t_stack **stack);
|
||||
void ft_stackdelone(t_stack *stack);
|
||||
t_stack *ft_stacklast(t_stack *stack);
|
||||
t_stack *ft_stacknew(int content);
|
||||
size_t ft_stacksize(t_stack *stack);
|
||||
t_stack *ft_stackbeforelast(t_stack *stack);
|
||||
|
||||
// operations
|
||||
void ft_swap_a(t_stack *stack_a);
|
||||
void ft_swap_b(t_stack *stack_b);
|
||||
void ft_stack_ss(t_stack *stack_a, t_stack *stack_b);
|
||||
|
||||
void ft_push_a(t_stack **stack_a, t_stack **stack_b);
|
||||
void ft_push_b(t_stack **stack_a, t_stack **stack_b);
|
||||
|
||||
void ft_rotatestack_a(t_stack **stack_a);
|
||||
void ft_rotatestack_b(t_stack **stack_b);
|
||||
void ft_rotatestack_r(t_stack **stack_a, t_stack **stack_b);
|
||||
|
||||
void ft_reverserotate_a(t_stack **stack_a);
|
||||
void ft_reverserotate_b(t_stack **stack_b);
|
||||
void ft_reverserotate_r(t_stack **stack_a, t_stack **stack_b);
|
||||
|
||||
// algo
|
||||
void ft_algo(t_stack **stack_a, t_stack **stack_b);
|
||||
int find_median(t_stack **stack);
|
||||
void insert_nb(t_stack **stack_a, t_stack **stack_b);
|
||||
void while_insert(t_stack **stack_a, t_stack **stack_b);
|
||||
size_t get_lowest_cost(t_stack **stack_a, t_stack **stack_b);
|
||||
t_boolean ft_is_sort(t_stack **stack);
|
||||
int find_q_one(t_stack **stack);
|
||||
|
||||
// get_min_max
|
||||
size_t get_stack_max(t_stack **stack);
|
||||
size_t get_stack_min(t_stack **stack);
|
||||
|
||||
// error checkin
|
||||
void ft_check_args_format(char **av);
|
||||
t_stack *ft_parsing(char **av);
|
||||
void ft_check_double(t_stack **stack);
|
||||
void ft_print_stack(t_stack *stack);
|
||||
|
||||
// utils
|
||||
t_boolean ft_is_sort(t_stack **stack);
|
||||
void ft_senderror(void);
|
||||
|
||||
#endif
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/05 11:42:40 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/22 13:29:39 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/03/07 11:30:13 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* is_sorted.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/11 15:39:00 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 15:41:20 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
t_boolean ft_is_sort(t_stack **stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
|
||||
tmp = *stack;
|
||||
while (tmp->next && tmp->nb < tmp->next->nb)
|
||||
tmp = tmp->next;
|
||||
if (!tmp->next)
|
||||
return (TRUE);
|
||||
return (FALSE);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* print_error.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/11 15:47:05 by adjoly #+# #+# */
|
||||
/* Updated: 2024/03/11 15:54:09 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
void ft_senderror(void)
|
||||
{
|
||||
ft_putendl_fd("Error", STDOUT_FILENO);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
Reference in new issue
Block a user