96 Commits

Author SHA1 Message Date
52220b5b49 Add index in t_stack + indexer function to add it 2026-01-14 11:15:31 +01:00
82111f59ad Merge branch 'master' of github.com:DavidGailleton/42-Push_Swap 2026-01-14 10:08:14 +01:00
Maoake Teriierooiterai
02993cded2 clean the include not used 2026-01-13 12:30:33 +01:00
f4824f2984 Merge branch 'master' of github.com:DavidGailleton/42-Push_Swap 2026-01-13 12:23:35 +01:00
Maoake Teriierooiterai
b2f9912963 fix my mistakes 2026-01-13 12:21:15 +01:00
Maoake Teriierooiterai
7d2fed6e76 starting the bonus and modify the name folder headers -> includes 2026-01-13 09:40:15 +01:00
Maoake Teriierooiterai
696f1ca2d5 doing the norm 2026-01-13 08:19:15 +01:00
Maoake Teriierooiterai
cfdf913c4b refactoring functions parsing main flags 2026-01-13 08:17:50 +01:00
Maoake Teriierooiterai
f8f1b43539 fix the adaptive 2026-01-12 16:18:17 +01:00
Maoake Teriierooiterai
9c3f91dcb9 delete print stacks and looking for the secure malloc if there is a malloc not secure 2026-01-12 14:48:14 +01:00
Maoake Teriierooiterai
97ce3bf40b fixing the parsing 2026-01-12 14:00:08 +01:00
5ae963bb09 Merge branch 'master' of github.com:DavidGailleton/42-Push_Swap 2026-01-12 13:37:31 +01:00
Maoake Teriierooiterai
ccccc3b640 finish the disorder 2026-01-12 12:37:56 +01:00
Maoake Teriierooiterai
69dc80e498 fix my medium algo 2026-01-12 11:35:23 +01:00
15ca9cfe3f FIX: exit if str is null 2026-01-09 14:57:42 +01:00
Maoake Teriierooiterai
376f457f7d Merge branch 'master' of github.com:DavidGailleton/42-Push_Swap into medium_algo 2026-01-09 14:57:41 +01:00
Maoake Teriierooiterai
ad855e5915 fixing some issues on the checker error and need to fix a last thing on the bucket range algo 2026-01-09 14:57:05 +01:00
7db1844447 Fix disorder incrementation 2026-01-09 14:53:52 +01:00
Maoake Teriierooiterai
be9d265fb5 Merge branch 'master' of github.com:DavidGailleton/42-Push_Swap into medium_algo 2026-01-09 12:43:13 +01:00
Maoake Teriierooiterai
439ac6948d fix valgrind 2026-01-09 12:42:02 +01:00
dae8fc84ea fix bench mode 2026-01-09 12:13:59 +01:00
Maoake Teriierooiterai
c138a43f0b Merge branch 'master' of github.com:DavidGailleton/42-Push_Swap into medium_algo 2026-01-09 12:00:29 +01:00
Maoake Teriierooiterai
90d45b07ae finish the checker error 2026-01-09 11:59:44 +01:00
b6a36a1696 Compute disorder function 2026-01-09 11:28:01 +01:00
Maoake Teriierooiterai
0e5ef61de7 change the parsing add join strlcpy strlcat 2026-01-09 08:55:43 +01:00
Maoake Teriierooiterai
c131843bc0 replacing piles by stacks 2026-01-09 07:24:32 +01:00
Maoake Teriierooiterai
32a0668b62 final finish on the parsing protecting the malloc 2026-01-09 07:21:50 +01:00
Maoake Teriierooiterai
0df082065e finish the parsing solve the problem with 2 arg 2026-01-08 19:02:13 +01:00
Maoake Teriierooiterai
00e01ed1f9 fix files 2026-01-08 18:44:50 +01:00
Maoake Teriierooiterai
a405d7dad5 modify flags.h 2026-01-08 17:14:22 +01:00
Maoake Teriierooiterai
ee4994dcc6 fix conflict 2026-01-08 17:12:33 +01:00
9a20072f36 Add flags to makefile and fix some compile error 2026-01-08 16:09:37 +00:00
38eae5ab16 Bench algo working 2026-01-08 15:28:07 +00:00
Maoake Teriierooiterai
f32a316c5f finish the pos of flags 2026-01-08 16:25:09 +01:00
Maoake Teriierooiterai
173c11c2e3 fixing some errors 2026-01-08 15:12:09 +01:00
Maoake Teriierooiterai
42ed0f7579 fix conflict 2026-01-08 15:07:55 +01:00
Maoake Teriierooiterai
4af87fa98a finnish the parsing 2026-01-08 15:04:02 +01:00
567a9c61ab Add disorder var in t_stacks 2026-01-08 14:02:15 +00:00
bb39e7d0ec delete condition to add operation in t_stacks 2026-01-08 13:59:09 +00:00
2ba9029955 Add bench mode and algo in t_stacks 2026-01-08 13:54:29 +00:00
765ec373e5 replace argv[1] by str 2026-01-08 13:17:43 +00:00
335827d6b6 add checker for bench mod 2026-01-08 13:13:52 +00:00
0dcbd8aaab FIX: multiple flags in input generate an error 2026-01-08 13:04:37 +00:00
4cb2fd3244 input checker 2026-01-08 12:58:15 +00:00
Maoake Teriierooiterai
9bc53094a9 manage conflict 2026-01-08 13:52:45 +01:00
Maoake Teriierooiterai
8dbf9f6c6e adding split substr strlen 2026-01-08 13:50:38 +01:00
b47b307663 fix segfault 2026-01-08 11:10:17 +00:00
c799dd65ee Radix is woking 2026-01-07 16:21:52 +00:00
Maoake Teriierooiterai
2f4498d2c5 adding file split strlen substr for the special parsing 2026-01-07 16:16:17 +01:00
2c6d8d4095 Merge branch 'master' into radix 2026-01-07 14:53:07 +00:00
David Gailleton
6942548108 Not working radix 2026-01-07 14:50:04 +00:00
Maoake Teriierooiterai
17f9a5affd delete the parsing2.c cause no use for now 2026-01-07 13:27:06 +01:00
Maoake Teriierooiterai
e1351b4d2f doing the norm 2026-01-07 13:23:42 +01:00
Maoake Teriierooiterai
93e8c10b27 adding the flags 2026-01-07 13:13:30 +01:00
Maoake Teriierooiterai
0d9b46c748 fix calcul_range function cause i didnt manage to scan the last value on the pile 2026-01-07 12:03:57 +01:00
Maoake Teriierooiterai
95577e5f02 finish to link all the files 2026-01-07 10:06:38 +01:00
Maoake Teriierooiterai
ca732b1b97 adding other files cause i put the git on the wrong 2026-01-07 08:14:22 +01:00
Maoake Teriierooiterai
2b255b94da starting the refactor on the folders 2026-01-07 08:13:27 +01:00
Maoake Teriierooiterai
a78b3f1a44 adding the new range bucket 2026-01-06 16:05:32 +01:00
Maoake Teriierooiterai
4bb0b71141 try to push 2026-01-06 14:54:34 +01:00
Maoake Teriierooiterai
40ec288032 fix some bugs 2026-01-06 14:52:47 +01:00
David Gailleton
54db222c48 rm executable 2026-01-06 13:06:33 +00:00
Maoake Teriierooiterai
ef9f16a142 Merge branch 'medium_algo' 2026-01-06 13:53:48 +01:00
Maoake Teriierooiterai
80179a5289 finally finish the medium algo 2026-01-06 13:52:29 +01:00
David Gailleton
508b63d296 rm bubble sort 2026-01-06 12:35:52 +00:00
David Gailleton
805d592160 ? 2026-01-06 12:34:15 +00:00
Maoake Teriierooiterai
f98dce6f61 finish the medium need to modify and sort in a just need to do the reverse 2026-01-06 12:41:36 +01:00
Maoake Teriierooiterai
ae4740c479 im in the bucket algo adding some files like sort_utils the purpose is to sort just for the range 2026-01-05 16:13:46 +01:00
Maoake Teriierooiterai
dfdcd68549 Merge branch 'master' into medium_algo 2026-01-02 13:06:01 +01:00
Maoake
a44b878625 setting up the searching path if i start from the top or from the bot need to be testing out 2025-12-23 10:08:33 +00:00
Maoake
e06a51e034 deleting the obj and push_swap exec 2025-12-23 09:22:45 +00:00
Maoake
85b83f3cc7 fixing the leak valgrind and conditionnal jump on some functions cause i forget to initialize the var to NULL i just need to remember this 2025-12-23 09:21:17 +00:00
Maoake
96a0a6d8f0 setting up my preset but i need to valgrind 2025-12-22 20:37:12 +00:00
Maoake
e563d663ec modifying some preset for the tabs and adding some functions in the main for do the test 2025-12-22 11:06:34 +00:00
David Gailleton
e2495fa62f add three pre sort alg + optimized simple sort alg 2025-12-15 14:47:27 +01:00
David Gailleton
e9c90e447c more optimized insertion sort algorithm 2025-12-14 17:31:36 +01:00
David Gailleton
0d01b58ff1 insertion sort note optimized is working 2025-12-14 16:51:11 +01:00
David Gailleton
a68bcd2503 Merge branch 'master' into simple_algorithm 2025-12-12 11:40:58 +01:00
David Gailleton
55363e0e6f Fix header file includes AND add write for each push swap functions 2025-12-12 11:40:02 +01:00
David Gailleton
f5da9fac1f New insertion algorithm WIP 2025-12-12 11:32:01 +01:00
Maoake Teriierooiterai
a7cbd18db6 deleting push swap 2025-12-11 18:33:09 +01:00
Maoake Teriierooiterai
48c13b8ef8 finish the makefile i can do all the test i want 2025-12-11 09:40:21 +01:00
Maoake
5372b944a4 adding makefile and debugging to try new test for the preset to test the program 2025-12-10 21:02:33 +00:00
Maoake Teriierooiterai
b6c1dc73a8 adding prototype in push_swap.h and norming some files 2025-12-10 16:30:26 +01:00
Maoake Teriierooiterai
977a4a4b1a adding all my files for my setup utils_struct_tab compare_value medium_algo utils_medium 2025-12-10 16:20:49 +01:00
David Gailleton
1463f3e14a bubble v1 2025-12-09 14:23:22 +01:00
David Gailleton
c781d6f5cd add print stacks function 2025-12-09 13:38:10 +01:00
Maoake Teriierooiterai
ad4275c206 bubble algorithm WIP 2025-12-09 12:43:28 +01:00
Maoake Teriierooiterai
c9066f4a9b Algorithm firsts utils (stack size + check order) 2025-12-09 12:32:13 +01:00
Maoake Teriierooiterai
8cd29f7151 all basics functions has been tested we can start the sort algorithms 2025-12-09 11:45:42 +01:00
Maoake
30b306d2c2 modified some files like all files in stack utils and adding main.c and modified atoi and parssing 2025-12-08 20:44:03 +00:00
Maoake
8e8ca5cfdc adding atoi in function 2025-12-08 18:31:08 +00:00
Maoake Teriierooiterai
15d56b2b32 adding the parsing need to test 2025-12-08 16:50:55 +01:00
David Gailleton
c521cff21a Merge branch 'stack_functions' 2025-12-08 16:17:46 +01:00
Maoake Teriierooiterai
9dc3d88d08 Merge branch 'stack_functions' 2025-12-08 15:37:16 +01:00
David Gailleton
d4d8a79d30 README initialisation 2025-12-08 14:58:49 +01:00
56 changed files with 2746 additions and 51 deletions

31
.clang-format Normal file
View File

@@ -0,0 +1,31 @@
Language: C
AlignConsecutiveDeclarations: false
AlignConsecutiveMacros: false
AlignEscapedNewlinesLeft: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
BinPackArguments: true
BinPackParameters: true
BreakBeforeBraces: Allman
BreakBeforeTernaryOperators: false
ColumnLimit: 80
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
PointerAlignment: Right
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakString: 1
PenaltyExcessCharacter: 0
SpaceAfterCStyleCast: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
TabWidth: 4
UseTab: Always

9
.gitignore vendored
View File

@@ -53,3 +53,12 @@ dkms.conf
# debug information files
*.dwo
# Vim swap files
*.swp
# Executable
push_swap
# File obj
obj/

124
Makefile Normal file
View File

@@ -0,0 +1,124 @@
#============================
# 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
CHECKER_DIR = checker
INCLUDES = includes
#============================
# ALL FILES WITHOUT PATH
#============================
SRC = main.c test_one.c ft_putnbr.c secure_write.c
INSERTION = insertion.c
FLAGS_FILES = algorithms_sort.c flag.c bench.c
PARSING = ft_atoi.c parsing.c ft_strncmp.c ft_split.c ft_strlen.c ft_substr.c checker.c ft_itoa.c ft_isdigit.c \
ft_strjoin.c ft_strlcat.c ft_strlcpy.c parsing_2.c disorder.c indexer.c
CHECKER_FILES = check_error.c verif_flag.c verif_is_digit.c verif_overflow.c verif_double.c
STACK_UTILS = push.c rev_rotate.c rotate.c stack_add.c stack_remove.c stacks_len.c swap.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) \
$(FLAGS_DIR)/$(FLAGS_FILES) $(CHECKER_DIR)/$(CHECKER_FILES)
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: $(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)/%.o: $(FLAGS_DIR)/%.c | $(OBJ_DIR)
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
$(OBJ_DIR)/%.o: $(CHECKER_DIR)/%.c | $(OBJ_DIR)
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
$(OBJ_DIR)/%.o: %.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
View 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*)

View 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');
}

View 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 *stacks, int max_range, int range, char c)
{
t_stack *tmp;
int i;
int first_pass;
if (c == 'a')
tmp = stacks->a;
else
tmp = stacks->b;
tmp = tmp->previous;
i = 0;
first_pass = 1;
while (first_pass || tmp != stacks->b->previous)
{
first_pass = 0;
if (in_range(tmp->value, max_range, range))
tmp = stacks->b;
tmp = tmp->previous;
i++;
}
return (i);
}
static int path_to_start(t_stacks *stacks, int max_range, int range, char c)
{
t_stack *tmp;
int i;
int first_pass;
if (c == 'a')
tmp = stacks->a;
else
tmp = stacks->b;
i = 0;
first_pass = 1;
while (first_pass || tmp != stacks->b)
{
first_pass = 0;
if (in_range(tmp->value, max_range, range))
{
tmp = stacks->b->previous;
}
tmp = tmp->next;
i++;
}
return (i);
}
int wich_path(t_stacks *stacks, int max_range, int range, char c)
{
int path_start;
int path_end;
path_start = path_to_start(stacks, max_range, range, c);
path_end = path_to_end(stacks, 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 *stacks, t_tab *preset, int range)
{
t_tab *tmp;
tmp = preset;
while (stacks->a)
pb(stacks);
while (preset)
{
push_range_to_b(stacks, preset, range);
if (preset->next)
tmp = preset->next;
else
tmp = NULL;
if (preset)
free(preset);
preset = tmp;
}
while (stacks->b)
pa(stacks);
return ;
}

View 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 *stacks, int value, char start_end)
{
int i;
t_stack *tmp;
i = 0;
tmp = stacks->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 *stacks, int value)
{
int start_path;
int end_path;
start_path = number_move_reverse(stacks, value, 's');
if (start_path == 0)
return (1);
end_path = number_move_reverse(stacks, value, 'e');
if (start_path < end_path)
return (1);
return (0);
}
static void sort_little_pile(t_stacks *stacks)
{
if (!stacks->a)
{
pa(stacks);
return ;
}
if (stacks->a->previous->value < stacks->b->value)
{
pa(stacks);
ra(stacks);
return ;
}
if (sort_path(stacks, stacks->b->value))
sort_from_left(stacks);
else
sort_from_right(stacks);
}
void push_range_to_b(t_stacks *stacks, t_tab *one_preset, int range)
{
while (one_preset->nb_in > 0)
{
if (wich_path(stacks, one_preset->max_range, range, 'b'))
{
while (!in_range(stacks->b->value, one_preset->max_range, range))
rb(stacks);
}
else
{
while (!in_range(stacks->b->value, one_preset->max_range, range))
{
rrb(stacks);
}
}
sort_little_pile(stacks);
one_preset->nb_in--;
}
}

View 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 *stacks)
{
int i;
i = 0;
while (stacks->b->value > stacks->a->value)
{
ra(stacks);
i++;
}
pa(stacks);
while (i > 0)
{
rra(stacks);
i--;
}
}
void sort_from_right(t_stacks *stacks)
{
int i;
i = 0;
while (stacks->b->value < stacks->a->previous->value)
{
rra(stacks);
i++;
}
pa(stacks);
while (i >= 0)
{
ra(stacks);
i--;
}
}

View File

@@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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;
int pass;
tmp = first;
lower = tmp->value;
pass = 1;
while (tmp != first || pass == 1)
{
pass = 0;
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;
if (next_lower == -2147483648)
return (next_lower);
}
tmp = tmp->next;
}
return (next_lower);
}
int calcul_range(int value, int range)
{
int max_range;
max_range = 0;
if (value == 2147483647)
return (value);
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;
int pass;
nb_in = 0;
tmp = a;
pass = 1;
first = tmp;
while (tmp != first || pass == 1)
{
pass = 0;
if (in_range(tmp->value, max_range, range))
nb_in++;
tmp = tmp->next;
}
return (nb_in);
}

View File

@@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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;
int pass;
t_stack *a;
a = first;
max = a->value;
pass = 1;
while (a != first || pass == 1)
{
pass = 0;
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;
int pass;
a = first;
min = a->value;
pass = 1;
while (a != first || pass == 1)
{
pass = 0;
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;
long diff;
int sqrt;
len = stack_len(first);
diff = (long)get_max_number(first) - (long)get_min_number(first);
sqrt = my_sqrt(len);
if (diff / sqrt < 2)
{
return (get_max_number(first));
}
return (diff / my_sqrt(len));
}

View 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
View 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);
}

View 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);
}

View File

@@ -1,42 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.h :+: :+: :+: */
/* compare.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* Created: 2025/12/11 17:46:00 by dgaillet #+# #+# */
/* Updated: 2025/12/11 17:46:22 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#ifndef PUSH_SWAP_H
# define PUSH_SWAP_H
#include "push_swap.h"
typedef struct s_stack
int is_lowest(t_stack *stack, t_stack *node, int len)
{
int value;
s_stack *next;
s_stack *previous;
} t_stack;
int i;
typedef struct s_stacks
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)
{
t_stack *a;
t_stack *b;
} t_stacks;
int i;
/*STACK_FUNCTIONS*/
void pa(t_stacks *stacks);
void pb(t_stacks *stacks);
void rra(t_stacks *stacks);
void rrb(t_stacks *stacks);
void rrr(t_stacks *stacks);
void ra(t_stacks *stacks);
void rb(t_stacks *stacks);
void rr(t_stacks *stacks);
void sa(t_stacks *stacks);
void sb(t_stacks *stacks);
void ss(t_stacks *stacks);
#endif
i = 0;
while (i < len)
{
if (node->value < stack->value)
return (0);
stack = stack->next;
i++;
}
return (1);
}

View 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);
}
}

View File

@@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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_two(t_stacks *stacks)
{
sa(stacks);
}
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);
}
}

39
checker/check_error.c Normal file
View File

@@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/09 09:31:50 by mteriier #+# #+# */
/* Updated: 2026/01/09 09:31:51 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "check_error.h"
#include "parsing.h"
int verif_is_number(char **tab)
{
int len;
len = len_split(tab);
if (tab[len - 1][0] == '-' && !ft_isdigit(tab[len - 1][1]))
return (0);
return (1);
}
int check_error(char **tab, int mod)
{
if (!verif_flag(tab, mod))
return (0);
if (!verif_is_digit(tab, mod))
return (0);
if (!verif_overflow(tab, mod))
return (0);
if (!verif_double(tab, mod))
return (0);
if (!verif_is_number(tab))
return (0);
return (1);
}

47
checker/verif_double.c Normal file
View File

@@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* verif_double.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/09 10:41:42 by mteriier #+# #+# */
/* Updated: 2026/01/09 10:41:43 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
#include "check_error.h"
static int is_double_in_tab(char **tab, int nb, int pos)
{
int i;
i = pos;
while (tab[i])
{
if (nb == ft_atoi(tab[i]))
return (0);
i++;
}
return (1);
}
int verif_double(char **tab, int mod)
{
int i;
int tmp;
i = wich_mod(mod);
while (tab[i])
{
if (tab[i + 1])
{
tmp = ft_atoi(tab[i]);
if (!is_double_in_tab(tab, tmp, i + 1))
return (0);
}
i++;
}
return (1);
}

61
checker/verif_flag.c Normal file
View File

@@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* verif_flag.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/09 09:51:11 by mteriier #+# #+# */
/* Updated: 2026/01/09 09:51:12 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
static int is_exist_flag(char **tab, int pos)
{
int verif;
verif = 0;
if (ft_strncmp(tab[pos], "--bench", 30)
|| ft_strncmp(tab[pos], "--simple", 30)
|| ft_strncmp(tab[pos], "--medium", 30)
|| ft_strncmp(tab[pos], "--adaptive", 30)
|| ft_strncmp(tab[pos], "--complex", 30))
verif = 1;
return (verif);
}
static int verif_exist_flag(char **tab, int mod)
{
int verif;
verif = 0;
if (mod == 1)
verif = (is_exist_flag(tab, 1));
else if (mod == 2)
{
if (is_exist_flag(tab, 1) && is_exist_flag(tab, 2))
verif = 1;
}
return (verif);
}
static int verif_double_flag(char **tab, int mod)
{
int verif;
verif = 1;
if (mod == 2 && ft_strncmp(tab[1], tab[2], 9))
verif = 0;
return (verif);
}
int verif_flag(char **tab, int mod)
{
if (mod == 0)
return (1);
if (verif_double_flag(tab, mod) && verif_exist_flag(tab, mod))
return (1);
return (0);
}

44
checker/verif_is_digit.c Normal file
View File

@@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* verif_is_digit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/09 10:25:23 by mteriier #+# #+# */
/* Updated: 2026/01/09 10:25:24 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
int scan_str_is_digit(char *tab)
{
int i;
i = 0;
while (tab[i])
{
if ((tab[i] == '+' || tab[i] == '-') && ft_isdigit(tab[i + 1])
&& tab[i + 1])
i++;
if (!ft_isdigit(tab[i]))
return (0);
i++;
}
return (1);
}
int verif_is_digit(char **tab, int mod)
{
int i;
i = wich_mod(mod);
while (tab[i])
{
if (!scan_str_is_digit(tab[i]))
return (0);
i++;
}
return (1);
}

53
checker/verif_overflow.c Normal file
View File

@@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* verif_overflow.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/09 10:30:39 by mteriier #+# #+# */
/* Updated: 2026/01/09 10:30:40 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
static int verif_atoi(const char *nptr)
{
size_t i;
int tmp;
int before;
i = 0;
tmp = 0;
before = 0;
if (ft_strncmp(nptr, "-2147483648", 15))
return (1);
while ((nptr[i] >= 9 && nptr[i] <= 13) || nptr[i] == ' ')
i++;
if (nptr[i] == '-' || nptr[i] == '+')
i++;
while (nptr[i] >= '0' && nptr[i] <= '9')
{
before = tmp;
tmp = tmp * 10 + nptr[i] - '0';
if (before > tmp)
return (0);
i++;
}
return (1);
}
int verif_overflow(char **tab, int mod)
{
int i;
i = wich_mod(mod);
while (tab[i])
{
if (!verif_atoi(tab[i]))
return (0);
i++;
}
return (1);
}

BIN
checker_linux Executable file

Binary file not shown.

68
flags/algorithms_sort.c Normal file
View File

@@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* algorithms_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/07 12:15:02 by mteriier #+# #+# */
/* Updated: 2026/01/08 16:07:00 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "medium_headers.h"
#include "parsing.h"
void simple(t_stacks *stacks)
{
int len;
len = stack_a_len(stacks);
stacks->algo = 1;
insertion(stacks, len);
}
void medium(t_stacks *stacks)
{
t_tab *buckets;
int range;
int len;
stacks->algo = 2;
len = stack_a_len(stacks);
if (len == 2)
sort_two(stacks);
else
{
range = range_bucket(stacks->a);
buckets = get_tabs(stacks->a, range);
if (!buckets)
return ;
bucket_algo(stacks, buckets, range);
}
}
void complex(t_stacks *stacks)
{
stacks->algo = 3;
radix(stacks);
}
void adaptive(t_stacks *stacks, char **tab)
{
int i;
float disorder;
i = 0;
while (!ft_isdigit(tab[i][0]) && tab[i])
i++;
disorder = stacks->disorder;
if (disorder < 0.3)
simple(stacks);
else if (disorder < 0.5)
medium(stacks);
else
complex(stacks);
return ;
}

115
flags/bench.c Normal file
View File

@@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bench.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/08 13:59:52 by dgaillet #+# #+# */
/* Updated: 2026/01/09 12:13:36 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "parsing.h"
#include <unistd.h>
static void print_disorder(t_stacks *stacks)
{
int nbr;
char *str;
nbr = (int) stacks->disorder * 10000;
str = ft_itoa(nbr);
if (!str)
exit (EXIT_FAILURE);
secure_write(2, "[bench] disorder: ", 18);
if (ft_strlen(str) <= 2)
secure_write(2, "0", 1);
else
secure_write(2, str, ft_strlen(str) - 2);
secure_write(2, ".", 1);
if (ft_strlen(str) == 1)
secure_write(2, "0", 1);
secure_write(2, &str[ft_strlen(str) - 2], 2);
secure_write(2, "%\n", 2);
free(str);
}
static void print_algo(t_stacks *stacks)
{
secure_write(2, "[bench] algo: ", 14);
if (stacks->algo == 0)
{
secure_write(2, "Adaptative", 10);
if (stacks->disorder < 0.2)
secure_write(2, " / O(n2n)\n", 10);
else if (stacks->disorder >= 0.5)
secure_write(2, " / O(n√n)\n", 10);
else
secure_write(2, " / O(nlogn)\n", 12);
}
else if (stacks->algo == 1)
secure_write(2, "Simple / O(n2n)\n", 16);
else if (stacks->algo == 2)
secure_write(2, "Medium / O(nlogn)\n", 18);
else if (stacks->algo == 3)
secure_write(2, "Complex / O(n√n)\n", 17);
}
static void print_total_ops(t_stacks *stacks)
{
unsigned int total_ops;
total_ops = 0;
total_ops += stacks->sa;
total_ops += stacks->sb;
total_ops += stacks->ss;
total_ops += stacks->pa;
total_ops += stacks->pb;
total_ops += stacks->ra;
total_ops += stacks->rb;
total_ops += stacks->rr;
total_ops += stacks->rra;
total_ops += stacks->rrb;
total_ops += stacks->rrr;
secure_write(2, "[bench] total_ops: ", 19);
ft_putnbr_fd((int) total_ops, 2);
secure_write(2, "\n", 1);
}
static void print_ops(t_stacks *stacks)
{
secure_write(2, "[bench] sa: ", 12);
ft_putnbr_fd((int) stacks->sa, 2);
secure_write(2, " sb: ", 5);
ft_putnbr_fd((int) stacks->sb, 2);
secure_write(2, " ss: ", 5);
ft_putnbr_fd((int) stacks->ss, 2);
secure_write(2, " pa: ", 5);
ft_putnbr_fd((int) stacks->pa, 2);
secure_write(2, " pb: ", 5);
ft_putnbr_fd((int) stacks->pb, 2);
secure_write(2, "\n", 1);
secure_write(2, "[bench] ra: ", 12);
ft_putnbr_fd((int) stacks->ra, 2);
secure_write(2, " rb: ", 5);
ft_putnbr_fd((int) stacks->rb, 2);
secure_write(2, " rr: ", 5);
ft_putnbr_fd((int) stacks->rr, 2);
secure_write(2, " rra: ", 6);
ft_putnbr_fd((int) stacks->rra, 2);
secure_write(2, " rrb: ", 6);
ft_putnbr_fd((int) stacks->rrb, 2);
secure_write(2, " rrr: ", 6);
ft_putnbr_fd((int) stacks->rrr, 2);
secure_write(2, "\n", 1);
}
void print_bench(t_stacks *stacks)
{
print_disorder(stacks);
print_algo(stacks);
print_total_ops(stacks);
print_ops(stacks);
}

76
flags/flag.c Normal file
View File

@@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* flag.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/07 12:39:29 by mteriier #+# #+# */
/* Updated: 2026/01/08 16:07:29 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "parsing.h"
#include "flags.h"
int calcul_mod(int argc, char **argv)
{
int mod;
mod = 0;
while (argv[mod] && !ft_isdigit(argv[mod][0]) && mod < argc)
mod++;
mod--;
return (mod);
}
int pos_flag(char **argv, int mod)
{
if (mod == 1 || mod == 4)
{
if (!ft_strncmp(argv[1], "--bench", 20))
return (1);
}
else if (mod == 2 || mod == 5)
{
if (!ft_strncmp(argv[1], "--bench", 20))
return (1);
else
return (2);
}
return (0);
}
int pos_bench(char **argv, int mod)
{
if (mod == 1 || mod == 4)
{
if (ft_strncmp(argv[1], "--bench", 20))
return (1);
}
else if (mod == 2 || mod == 5)
{
if (ft_strncmp(argv[1], "--bench", 20))
return (1);
else
return (2);
}
return (0);
}
void flags(int pos, int pos_b, char **argv, t_stacks *stacks)
{
if (ft_strncmp(argv[pos], "--simple", 30) && pos > 0)
simple(stacks);
else if (ft_strncmp(argv[pos], "--medium", 30) && pos > 0)
medium(stacks);
else if (ft_strncmp(argv[pos], "--complex", 30) && pos > 0)
complex(stacks);
else if (ft_strncmp(argv[pos], "--adaptive", 30) && pos > 0)
adaptive(stacks, argv);
else
adaptive(stacks, argv);
if (pos_b > 0)
stacks->bench = 1;
}

31
ft_putnbr.c Normal file
View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/08 15:17:28 by dgaillet #+# #+# */
/* Updated: 2026/01/09 12:09:55 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void rec_ft_putnbr_fd(int nbr, int fd)
{
char c;
if (!nbr)
return ;
ft_putnbr_fd(nbr / 10, fd);
c = (nbr % 10) + '0';
secure_write(fd, &c, 1);
}
void ft_putnbr_fd(int nbr, int fd)
{
if (!nbr)
secure_write(fd, "0", 1);
rec_ft_putnbr_fd(nbr, fd);
}

23
includes/check_error.h Normal file
View File

@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_error.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/09 10:16:35 by mteriier #+# #+# */
/* Updated: 2026/01/09 10:16:37 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#ifndef CHECK_ERROR_H
# define CHECK_ERROR_H
int verif_flag(char **tab, int mod);
int check_error(char **tab, int mod);
int verif_is_digit(char **tab, int mod);
int verif_overflow(char **tab, int mod);
int verif_double(char **tab, int mod);
int scan_str_is_digit(char *tab);
#endif

26
includes/flags.h Normal file
View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* flags.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/07 13:05:52 by mteriier #+# #+# */
/* Updated: 2026/01/08 15:32:54 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#ifndef FLAGS_H
# define FLAGS_H
void simple(t_stacks *stacks);
void medium(t_stacks *stacks);
void complex(t_stacks *stacks);
void adaptive(t_stacks *stacks, char **tab);
void flags(int pos, int pos_b, char **argv, t_stacks *stacks);
int pos_bench(char **argv, int mod);
int pos_flag(char **argv, int mod);
int calcul_mod(int argc, char **argv);
void print_bench(t_stacks *stacks);
#endif

41
includes/medium_headers.h Normal file
View 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 *stacks, int max_range, int range, char c);
int stack_len(t_stack *stack);
void bucket_algo(t_stacks *stacks, t_tab *preset, int range);
/* SORT UTILS FILES */
void sort_from_left(t_stacks *stacks);
void sort_from_right(t_stacks *stacks);
void push_range_to_b(t_stacks *stacks, 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

39
includes/parsing.h Normal file
View File

@@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/07 08:03:08 by mteriier #+# #+# */
/* Updated: 2026/01/09 11:27:39 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#ifndef PARSING_H
# define PARSING_H
# include <stdlib.h>
# include "push_swap.h"
int ft_atoi(const char *nptr);
int wich_mod(int mod);
t_stacks *init_stacks(int argc, char **argv, int mod);
int ft_strncmp(const char *s1, const char *s2, int n);
size_t ft_strlen(const char *s);
char *ft_substr(char const *s, unsigned int start, size_t len);
char **ft_split(char const *s, char c);
void free_tab(char **tab);
int checker(int argc, char **argv);
int len_split(char **tab);
char *ft_itoa(int n);
int ft_isdigit(int c);
char *ft_strjoin(char const *s1, char const *s2);
int ft_strlcpy(char *dst, const char *src, int size);
int ft_strlcat(char *dst, const char *src, int size);
char *join_all(int argc, char **argv);
char **split_all(char *tab);
float compute_disorder(char **strs, int pos);
void indexer(t_stacks *stacks);
#endif

89
includes/push_swap.h Normal file
View File

@@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */
/* Updated: 2026/01/08 15:32:04 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#ifndef PUSH_SWAP_H
# define PUSH_SWAP_H
typedef struct s_stack
{
int value;
long long index;
struct s_stack *next;
struct s_stack *previous;
} t_stack;
typedef struct s_stacks
{
t_stack *a;
t_stack *b;
int algo;
int bench;
float disorder;
unsigned int ra;
unsigned int rb;
unsigned int rr;
unsigned int rra;
unsigned int rrb;
unsigned int rrr;
unsigned int pa;
unsigned int pb;
unsigned int sa;
unsigned int sb;
unsigned int ss;
} t_stacks;
/*STACK_FUNCTIONS*/
void pa(t_stacks *stacks);
void pb(t_stacks *stacks);
void rra(t_stacks *stacks);
void rrb(t_stacks *stacks);
void rrr(t_stacks *stacks);
void ra(t_stacks *stacks);
void rb(t_stacks *stacks);
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 *stacks);
/* 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);
void sort_two(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(char **tab, int len, int mod);
/* RADIX */
void radix(t_stacks *stacks);
void secure_write(int fd, char *str, int len);
void ft_putnbr_fd(int nbr, int fd);
#endif

38
main.c Normal file
View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 18:32:35 by mteriier #+# #+# */
/* Updated: 2026/01/08 15:34:59 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "parsing.h"
#include "check_error.h"
#include "flags.h"
#include <unistd.h>
int main(int argc, char **argv)
{
char **tab;
int mod;
int len;
if (argc < 2)
return (0);
tab = split_all(join_all(argc, argv));
if (!tab)
return (0);
len = len_split(tab);
mod = calcul_mod(len, tab);
if (check_error(tab, mod))
test1(tab, len, mod);
else
write(2, "Error\n", 6);
free_tab(tab);
return (0);
}

64
parsing/checker.c Normal file
View File

@@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* checker.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/08 12:37:12 by dgaillet #+# #+# */
/* Updated: 2026/01/08 13:17:36 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "parsing.h"
static int check_digits(char *str)
{
int i;
i = 0;
while (str[i])
{
if (!ft_isdigit(str[i]) && str[i] != ' ')
return (0);
i++;
}
return (1);
}
static int check_flags(char *str)
{
if (!ft_strncmp("--simple", str, ft_strlen(str)))
return (1);
else if (!ft_strncmp("--medium", str, ft_strlen(str)))
return (1);
else if (!ft_strncmp("--complex", str, ft_strlen(str)))
return (1);
else if (!ft_strncmp("--adaptative", str, ft_strlen(str)))
return (1);
return (0);
}
int checker(int argc, char **argv)
{
int i;
int methods_flag;
int bench;
bench = 0;
methods_flag = 0;
i = 0;
while (++i < argc)
{
if (check_flags(argv[1]) && !methods_flag)
methods_flag = 1;
else if (!ft_strncmp("--bench", argv[1], ft_strlen(argv[1])) && !bench)
bench = 1;
else if (check_digits(argv[1]))
continue ;
else
return (0);
}
return (1);
}

49
parsing/disorder.c Normal file
View File

@@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* disorder.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/09 11:05:37 by dgaillet #+# #+# */
/* Updated: 2026/01/09 14:53:45 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "parsing.h"
static int strs_len(char **strs)
{
int i;
i = 0;
while (strs[i])
i++;
return (i);
}
float compute_disorder(char **strs, int pos)
{
float mistakes;
float total_pairs;
int i;
int j;
mistakes = 0;
total_pairs = 0;
i = pos;
while (i < strs_len(strs))
{
j = i + 1;
while (j < strs_len(strs))
{
total_pairs += 1;
if (ft_atoi(strs[i]) > ft_atoi(strs[j]))
mistakes += 1;
j++;
}
i++;
}
return (mistakes / total_pairs);
}

42
parsing/ft_atoi.c Normal file
View 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);
}

18
parsing/ft_isdigit.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/08 14:13:54 by dgaillet #+# #+# */
/* Updated: 2026/01/08 14:13:59 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
int ft_isdigit(int c)
{
if (c >= '0' && c <= '9')
return (1);
return (0);
}

62
parsing/ft_itoa.c Normal file
View File

@@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/08 14:13:00 by dgaillet #+# #+# */
/* Updated: 2026/01/08 14:13:27 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
static size_t count_digits(int n)
{
size_t size;
size = 0;
if (n <= 0)
size++;
while (n)
{
n /= 10;
size++;
}
return (size);
}
static void insert_char(char *str, unsigned int nbr, size_t index)
{
while (nbr)
{
str[index--] = nbr % 10 + '0';
nbr /= 10;
}
}
char *ft_itoa(int n)
{
unsigned int nbr;
char *str;
size_t size;
nbr = n;
if (n < 0)
nbr = n * -1;
size = count_digits(n);
str = malloc(sizeof(char) * (size + 1));
if (!str)
return (NULL);
str[size] = '\0';
if (nbr == 0)
str[0] = '0';
else
{
if (n < 0)
str[0] = '-';
insert_char(str, nbr, size - 1);
}
return (str);
}

93
parsing/ft_split.c Normal file
View File

@@ -0,0 +1,93 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/11 15:34:04 by mteriier #+# #+# */
/* Updated: 2025/11/13 14:55:21 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
static size_t count_words(char const *s, char c)
{
size_t i;
size_t len;
size_t tester;
i = 0;
len = 0;
while (s[i])
{
while (s[i] == c && s[i])
i++;
tester = 0;
while (s[i] != c && s[i])
{
tester++;
i++;
}
if (tester > 0)
len++;
}
return (len);
}
static size_t len_word(size_t i, char const *s, char c)
{
size_t len;
len = 0;
while (s[i])
{
while (s[i] != c && s[i])
{
len++;
i++;
}
return (len);
}
return (len);
}
void free_tab(char **tab)
{
int i;
i = 0;
while (tab[i])
free(tab[i++]);
free(tab);
}
char **ft_split(char const *s, char c)
{
size_t i;
size_t j;
char **big_tab;
big_tab = malloc((count_words(s, c) + 1) * sizeof(char *));
if (!big_tab)
return (0);
i = 0;
j = 0;
while (s[i])
{
while (s[i] == c && s[i])
i++;
if (s[i])
{
big_tab[j] = ft_substr(s, i, len_word(i, s, c));
if (!big_tab[j])
return (free_tab(big_tab), NULL);
j++;
}
while (s[i] != c && s[i])
i++;
}
big_tab[j] = 0;
return (big_tab);
}

27
parsing/ft_strjoin.c Normal file
View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/11 14:01:54 by mteriier #+# #+# */
/* Updated: 2025/11/12 08:34:59 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
char *ft_strjoin(char const *s1, char const *s2)
{
int total_len;
char *tmp;
total_len = ft_strlen(s1) + ft_strlen(s2);
tmp = malloc((total_len + 1) * sizeof(char));
if (!tmp)
return (0);
ft_strlcpy(tmp, s1, total_len + 1);
ft_strlcat(tmp, s2, total_len + 1);
return (tmp);
}

38
parsing/ft_strlcat.c Normal file
View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/10 09:22:05 by mteriier #+# #+# */
/* Updated: 2025/11/13 11:03:31 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
int ft_strlcat(char *dst, const char *src, int size)
{
int i;
int j;
int len_src;
int len_dst;
i = 0;
len_src = ft_strlen(src);
if (size == 0)
return (len_src);
j = ft_strlen(dst);
len_dst = ft_strlen(dst);
while (j < size - 1 && src[i])
{
dst[j] = src[i];
j++;
i++;
}
dst[j] = '\0';
if (len_dst > size)
return (len_src + size);
return (len_dst + len_src);
}

31
parsing/ft_strlcpy.c Normal file
View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/06 16:33:57 by mteriier #+# #+# */
/* Updated: 2025/11/13 10:58:32 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
int ft_strlcpy(char *dst, const char *src, int size)
{
int i;
int len;
len = ft_strlen(src);
i = 0;
if (size == 0)
return (len);
while (i < size - 1 && src[i])
{
dst[i] = src[i];
i++;
}
dst[i] = '\0';
return (len);
}

33
parsing/ft_strlen.c Normal file
View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/06 13:25:17 by mteriier #+# #+# */
/* Updated: 2025/11/13 11:24:39 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <string.h>
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i])
i++;
return (i);
}
int len_split(char **tab)
{
int i;
i = 0;
while (tab[i])
i++;
return (i);
}

30
parsing/ft_strncmp.c Normal file
View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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;
int res;
i = 0;
if (n == 0)
return (0);
while (i < n - 1 && s1[i] && s1[i] == s2[i])
{
i++;
}
res = (unsigned char)s1[i] - (unsigned char)s2[i];
if (res == 0)
return (1);
else
return (0);
}

40
parsing/ft_substr.c Normal file
View File

@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/11 13:46:41 by mteriier #+# #+# */
/* Updated: 2025/11/13 13:42:37 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t len_s;
size_t i;
char *tmp;
if (!s)
return (0);
len_s = ft_strlen(s);
if (len_s < start)
len = 0;
if (len > len_s - start)
len = len_s - start;
tmp = malloc((len + 1) * sizeof(char));
if (!tmp)
return (0);
i = 0;
while (i < len)
{
tmp[i] = s[start];
i++;
start++;
}
tmp[i] = '\0';
return (tmp);
}

54
parsing/indexer.c Normal file
View File

@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* indexer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/14 10:14:06 by dgaillet #+# #+# */
/* Updated: 2026/01/14 10:14:08 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static t_stack *first_without_index(t_stack *stack)
{
t_stack *first;
if (stack->index < 0)
return (stack);
first = stack;
stack = stack->next;
while (stack != first)
{
if (stack->index < 0)
return (stack);
stack = stack->next;
}
return ((void *) 0);
}
void indexer(t_stacks *stacks)
{
int i;
int len;
t_stack *lower;
t_stack *temp;
i = 0;
len = stack_a_len(stacks);
while (i < len)
{
lower = first_without_index(stacks->a);
temp = lower->next;
while (temp != stacks->a)
{
if (lower->value > temp->value && temp->index < 0)
lower = temp;
temp = temp->next;
}
lower->index = i;
i++;
}
}

84
parsing/parsing.c Normal file
View File

@@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 16:21:05 by mteriier #+# #+# */
/* Updated: 2026/01/08 14:01:23 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "parsing.h"
int wich_mod(int mod)
{
return (mod % 3 + 1);
}
static t_stack *parsing(int argc, char **argv, int mod)
{
int i;
int stock;
t_stack *first;
t_stack *new;
i = wich_mod(mod);
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);
}
static void set_t_stacks(t_stacks *stacks)
{
stacks->a = NULL;
stacks->b = NULL;
stacks->algo = 0;
stacks->bench = 0;
stacks->disorder = 0;
stacks->sa = 0;
stacks->sb = 0;
stacks->ss = 0;
stacks->pa = 0;
stacks->pb = 0;
stacks->ra = 0;
stacks->rb = 0;
stacks->rr = 0;
stacks->rra = 0;
stacks->rrb = 0;
stacks->rrr = 0;
}
t_stacks *init_stacks(int argc, char **argv, int mod)
{
t_stacks *stacks;
t_stack *a;
stacks = malloc(sizeof(t_stacks));
if (!stacks)
return (NULL);
set_t_stacks(stacks);
a = parsing(argc, argv, mod);
if (!a)
{
free(stacks);
return (NULL);
}
stacks->a = a;
return (stacks);
}

64
parsing/parsing_2.c Normal file
View File

@@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/09 08:22:21 by mteriier #+# #+# */
/* Updated: 2026/01/09 08:22:22 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
static char *free_join(char *tab)
{
free(tab);
return (NULL);
}
static char *join_utils(char *start_tab, char *end_str)
{
char *tab;
char *tmp2;
tmp2 = ft_strjoin(end_str, " ");
if (!tmp2)
return (free_join(start_tab));
tab = ft_strjoin(start_tab, tmp2);
free(start_tab);
free(tmp2);
return (tab);
}
char *join_all(int argc, char **argv)
{
char *tab;
int i;
i = 0;
tab = ft_strjoin(argv[i], " ");
i++;
if (!tab)
return (NULL);
while (i < argc)
{
tab = join_utils(tab, argv[i]);
i++;
}
return (tab);
}
char **split_all(char *tab)
{
char **split;
if (!tab)
return (NULL);
split = ft_split(tab, ' ');
free (tab);
if (!split)
return (NULL);
return (split);
}

22
secure_write.c Normal file
View File

@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* secure_write.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/08 14:30:38 by dgaillet #+# #+# */
/* Updated: 2026/01/09 14:55:16 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdlib.h>
void secure_write(int fd, char *str, int len)
{
if (len < 0)
return ;
if (!str || write(fd, str, len) < 0)
exit (EXIT_FAILURE);
}

View File

@@ -6,28 +6,50 @@
/* 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: 2026/01/08 13:57:56 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);
stacks->pa++;
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);
stacks->pb++;
write(1, "pb\n", 3);
}

View File

@@ -6,26 +6,35 @@
/* 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: 2026/01/08 13:57:27 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;
stacks->rra++;
write(1, "rra\n", 4);
}
void rrb(t_stacks *stacks)
{
if (stacks && stacks->b && stacks->b->previous)
stacks->b = stacks->b->previous;
stacks->rrb++;
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;
stacks->rrr++;
write(1, "rrr\n", 4);
}

View File

@@ -6,26 +6,35 @@
/* 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: 2026/01/08 13:57:47 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;
stacks->ra++;
write(1, "ra\n", 3);
}
void rb(t_stacks *stacks)
{
if (stacks && stacks->b && stacks->b->next)
stacks->b = stacks->b->next;
stacks->rb++;
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;
stacks->rr++;
write(1, "rr\n", 3);
}

View File

@@ -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 */
/* */
/* ************************************************************************** */
@@ -21,6 +21,7 @@ t_stack *new_stack(int value)
if (!new)
return (NULL);
new->value = value;
new->index = -1;
new->next = NULL;
new->previous = NULL;
return (new);
@@ -30,7 +31,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 +40,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 +48,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;

View File

@@ -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 *stacks)
{
if (stacks->a)
stack_clear_all(stacks->a, stacks->a);
if (stacks->b)
stack_clear_all(stacks->b, stacks->b);
free(stacks);
}

63
stack_utils/stacks_len.c Normal file
View 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);
}

View File

@@ -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: 2026/01/08 13:58:13 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,13 @@ void sa(t_stacks *stacks)
stock = a->value;
a->value = a->next->value;
a->next->value = stock;
stacks->sa++;
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 +39,32 @@ void sb(t_stacks *stacks)
stock = b->value;
b->value = b->next->value;
b->next->value = stock;
stacks->sb++;
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;
}
stacks->ss++;
write(1, "ss\n", 3);
}

40
test_one.c Normal file
View File

@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_one.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.lyon42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/22 12:33:58 by mteriier #+# #+# */
/* Updated: 2026/01/08 15:35:43 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "flags.h"
#include "parsing.h"
#include "medium_headers.h"
int test1(char **tab, int len, int mod)
{
t_stacks *stacks;
stacks = NULL;
if (mod == -1)
return (0);
stacks = init_stacks(len, tab, mod);
indexer(stacks);
if (!stacks)
return (0);
if (check_order(stacks->a))
{
free_all(stacks);
return (0);
}
stacks->disorder = compute_disorder(tab, wich_mod(mod));
flags(pos_flag(tab, mod), pos_bench(tab, mod), tab, stacks);
if (stacks->bench == 1)
print_bench(stacks);
free_all(stacks);
return (0);
}