mirror of
https://github.com/DavidGailleton/42-Push_Swap.git
synced 2026-01-27 00:41:57 +00:00
Compare commits
49 Commits
stack_func
...
radix
| Author | SHA1 | Date | |
|---|---|---|---|
| 27a6a4806b | |||
| c799dd65ee | |||
| 2c6d8d4095 | |||
|
|
6942548108 | ||
|
|
17f9a5affd | ||
|
|
e1351b4d2f | ||
|
|
93e8c10b27 | ||
|
|
0d9b46c748 | ||
|
|
95577e5f02 | ||
|
|
ca732b1b97 | ||
|
|
2b255b94da | ||
|
|
a78b3f1a44 | ||
|
|
4bb0b71141 | ||
|
|
40ec288032 | ||
|
|
54db222c48 | ||
|
|
ef9f16a142 | ||
|
|
80179a5289 | ||
|
|
508b63d296 | ||
|
|
805d592160 | ||
|
|
f98dce6f61 | ||
|
|
ae4740c479 | ||
|
|
dfdcd68549 | ||
|
|
a44b878625 | ||
|
|
e06a51e034 | ||
|
|
85b83f3cc7 | ||
|
|
96a0a6d8f0 | ||
|
|
e563d663ec | ||
|
|
e2495fa62f | ||
|
|
e9c90e447c | ||
|
|
0d01b58ff1 | ||
|
|
a68bcd2503 | ||
|
|
55363e0e6f | ||
|
|
f5da9fac1f | ||
|
|
a7cbd18db6 | ||
|
|
48c13b8ef8 | ||
|
|
5372b944a4 | ||
|
|
b6c1dc73a8 | ||
|
|
977a4a4b1a | ||
|
|
1463f3e14a | ||
|
|
c781d6f5cd | ||
|
|
ad4275c206 | ||
|
|
c9066f4a9b | ||
|
|
8cd29f7151 | ||
|
|
30b306d2c2 | ||
|
|
8e8ca5cfdc | ||
|
|
15d56b2b32 | ||
|
|
c521cff21a | ||
|
|
9dc3d88d08 | ||
|
|
d4d8a79d30 |
9
.gitignore
vendored
9
.gitignore
vendored
@@ -53,3 +53,12 @@ dkms.conf
|
||||
|
||||
# debug information files
|
||||
*.dwo
|
||||
|
||||
# Vim swap files
|
||||
*.swp
|
||||
|
||||
# Executable
|
||||
push_swap
|
||||
|
||||
# File obj
|
||||
obj/
|
||||
|
||||
112
Makefile
Normal file
112
Makefile
Normal file
@@ -0,0 +1,112 @@
|
||||
#============================
|
||||
# ALL FOLDERS
|
||||
#============================
|
||||
|
||||
STACK_UTILS_DIR = stack_utils
|
||||
|
||||
ALGO_UTILS_DIR = algorithms/utils
|
||||
|
||||
INSERT_DIR = algorithms/insertion
|
||||
|
||||
ALGO_DIR = algorithms
|
||||
|
||||
PARS_DIR = parsing
|
||||
|
||||
MEDIUM_DIR = medium
|
||||
|
||||
COMPLEX_DIR = radix
|
||||
|
||||
FLAGS_DIR = flags
|
||||
|
||||
INCLUDES = headers
|
||||
|
||||
#============================
|
||||
# ALL FILES WITHOUT PATH
|
||||
#============================
|
||||
|
||||
SRC = main.c test_one.c
|
||||
|
||||
INSERTION = insertion.c
|
||||
|
||||
FLAGS_FILES = algorithms_sort.c flag.c
|
||||
|
||||
PARSING = ft_atoi.c parsing.c ft_strncmp.c
|
||||
|
||||
STACK_UTILS = push.c rev_rotate.c rotate.c stack_add.c stack_remove.c stacks_len.c swap.c print_stacks.c
|
||||
|
||||
MEDIUM_ALGO = utils_medium.c utils_struct_tab.c utils_medium_two.c sort_utils.c sort_utils_two.c medium_algo.c
|
||||
|
||||
COMPLEX_ALGO = radix.c
|
||||
|
||||
ALGO_UTILS = check_order.c compare.c iterate.c pre_sort.c
|
||||
|
||||
#============================
|
||||
# ADDING PATH TO THE FILES
|
||||
#============================
|
||||
|
||||
ALL_FILES = $(SRC) $(STACK_UTILS_DIR)/$(STACK_UTILS) $(PARS_DIR)/$(PARSING) \
|
||||
$(ALGO_DIR)/$(MEDIUM_DIR)/$(MEDIUM_ALGO) $(ALGO_UTILS_DIR)/$(ALGO_UTILS) \
|
||||
$(INSERT_DIR)/$(INSERTION) $(ALGO_DIR)/$(COMPLEX_DIR)/$(COMPLEX_ALGO)
|
||||
|
||||
OBJ_DIR = obj
|
||||
|
||||
CC = cc
|
||||
|
||||
CFLAGS = -Wall -Werror -Wextra -g3 -I$(INCLUDES)
|
||||
|
||||
NAME = push_swap
|
||||
|
||||
OBJ = $(addprefix $(OBJ_DIR)/, $(notdir $(ALL_FILES:.c=.o)))
|
||||
DEP = $(OBJ:.o=.d)
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJ)
|
||||
@$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
|
||||
@echo "===================================="
|
||||
@echo "======= PUSH SWAP COMPILED ========="
|
||||
@echo "===================================="
|
||||
|
||||
$(OBJ_DIR)/%.o: %.c | $(OBJ_DIR)
|
||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||
|
||||
$(OBJ_DIR)/%.o: $(PARS_DIR)/%.c | $(OBJ_DIR)
|
||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||
|
||||
$(OBJ_DIR)/%.o: $(INSERT_DIR)/%.c | $(OBJ_DIR)
|
||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||
|
||||
$(OBJ_DIR)/%.o: $(STACK_UTILS_DIR)/%.c | $(OBJ_DIR)
|
||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||
|
||||
$(OBJ_DIR)/%.o: $(ALGO_DIR)/%.c | $(OBJ_DIR)
|
||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||
|
||||
$(OBJ_DIR)/%.o: $(ALGO_DIR)/$(MEDIUM_DIR)/%.c | $(OBJ_DIR)
|
||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||
|
||||
$(OBJ_DIR)/%.o: $(ALGO_DIR)/$(COMPLEX_DIR)/%.c | $(OBJ_DIR)
|
||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||
|
||||
$(OBJ_DIR)/%.o: $(ALGO_UTILS_DIR)/%.c | $(OBJ_DIR)
|
||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||
|
||||
$(OBJ_DIR):
|
||||
@mkdir -p $(OBJ_DIR)
|
||||
|
||||
clean:
|
||||
@rm -rf $(OBJ_DIR)
|
||||
@echo "===================================="
|
||||
@echo "= ALL OBJECT AND DEPENDENCES CLEAN ="
|
||||
@echo "===================================="
|
||||
|
||||
fclean: clean
|
||||
@rm -f $(NAME)
|
||||
@echo "========== EXEC DELETED ============"
|
||||
@echo "===================================="
|
||||
|
||||
re: fclean all
|
||||
|
||||
-include $(DEP)
|
||||
5
README.md
Normal file
5
README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
This project has been created as part of the 42 curriculum by mteriier[, dgaillet].
|
||||
|
||||
## Description
|
||||
|
||||
The *Push swap* project goal is to sort values with push swap operations beetween 2 stack (*a* and *b*)
|
||||
72
algorithms/insertion/insertion.c
Normal file
72
algorithms/insertion/insertion.c
Normal file
@@ -0,0 +1,72 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* insertion.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/10 15:38:52 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/12/15 14:45:54 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
static int to_insert(t_stacks *stacks, int sorted)
|
||||
{
|
||||
int i;
|
||||
t_stack *a;
|
||||
|
||||
if (is_lowest(stacks->a, stacks->b, sorted)
|
||||
|| is_highest(stacks->a, stacks->b, sorted))
|
||||
return (sorted - r_to_lowest(stacks->a, sorted));
|
||||
i = 0;
|
||||
a = stacks->a;
|
||||
while (i < sorted)
|
||||
{
|
||||
if (stacks->b->value > a->previous->value
|
||||
&& stacks->b->value <= a->value)
|
||||
return (i);
|
||||
a = a->previous;
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
static void ft_extra(t_stacks *stacks, int sorted, int b_len)
|
||||
{
|
||||
int to_r_rotate;
|
||||
|
||||
if (b_len <= 0)
|
||||
return ;
|
||||
if (sorted <= 1)
|
||||
pa(stacks);
|
||||
else
|
||||
{
|
||||
to_r_rotate = to_insert(stacks, sorted);
|
||||
optimal_rotate(stacks, sorted - to_r_rotate, sorted, 'a');
|
||||
pa(stacks);
|
||||
}
|
||||
ft_extra(stacks, sorted + 1, b_len - 1);
|
||||
}
|
||||
|
||||
void insertion(t_stacks *stacks, int len)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (len == 3)
|
||||
{
|
||||
sort_three_a(stacks);
|
||||
return ;
|
||||
}
|
||||
if (check_order(stacks->a))
|
||||
return ;
|
||||
i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
pb(stacks);
|
||||
i++;
|
||||
}
|
||||
ft_extra(stacks, 0, len);
|
||||
optimal_rotate(stacks, r_to_lowest(stacks->a, len), len, 'a');
|
||||
}
|
||||
114
algorithms/medium/medium_algo.c
Normal file
114
algorithms/medium/medium_algo.c
Normal file
@@ -0,0 +1,114 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* medium_algo.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/10 07:14:45 by mteriier #+# #+# */
|
||||
/* Updated: 2026/01/06 12:57:05 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "medium_headers.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static int path_to_end(t_stacks *piles, int max_range, int range, char c)
|
||||
{
|
||||
t_stack *tmp;
|
||||
int i;
|
||||
int first_pass;
|
||||
|
||||
if (c == 'a')
|
||||
tmp = piles->a;
|
||||
else
|
||||
tmp = piles->b;
|
||||
tmp = tmp->previous;
|
||||
i = 0;
|
||||
first_pass = 1;
|
||||
while (first_pass || tmp != piles->b->previous)
|
||||
{
|
||||
first_pass = 0;
|
||||
if (in_range(tmp->value, max_range, range))
|
||||
tmp = piles->b;
|
||||
tmp = tmp->previous;
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
static int path_to_start(t_stacks *piles, int max_range, int range, char c)
|
||||
{
|
||||
t_stack *tmp;
|
||||
int i;
|
||||
int first_pass;
|
||||
|
||||
if (c == 'a')
|
||||
tmp = piles->a;
|
||||
else
|
||||
tmp = piles->b;
|
||||
i = 0;
|
||||
first_pass = 1;
|
||||
while (first_pass || tmp != piles->b)
|
||||
{
|
||||
first_pass = 0;
|
||||
if (in_range(tmp->value, max_range, range))
|
||||
{
|
||||
tmp = piles->b->previous;
|
||||
}
|
||||
tmp = tmp->next;
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
int wich_path(t_stacks *piles, int max_range, int range, char c)
|
||||
{
|
||||
int path_start;
|
||||
int path_end;
|
||||
|
||||
path_start = path_to_start(piles, max_range, range, c);
|
||||
path_end = path_to_end(piles, max_range, range, c);
|
||||
if (path_start < path_end)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int stack_len(t_stack *stack)
|
||||
{
|
||||
t_stack *first;
|
||||
int i;
|
||||
|
||||
first = stack;
|
||||
i = 1;
|
||||
while (first->next != stack)
|
||||
{
|
||||
first = first->next;
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
void bucket_algo(t_stacks *piles, t_tab *preset, int range)
|
||||
{
|
||||
t_tab *tmp;
|
||||
|
||||
tmp = preset;
|
||||
while (piles->a)
|
||||
pb(piles);
|
||||
while (preset)
|
||||
{
|
||||
push_range_to_b(piles, preset, range);
|
||||
if (preset->next)
|
||||
tmp = preset->next;
|
||||
else
|
||||
tmp = NULL;
|
||||
if (preset)
|
||||
free(preset);
|
||||
preset = tmp;
|
||||
}
|
||||
while (piles->b)
|
||||
pa(piles);
|
||||
return ;
|
||||
}
|
||||
95
algorithms/medium/sort_utils.c
Normal file
95
algorithms/medium/sort_utils.c
Normal file
@@ -0,0 +1,95 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/05 14:11:49 by mteriier #+# #+# */
|
||||
/* Updated: 2026/01/05 14:11:52 by mteriier ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "medium_headers.h"
|
||||
|
||||
static int number_move_reverse(t_stacks *piles, int value, char start_end)
|
||||
{
|
||||
int i;
|
||||
t_stack *tmp;
|
||||
|
||||
i = 0;
|
||||
tmp = piles->a;
|
||||
if (start_end == 's')
|
||||
{
|
||||
while (tmp->value < value)
|
||||
{
|
||||
tmp = tmp->next;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp = tmp->previous;
|
||||
while (tmp->value > value)
|
||||
{
|
||||
tmp = tmp->previous;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
static int sort_path(t_stacks *piles, int value)
|
||||
{
|
||||
int start_path;
|
||||
int end_path;
|
||||
|
||||
start_path = number_move_reverse(piles, value, 's');
|
||||
if (start_path == 0)
|
||||
return (1);
|
||||
end_path = number_move_reverse(piles, value, 'e');
|
||||
if (start_path < end_path)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void sort_little_pile(t_stacks *piles)
|
||||
{
|
||||
if (!piles->a)
|
||||
{
|
||||
pa(piles);
|
||||
return ;
|
||||
}
|
||||
if (piles->a->previous->value < piles->b->value)
|
||||
{
|
||||
pa(piles);
|
||||
ra(piles);
|
||||
return ;
|
||||
}
|
||||
if (sort_path(piles, piles->b->value))
|
||||
sort_from_left(piles);
|
||||
else
|
||||
sort_from_right(piles);
|
||||
}
|
||||
|
||||
void push_range_to_b(t_stacks *piles, t_tab *one_preset, int range)
|
||||
{
|
||||
while (one_preset->nb_in > 0)
|
||||
{
|
||||
if (wich_path(piles, one_preset->max_range, range, 'b'))
|
||||
{
|
||||
while (!in_range(piles->b->value, one_preset->max_range, range))
|
||||
rb(piles);
|
||||
}
|
||||
else
|
||||
{
|
||||
while (!in_range(piles->b->value, one_preset->max_range, range))
|
||||
{
|
||||
rrb(piles);
|
||||
}
|
||||
}
|
||||
sort_little_pile(piles);
|
||||
one_preset->nb_in--;
|
||||
}
|
||||
}
|
||||
49
algorithms/medium/sort_utils_two.c
Normal file
49
algorithms/medium/sort_utils_two.c
Normal file
@@ -0,0 +1,49 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort_utils_two.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/06 07:52:36 by mteriier #+# #+# */
|
||||
/* Updated: 2026/01/06 07:52:38 by mteriier ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
void sort_from_left(t_stacks *piles)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (piles->b->value > piles->a->value)
|
||||
{
|
||||
ra(piles);
|
||||
i++;
|
||||
}
|
||||
pa(piles);
|
||||
while (i > 0)
|
||||
{
|
||||
rra(piles);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
void sort_from_right(t_stacks *piles)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (piles->b->value < piles->a->previous->value)
|
||||
{
|
||||
rra(piles);
|
||||
i++;
|
||||
}
|
||||
pa(piles);
|
||||
while (i >= 0)
|
||||
{
|
||||
ra(piles);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
96
algorithms/medium/utils_medium.c
Normal file
96
algorithms/medium/utils_medium.c
Normal file
@@ -0,0 +1,96 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils_medium.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/10 13:58:40 by mteriier #+# #+# */
|
||||
/* Updated: 2025/12/10 13:58:42 by mteriier ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
int get_first_lower(t_stack *first)
|
||||
{
|
||||
t_stack *tmp;
|
||||
int lower;
|
||||
|
||||
tmp = first;
|
||||
lower = tmp->value;
|
||||
while (tmp->next != first)
|
||||
{
|
||||
if (lower > tmp->value)
|
||||
lower = tmp->value;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return (lower);
|
||||
}
|
||||
|
||||
int get_next_lower(t_stack *first, int old_lower)
|
||||
{
|
||||
t_stack *tmp;
|
||||
int next_lower;
|
||||
int skip_first;
|
||||
|
||||
tmp = first;
|
||||
skip_first = 1;
|
||||
next_lower = 2147483647;
|
||||
while (tmp != first || skip_first)
|
||||
{
|
||||
skip_first = 0;
|
||||
if (old_lower < tmp->value && tmp->value <= next_lower)
|
||||
{
|
||||
next_lower = tmp->value;
|
||||
}
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return (next_lower);
|
||||
}
|
||||
|
||||
int calcul_range(int value, int range)
|
||||
{
|
||||
int max_range;
|
||||
|
||||
max_range = 0;
|
||||
if (value < 0)
|
||||
while (max_range > value)
|
||||
max_range -= range;
|
||||
else
|
||||
while (max_range <= value)
|
||||
max_range += range;
|
||||
if (max_range < 0)
|
||||
return (max_range + range - 1);
|
||||
return (max_range - 1);
|
||||
}
|
||||
|
||||
int in_range(int value, int max_range, int range)
|
||||
{
|
||||
int min_range;
|
||||
|
||||
min_range = max_range - (range - 1);
|
||||
if (value <= max_range && value >= min_range)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int get_number_in_range(int max_range, t_stack *a, int range)
|
||||
{
|
||||
int nb_in;
|
||||
t_stack *tmp;
|
||||
t_stack *first;
|
||||
|
||||
nb_in = 0;
|
||||
tmp = a;
|
||||
first = tmp;
|
||||
while (tmp->next != first)
|
||||
{
|
||||
if (in_range(tmp->value, max_range, range))
|
||||
nb_in++;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
if (in_range(tmp->value, max_range, range))
|
||||
nb_in++;
|
||||
return (nb_in);
|
||||
}
|
||||
74
algorithms/medium/utils_medium_two.c
Normal file
74
algorithms/medium/utils_medium_two.c
Normal file
@@ -0,0 +1,74 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils_medium_two.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.lyon42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/22 09:36:56 by mteriier #+# #+# */
|
||||
/* Updated: 2025/12/22 09:38:19 by mteriier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "medium_headers.h"
|
||||
|
||||
static int get_max_number(t_stack *first)
|
||||
{
|
||||
int max;
|
||||
t_stack *a;
|
||||
|
||||
a = first;
|
||||
max = a->value;
|
||||
while (a->next != first)
|
||||
{
|
||||
if (max < a->value)
|
||||
max = a->value;
|
||||
a = a->next;
|
||||
}
|
||||
return (max);
|
||||
}
|
||||
|
||||
static int get_min_number(t_stack *first)
|
||||
{
|
||||
int min;
|
||||
t_stack *a;
|
||||
|
||||
a = first;
|
||||
min = a->value;
|
||||
while (a->next != first)
|
||||
{
|
||||
if (min > a->value)
|
||||
min = a->value;
|
||||
a = a->next;
|
||||
}
|
||||
return (min);
|
||||
}
|
||||
|
||||
static int my_sqrt(int nb)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (nb < 1)
|
||||
return (0);
|
||||
i = 0;
|
||||
while (i * i <= nb)
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
int range_bucket(t_stack *first)
|
||||
{
|
||||
int len;
|
||||
int diff;
|
||||
int sqrt;
|
||||
|
||||
len = stack_len(first);
|
||||
diff = get_max_number(first) - get_min_number(first);
|
||||
sqrt = my_sqrt(len);
|
||||
if (diff / sqrt < 2)
|
||||
{
|
||||
return (get_max_number(first));
|
||||
}
|
||||
return ((get_max_number(first) - get_min_number(first)) / my_sqrt(len));
|
||||
}
|
||||
91
algorithms/medium/utils_struct_tab.c
Normal file
91
algorithms/medium/utils_struct_tab.c
Normal file
@@ -0,0 +1,91 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils_struct_tab.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/10 13:36:43 by mteriier #+# #+# */
|
||||
/* Updated: 2025/12/10 13:36:48 by mteriier ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "medium_headers.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static t_tab *allocate_tab(int range_max, int nb)
|
||||
{
|
||||
t_tab *tab;
|
||||
|
||||
tab = malloc(sizeof(t_tab));
|
||||
if (!tab)
|
||||
return (NULL);
|
||||
tab->next = NULL;
|
||||
tab->max_range = range_max;
|
||||
tab->nb_in = nb;
|
||||
return (tab);
|
||||
}
|
||||
|
||||
static t_tab *free_tab(t_tab **first)
|
||||
{
|
||||
if (!(*first))
|
||||
return (NULL);
|
||||
if ((*first)->next)
|
||||
free_tab(&(*first)->next);
|
||||
free(*first);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static t_tab *init_first_tab(t_stack *first, int range)
|
||||
{
|
||||
t_tab *tab;
|
||||
int lower;
|
||||
int range_max;
|
||||
|
||||
lower = get_first_lower(first);
|
||||
range_max = calcul_range(lower, range);
|
||||
tab = allocate_tab(range_max, get_number_in_range(range_max, first, range));
|
||||
if (!tab)
|
||||
return (NULL);
|
||||
return (tab);
|
||||
}
|
||||
|
||||
static t_tab *get_next_tab(t_stack *first, t_tab *tab, int range)
|
||||
{
|
||||
int lower;
|
||||
int range_max;
|
||||
t_tab *next_tab;
|
||||
|
||||
lower = get_next_lower(first, tab->max_range);
|
||||
range_max = calcul_range(lower, range);
|
||||
next_tab = allocate_tab(range_max,
|
||||
get_number_in_range(range_max, first, range));
|
||||
if (!next_tab)
|
||||
return (NULL);
|
||||
return (next_tab);
|
||||
}
|
||||
|
||||
t_tab *get_tabs(t_stack *first, int range)
|
||||
{
|
||||
t_tab *tmp;
|
||||
t_tab *first_tab;
|
||||
int len_stack;
|
||||
int scan_nb_in_tab;
|
||||
|
||||
len_stack = stack_len(first);
|
||||
first_tab = init_first_tab(first, range);
|
||||
if (!first_tab)
|
||||
return (NULL);
|
||||
scan_nb_in_tab = first_tab->nb_in;
|
||||
tmp = first_tab;
|
||||
while (scan_nb_in_tab < len_stack)
|
||||
{
|
||||
tmp->next = get_next_tab(first, tmp, range);
|
||||
if (!(tmp->next))
|
||||
return (free_tab(&first_tab));
|
||||
tmp = tmp->next;
|
||||
scan_nb_in_tab += tmp->nb_in;
|
||||
}
|
||||
return (first_tab);
|
||||
}
|
||||
76
algorithms/radix/radix.c
Normal file
76
algorithms/radix/radix.c
Normal file
@@ -0,0 +1,76 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* radix.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/06 12:47:06 by dgaillet #+# #+# */
|
||||
/* Updated: 2026/01/08 11:06:11 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
static int still_unit_value(t_stacks *stacks, int unit)
|
||||
{
|
||||
t_stack *temp;
|
||||
|
||||
temp = stacks->a;
|
||||
if (!temp || temp->value >= unit)
|
||||
return (1);
|
||||
temp = temp->next;
|
||||
while (temp != stacks->a)
|
||||
{
|
||||
if (temp->value >= unit)
|
||||
return (1);
|
||||
temp = temp->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void push_by_number_to_b(t_stacks *stacks, int unit, int nb)
|
||||
{
|
||||
int i;
|
||||
int s_len;
|
||||
t_stack *temp;
|
||||
|
||||
temp = stacks->a;
|
||||
s_len = 1;
|
||||
while (temp && temp->next != stacks->a)
|
||||
{
|
||||
s_len++;
|
||||
temp = temp->next;
|
||||
}
|
||||
i = 0;
|
||||
while (i < s_len && still_unit_value(stacks, unit))
|
||||
{
|
||||
if (stacks->a && (stacks->a->value % (unit * 10)) / unit == nb)
|
||||
pb(stacks);
|
||||
else
|
||||
ra(stacks);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
static void rec_sort(t_stacks *stacks, int unit)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (!still_unit_value(stacks, unit))
|
||||
return ;
|
||||
while (i <= 9)
|
||||
{
|
||||
push_by_number_to_b(stacks, unit, i);
|
||||
i++;
|
||||
}
|
||||
while (stacks->b)
|
||||
pa(stacks);
|
||||
rec_sort(stacks, unit * 10);
|
||||
}
|
||||
|
||||
void radix(t_stacks *stacks)
|
||||
{
|
||||
rec_sort(stacks, 1);
|
||||
}
|
||||
27
algorithms/utils/check_order.c
Normal file
27
algorithms/utils/check_order.c
Normal file
@@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* check_order.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/09 11:57:06 by mteriier #+# #+# */
|
||||
/* Updated: 2025/12/11 17:50:21 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
int check_order(t_stack *stack)
|
||||
{
|
||||
t_stack *first;
|
||||
|
||||
first = stack;
|
||||
while (stack->next != first)
|
||||
{
|
||||
if (stack->value > stack->next->value)
|
||||
return (0);
|
||||
stack = stack->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
43
algorithms/utils/compare.c
Normal file
43
algorithms/utils/compare.c
Normal file
@@ -0,0 +1,43 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* compare.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/11 17:46:00 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/12/11 17:46:22 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
int is_lowest(t_stack *stack, t_stack *node, int len)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
if (node->value > stack->value)
|
||||
return (0);
|
||||
stack = stack->next;
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int is_highest(t_stack *stack, t_stack *node, int len)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
if (node->value < stack->value)
|
||||
return (0);
|
||||
stack = stack->next;
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
42
algorithms/utils/iterate.c
Normal file
42
algorithms/utils/iterate.c
Normal file
@@ -0,0 +1,42 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* iterate.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/11 17:49:56 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/12/15 14:44:56 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
static void iterate_fn(t_stacks *stacks, int i, void (f)(t_stacks *stacks))
|
||||
{
|
||||
while (i > 0)
|
||||
{
|
||||
f(stacks);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
void optimal_rotate(t_stacks *stacks, int i, int len, char stack)
|
||||
{
|
||||
if (i == len)
|
||||
return ;
|
||||
if (i && len / i >= 2)
|
||||
{
|
||||
if (stack == 'a')
|
||||
iterate_fn(stacks, i, &ra);
|
||||
else
|
||||
iterate_fn(stacks, i, &rb);
|
||||
}
|
||||
else if (i)
|
||||
{
|
||||
if (stack == 'a')
|
||||
iterate_fn(stacks, len - i, &rra);
|
||||
else
|
||||
iterate_fn(stacks, len - i, &rrb);
|
||||
}
|
||||
}
|
||||
51
algorithms/utils/pre_sort.c
Normal file
51
algorithms/utils/pre_sort.c
Normal file
@@ -0,0 +1,51 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pre_sort.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/15 14:21:20 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/12/15 14:59:52 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
int r_to_lowest(t_stack *stack, int len)
|
||||
{
|
||||
t_stack *lowest;
|
||||
int lowest_i;
|
||||
int i;
|
||||
|
||||
i = 1;
|
||||
lowest_i = 0;
|
||||
lowest = stack;
|
||||
stack = stack->next;
|
||||
while (i < len)
|
||||
{
|
||||
if (stack->value < lowest->value)
|
||||
{
|
||||
lowest_i = i;
|
||||
lowest = stack;
|
||||
}
|
||||
stack = stack->next;
|
||||
i++;
|
||||
}
|
||||
return (lowest_i);
|
||||
}
|
||||
|
||||
void sort_three_a(t_stacks *stacks)
|
||||
{
|
||||
if (check_order(stacks->a))
|
||||
return ;
|
||||
if (stacks->a->value > stacks->a->next->value)
|
||||
sa(stacks);
|
||||
optimal_rotate(stacks, r_to_lowest(stacks->a, 3), 3, 'a');
|
||||
if (stacks->a->next->value > stacks->a->previous->value)
|
||||
{
|
||||
ra(stacks);
|
||||
sa(stacks);
|
||||
rra(stacks);
|
||||
}
|
||||
}
|
||||
42
flags/algorithms_sort.c
Normal file
42
flags/algorithms_sort.c
Normal file
@@ -0,0 +1,42 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* algorithms_sort.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/07 12:15:02 by mteriier #+# #+# */
|
||||
/* Updated: 2026/01/07 12:15:05 by mteriier ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "medium_algo.h"
|
||||
|
||||
void simple(t_stacks *piles)
|
||||
{
|
||||
int len;
|
||||
|
||||
len = stack_a_len(piles);
|
||||
insertion(piles, len);
|
||||
}
|
||||
|
||||
void medium(t_stacks *piles)
|
||||
{
|
||||
t_tab *buckets;
|
||||
int range;
|
||||
|
||||
range = range_bucket(piles->a);
|
||||
buckets = get_tabs(piles->a, range);
|
||||
bucket_algo(piles, buckets, range);
|
||||
}
|
||||
|
||||
void complex(t_stacks *piles)
|
||||
{
|
||||
return ;
|
||||
}
|
||||
|
||||
void adaptive(t_stacks *piles)
|
||||
{
|
||||
return ;
|
||||
}
|
||||
28
flags/flag.c
Normal file
28
flags/flag.c
Normal file
@@ -0,0 +1,28 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* flag.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/07 12:39:29 by mteriier #+# #+# */
|
||||
/* Updated: 2026/01/07 12:39:31 by mteriier ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "parsing.h"
|
||||
|
||||
void flags(int pos, char **argv, t_stacks *piles)
|
||||
{
|
||||
if (ft_strncmp(argv[pos], "--simple", 30) && pos > 0)
|
||||
simple(piles);
|
||||
else if (ft_strncmp(argv[pos], "--medium", 30) && pos > 0)
|
||||
medium(piles);
|
||||
else if (ft_strncmp(argv[pos], "--complex", 30) && pos > 0)
|
||||
complex(piles);
|
||||
else if (ft_strncmp(argv[pos], "--adaptive", 30) && pos > 0)
|
||||
adaptive(piles);
|
||||
else
|
||||
adaptive(piles);
|
||||
}
|
||||
22
headers/flags.h
Normal file
22
headers/flags.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* flags.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/07 13:05:52 by mteriier #+# #+# */
|
||||
/* Updated: 2026/01/07 13:05:53 by mteriier ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FLAGS_H
|
||||
# define FLAGS_H
|
||||
|
||||
void simple(t_stacks *piles);
|
||||
void medium(t_stacks *piles);
|
||||
void complex(t_stacks *piles);
|
||||
void adaptive(t_stacks *piles);
|
||||
void flags(int pos, char **argv, t_stacks *piles);
|
||||
|
||||
#endif
|
||||
41
headers/medium_headers.h
Normal file
41
headers/medium_headers.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* medium_headers.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/07 07:47:49 by mteriier #+# #+# */
|
||||
/* Updated: 2026/01/07 07:47:51 by mteriier ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MEDIUM_HEADERS_H
|
||||
# define MEDIUM_HEADERS_H
|
||||
|
||||
typedef struct s_tab
|
||||
{
|
||||
int max_range;
|
||||
int nb_in;
|
||||
struct s_tab *next;
|
||||
} t_tab;
|
||||
|
||||
/* MEDIUM ALGO FILE */
|
||||
int wich_path(t_stacks *piles, int max_range, int range, char c);
|
||||
int stack_len(t_stack *stack);
|
||||
void bucket_algo(t_stacks *piles, t_tab *preset, int range);
|
||||
/* SORT UTILS FILES */
|
||||
void sort_from_left(t_stacks *piles);
|
||||
void sort_from_right(t_stacks *piles);
|
||||
void push_range_to_b(t_stacks *piles, t_tab *one_preset, int range);
|
||||
/* MEDIUM UTILS FILES */
|
||||
int range_bucket(t_stack *first);
|
||||
int get_first_lower(t_stack *first);
|
||||
int get_next_lower(t_stack *first, int old_lower);
|
||||
int calcul_range(int value, int range);
|
||||
int in_range(int value, int max_range, int range);
|
||||
int get_number_in_range(int max_range, t_stack *a, int range);
|
||||
/* UTILS STRUCT TAB FILE */
|
||||
t_tab *get_tabs(t_stack *first, int range);
|
||||
|
||||
#endif
|
||||
20
headers/parsing.h
Normal file
20
headers/parsing.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parsing.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/07 08:03:08 by mteriier #+# #+# */
|
||||
/* Updated: 2026/01/07 08:03:10 by mteriier ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PARSING_H
|
||||
# define PARSING_H
|
||||
|
||||
int ft_atoi(const char *nptr);
|
||||
t_stacks *init_big_stacks(int argc, char **argv);
|
||||
int ft_strncmp(const char *s1, const char *s2, int n);
|
||||
|
||||
#endif
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/12/08 15:34:51 by mteriier ### ########lyon.fr */
|
||||
/* Updated: 2026/01/07 10:30:42 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
typedef struct s_stack
|
||||
{
|
||||
int value;
|
||||
s_stack *next;
|
||||
s_stack *previous;
|
||||
struct s_stack *next;
|
||||
struct s_stack *previous;
|
||||
} t_stack;
|
||||
|
||||
typedef struct s_stacks
|
||||
@@ -26,6 +26,8 @@ typedef struct s_stacks
|
||||
t_stack *b;
|
||||
} t_stacks;
|
||||
|
||||
/* PRINT STACK FUNCTION*/
|
||||
void print_stacks(t_stacks *stacks, int len, t_stack *a, t_stack *b);
|
||||
/*STACK_FUNCTIONS*/
|
||||
void pa(t_stacks *stacks);
|
||||
void pb(t_stacks *stacks);
|
||||
@@ -38,5 +40,33 @@ void rr(t_stacks *stacks);
|
||||
void sa(t_stacks *stacks);
|
||||
void sb(t_stacks *stacks);
|
||||
void ss(t_stacks *stacks);
|
||||
/* STACK ADD AND CLEAR FILES */
|
||||
t_stack *new_stack(int value);
|
||||
void stack_add_back(t_stack **stack, t_stack *new);
|
||||
void stack_add_front(t_stack **stack, t_stack *new);
|
||||
void stack_clear_all(t_stack *stack, t_stack *first);
|
||||
void free_all(t_stacks *piles);
|
||||
/* STACKS LEN FILES */
|
||||
int stack_a_len(t_stacks *stacks);
|
||||
int stack_b_len(t_stacks *stacks);
|
||||
int highest_stack_len(t_stacks *stacks);
|
||||
/* PRE SORT */
|
||||
int r_to_lowest(t_stack *stack, int len);
|
||||
void sort_three_a(t_stacks *stacks);
|
||||
/* ITERATE FILE */
|
||||
void optimal_rotate(t_stacks *stacks, int i, int len, char stack);
|
||||
/* COMPARE FILE */
|
||||
int is_lowest(t_stack *stack, t_stack *node, int len);
|
||||
int is_highest(t_stack *stack, t_stack *node, int len);
|
||||
/* CHECK ORDER FILE */
|
||||
int check_order(t_stack *stack);
|
||||
/* INSERTION */
|
||||
void insertion(t_stacks *stacks, int len);
|
||||
int test2(char **argv);
|
||||
/* TEST FILE */
|
||||
int test1(int argc, char **argv);
|
||||
|
||||
/* RADIX */
|
||||
void radix(t_stacks *stacks);
|
||||
|
||||
#endif
|
||||
20
main.c
Normal file
20
main.c
Normal file
@@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/08 18:32:35 by mteriier #+# #+# */
|
||||
/* Updated: 2025/12/14 16:55:43 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc > 1)
|
||||
test1(argc, argv);
|
||||
return (0);
|
||||
}
|
||||
42
parsing/ft_atoi.c
Normal file
42
parsing/ft_atoi.c
Normal file
@@ -0,0 +1,42 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atoi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/08 18:28:17 by mteriier #+# #+# */
|
||||
/* Updated: 2025/12/08 19:30:48 by mteriier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
static int calcul_sign(char c)
|
||||
{
|
||||
if (c == '-')
|
||||
return (-1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_atoi(const char *nptr)
|
||||
{
|
||||
size_t i;
|
||||
int sign;
|
||||
int tmp;
|
||||
|
||||
i = 0;
|
||||
sign = 1;
|
||||
tmp = 0;
|
||||
while ((nptr[i] >= 9 && nptr[i] <= 13) || nptr[i] == ' ')
|
||||
i++;
|
||||
sign = calcul_sign(nptr[i]);
|
||||
if (nptr[i] == '-' || nptr[i] == '+')
|
||||
i++;
|
||||
while (nptr[i] >= '0' && nptr[i] <= '9')
|
||||
{
|
||||
tmp = tmp * 10 + nptr[i] - '0';
|
||||
i++;
|
||||
}
|
||||
return (tmp * sign);
|
||||
}
|
||||
25
parsing/ft_strncmp.c
Normal file
25
parsing/ft_strncmp.c
Normal file
@@ -0,0 +1,25 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strncmp.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/07 12:36:38 by mteriier #+# #+# */
|
||||
/* Updated: 2026/01/07 12:36:40 by mteriier ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_strncmp(const char *s1, const char *s2, int n)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (n == 0)
|
||||
return (0);
|
||||
while (i < n - 1 && s1[i] && s1[i] == s2[i])
|
||||
{
|
||||
i++;
|
||||
}
|
||||
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
|
||||
}
|
||||
56
parsing/parsing.c
Normal file
56
parsing/parsing.c
Normal file
@@ -0,0 +1,56 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parsing.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/08 16:21:05 by mteriier #+# #+# */
|
||||
/* Updated: 2025/12/09 10:19:17 by mteriier ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "parsing.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static t_stack *parsing(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
int stock;
|
||||
t_stack *first;
|
||||
t_stack *new;
|
||||
|
||||
i = 1;
|
||||
first = NULL;
|
||||
while (i < argc)
|
||||
{
|
||||
stock = ft_atoi(argv[i]);
|
||||
new = new_stack(stock);
|
||||
if (!new && !first)
|
||||
return (NULL);
|
||||
else if (!new)
|
||||
{
|
||||
stack_clear_all(first, first);
|
||||
return (NULL);
|
||||
}
|
||||
stack_add_back(&first, new);
|
||||
i++;
|
||||
}
|
||||
return (first);
|
||||
}
|
||||
|
||||
t_stacks *init_big_stacks(int argc, char **argv)
|
||||
{
|
||||
t_stacks *stacks;
|
||||
t_stack *a;
|
||||
|
||||
stacks = malloc(sizeof(t_stacks));
|
||||
stacks->a = NULL;
|
||||
stacks->b = NULL;
|
||||
if (!stacks)
|
||||
return (NULL);
|
||||
a = parsing(argc, argv);
|
||||
stacks->a = a;
|
||||
return (stacks);
|
||||
}
|
||||
42
stack_utils/print_stacks.c
Normal file
42
stack_utils/print_stacks.c
Normal file
@@ -0,0 +1,42 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* print_stacks.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/09 13:10:00 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/12/09 13:36:03 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void print_stacks(t_stacks *stacks, int len, t_stack *a, t_stack *b)
|
||||
{
|
||||
int a_len;
|
||||
int b_len;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
a_len = stack_a_len(stacks);
|
||||
b_len = stack_b_len(stacks);
|
||||
while (i < len)
|
||||
{
|
||||
if (a_len >= len - i)
|
||||
{
|
||||
printf("%d", a->value);
|
||||
a = a->next;
|
||||
}
|
||||
printf("\t");
|
||||
if (b_len >= len - i)
|
||||
{
|
||||
printf("%d", b->value);
|
||||
b = b->next;
|
||||
}
|
||||
printf("\n");
|
||||
i++;
|
||||
}
|
||||
printf("_\t_\nA\tB\n\n");
|
||||
}
|
||||
@@ -6,28 +6,48 @@
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/08 15:09:40 by mteriier #+# #+# */
|
||||
/* Updated: 2025/12/08 15:35:23 by mteriier ### ########lyon.fr */
|
||||
/* Updated: 2025/12/12 11:39:33 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void pa(t_stacks *stacks)
|
||||
{
|
||||
t_stack a;
|
||||
t_stack *b_push;
|
||||
|
||||
if (!stacks || !stacks->b)
|
||||
return ;
|
||||
a = stack->a;
|
||||
a->value = stacks->b->value;
|
||||
b_push = stacks->b;
|
||||
if (stacks->b->next == stacks->b)
|
||||
stacks->b = NULL;
|
||||
else
|
||||
{
|
||||
stacks->b->next->previous = stacks->b->previous;
|
||||
stacks->b->previous->next = stacks->b->next;
|
||||
stacks->b = stacks->b->next;
|
||||
}
|
||||
stack_add_front(&(stacks->a), b_push);
|
||||
write(1, "pa\n", 3);
|
||||
}
|
||||
|
||||
void pb(t_stacks *stacks)
|
||||
{
|
||||
t_stack b;
|
||||
t_stack *a_push;
|
||||
|
||||
if (!stacks || !stacks->a)
|
||||
return ;
|
||||
b = stacks->b;
|
||||
b->value = stacks->a->value;
|
||||
a_push = stacks->a;
|
||||
if (stacks->a->next == stacks->a)
|
||||
stacks->a = NULL;
|
||||
else
|
||||
{
|
||||
stacks->a->next->previous = stacks->a->previous;
|
||||
stacks->a->previous->next = stacks->a->next;
|
||||
stacks->a = stacks->a->next;
|
||||
}
|
||||
stack_add_front(&(stacks->b), a_push);
|
||||
write(1, "pb\n", 3);
|
||||
}
|
||||
|
||||
@@ -6,26 +6,32 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/08 14:43:45 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/12/08 14:52:53 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2025/12/12 11:39:25 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include <unistd.h>
|
||||
|
||||
void rra(t_stacks *stacks)
|
||||
{
|
||||
if (stacks && stacks->a && stacks->a->previous)
|
||||
stacks->a = stacks->a->previous;
|
||||
write(1, "rra\n", 4);
|
||||
}
|
||||
|
||||
void rrb(t_stacks *stacks)
|
||||
{
|
||||
if (stacks && stacks->b && stacks->b->previous)
|
||||
stacks->b = stacks->b->previous;
|
||||
write(1, "rrb\n", 4);
|
||||
}
|
||||
|
||||
void rrr(t_stacks *stacks)
|
||||
{
|
||||
rra(stacks);
|
||||
rrb(stacks);
|
||||
if (stacks && stacks->b && stacks->b->previous)
|
||||
stacks->b = stacks->b->previous;
|
||||
if (stacks && stacks->a && stacks->a->previous)
|
||||
stacks->a = stacks->a->previous;
|
||||
write(1, "rrr\n", 4);
|
||||
}
|
||||
|
||||
@@ -6,26 +6,32 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/08 14:32:10 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/12/08 14:52:37 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2025/12/12 11:39:17 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include <unistd.h>
|
||||
|
||||
void ra(t_stacks *stacks)
|
||||
{
|
||||
if (stacks && stacks->a && stacks->a->next)
|
||||
stacks->a = stacks->a->next;
|
||||
write(1, "ra\n", 3);
|
||||
}
|
||||
|
||||
void rb(t_stacks *stacks)
|
||||
{
|
||||
if (stacks && stacks->b && stacks->b->next)
|
||||
stacks->b = stacks->b->next;
|
||||
write(1, "rb\n", 3);
|
||||
}
|
||||
|
||||
void rr(t_stacks *stacks)
|
||||
{
|
||||
ra(stacks);
|
||||
rb(stacks);
|
||||
if (stacks && stacks->a && stacks->a->next)
|
||||
stacks->a = stacks->a->next;
|
||||
if (stacks && stacks->b && stacks->b->next)
|
||||
stacks->b = stacks->b->next;
|
||||
write(1, "rr\n", 3);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/08 15:52:40 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/12/08 16:03:54 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2025/12/12 11:39:09 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -30,7 +30,7 @@ void stack_add_back(t_stack **stack, t_stack *new)
|
||||
{
|
||||
if (!stack || !new)
|
||||
return ;
|
||||
if (!(*stack))
|
||||
if (*stack == NULL)
|
||||
{
|
||||
(*stack) = new;
|
||||
new->next = new;
|
||||
@@ -39,7 +39,7 @@ void stack_add_back(t_stack **stack, t_stack *new)
|
||||
else
|
||||
{
|
||||
(*stack)->previous->next = new;
|
||||
new->previous = *(stack)->previous;
|
||||
new->previous = (*stack)->previous;
|
||||
(*stack)->previous = new;
|
||||
new->next = *stack;
|
||||
}
|
||||
@@ -47,7 +47,7 @@ void stack_add_back(t_stack **stack, t_stack *new)
|
||||
|
||||
void stack_add_front(t_stack **stack, t_stack *new)
|
||||
{
|
||||
if (!stack || *new)
|
||||
if (!stack || !new)
|
||||
return ;
|
||||
stack_add_back(stack, new);
|
||||
*stack = new;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/08 16:05:27 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/12/08 16:15:28 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2025/12/12 11:39:02 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -21,3 +21,12 @@ void stack_clear_all(t_stack *stack, t_stack *first)
|
||||
stack_clear_all(stack->next, first);
|
||||
free(stack);
|
||||
}
|
||||
|
||||
void free_all(t_stacks *piles)
|
||||
{
|
||||
if (piles->a)
|
||||
stack_clear_all(piles->a, piles->a);
|
||||
if (piles->b)
|
||||
stack_clear_all(piles->b, piles->b);
|
||||
free(piles);
|
||||
}
|
||||
|
||||
63
stack_utils/stacks_len.c
Normal file
63
stack_utils/stacks_len.c
Normal file
@@ -0,0 +1,63 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* stacks_len.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/09 13:11:50 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/12/09 13:19:21 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
int stack_a_len(t_stacks *stacks)
|
||||
{
|
||||
t_stack *first;
|
||||
t_stack *a_stack;
|
||||
int len;
|
||||
|
||||
a_stack = stacks->a;
|
||||
first = a_stack;
|
||||
if (!a_stack)
|
||||
return (0);
|
||||
len = 1;
|
||||
while (a_stack->next != first)
|
||||
{
|
||||
a_stack = a_stack->next;
|
||||
len++;
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
|
||||
int stack_b_len(t_stacks *stacks)
|
||||
{
|
||||
t_stack *first;
|
||||
t_stack *b_stack;
|
||||
int len;
|
||||
|
||||
b_stack = stacks->b;
|
||||
first = b_stack;
|
||||
if (!b_stack)
|
||||
return (0);
|
||||
len = 1;
|
||||
while (b_stack->next != first)
|
||||
{
|
||||
b_stack = b_stack->next;
|
||||
len++;
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
|
||||
int highest_stack_len(t_stacks *stacks)
|
||||
{
|
||||
int a_len;
|
||||
int b_len;
|
||||
|
||||
a_len = stack_a_len(stacks);
|
||||
b_len = stack_b_len(stacks);
|
||||
if (a_len > b_len)
|
||||
return (a_len);
|
||||
return (b_len);
|
||||
}
|
||||
@@ -6,15 +6,16 @@
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/08 14:48:44 by mteriier #+# #+# */
|
||||
/* Updated: 2025/12/08 15:04:39 by mteriier ### ########lyon.fr */
|
||||
/* Updated: 2025/12/12 11:38:52 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include <unistd.h>
|
||||
|
||||
void sa(t_stacks *stacks)
|
||||
{
|
||||
t_stack a;
|
||||
t_stack *a;
|
||||
int stock;
|
||||
|
||||
if (!stacks || !stacks->a || !stacks->a->next)
|
||||
@@ -23,11 +24,12 @@ void sa(t_stacks *stacks)
|
||||
stock = a->value;
|
||||
a->value = a->next->value;
|
||||
a->next->value = stock;
|
||||
write(1, "sa\n", 3);
|
||||
}
|
||||
|
||||
void sb(t_stacks *stacks)
|
||||
{
|
||||
t_stack b;
|
||||
t_stack *b;
|
||||
int stock;
|
||||
|
||||
if (!stacks || !stacks->b || !stacks->b->next)
|
||||
@@ -36,10 +38,30 @@ void sb(t_stacks *stacks)
|
||||
stock = b->value;
|
||||
b->value = b->next->value;
|
||||
b->next->value = stock;
|
||||
write(1, "sb\n", 3);
|
||||
}
|
||||
|
||||
void ss(t_stacks *stacks)
|
||||
{
|
||||
sa(stacks);
|
||||
sb(stacks);
|
||||
t_stack *b;
|
||||
t_stack *a;
|
||||
int stock;
|
||||
|
||||
if (!stacks)
|
||||
return ;
|
||||
if (stacks->b && stacks->b->next)
|
||||
{
|
||||
b = stacks->b;
|
||||
stock = b->value;
|
||||
b->value = b->next->value;
|
||||
b->next->value = stock;
|
||||
}
|
||||
if (stacks->a && stacks->a->next)
|
||||
{
|
||||
a = stacks->a;
|
||||
stock = a->value;
|
||||
a->value = a->next->value;
|
||||
a->next->value = stock;
|
||||
}
|
||||
write(1, "ss\n", 3);
|
||||
}
|
||||
|
||||
35
test_one.c
Normal file
35
test_one.c
Normal file
@@ -0,0 +1,35 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* test_one.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.lyon42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/22 12:33:58 by mteriier #+# #+# */
|
||||
/* Updated: 2026/01/07 14:58:10 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "parsing.h"
|
||||
#include "medium_headers.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int test1(int argc, char **argv)
|
||||
{
|
||||
t_stacks *piles;
|
||||
//t_tab *preset;
|
||||
|
||||
piles = NULL;
|
||||
if (argc > 1)
|
||||
{
|
||||
piles = init_big_stacks(argc, argv);
|
||||
//preset = get_tabs(piles->a, range_bucket(piles->a));
|
||||
//bucket_algo(piles, preset, range_bucket(piles->a));
|
||||
radix(piles);
|
||||
}
|
||||
free_all(piles);
|
||||
//free(piles);
|
||||
return (0);
|
||||
}
|
||||
Reference in New Issue
Block a user