mirror of
https://github.com/DavidGailleton/42-Push_Swap.git
synced 2026-01-27 08:41:58 +00:00
Compare commits
38 Commits
medium_alg
...
playtest
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3887e3b4a5 | ||
|
|
a3a462a515 | ||
|
|
587ea1750c | ||
|
|
afa8785379 | ||
| 0e7d1dce50 | |||
|
|
469320374d | ||
| ddd5b11a7a | |||
|
|
854d699780 | ||
|
|
52d868904f | ||
|
|
81ed92dc88 | ||
|
|
c6f04ab48f | ||
|
|
e266b6d6e9 | ||
|
|
59a80e6c9b | ||
| e9c0894f92 | |||
|
|
381bd66c2b | ||
| c2e7da01f4 | |||
|
|
75c331e7ea | ||
| 78ec8a3fa4 | |||
|
|
c323449e3a | ||
|
|
e0712aa8c4 | ||
| 2c805c9506 | |||
|
|
6b5347c997 | ||
|
|
89b7709d2d | ||
|
|
00af75f98f | ||
|
|
29c60fef6a | ||
| 60011b1b11 | |||
| f92eba4f03 | |||
| 1ebb32f58d | |||
| 52220b5b49 | |||
|
|
e8b2a90dc1 | ||
|
|
68dbe33733 | ||
| 82111f59ad | |||
|
|
fe69eeb821 | ||
| f4824f2984 | |||
| 5ae963bb09 | |||
| 15ca9cfe3f | |||
| 929d7d4da5 | |||
| 319ed2521e |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -59,6 +59,8 @@ dkms.conf
|
||||
|
||||
# Executable
|
||||
push_swap
|
||||
checker
|
||||
checker_linux
|
||||
|
||||
# File obj
|
||||
obj/
|
||||
|
||||
59
Makefile
59
Makefile
@@ -18,7 +18,7 @@ COMPLEX_DIR = radix
|
||||
|
||||
FLAGS_DIR = flags
|
||||
|
||||
CHECKER_DIR = checker
|
||||
CHECKER_DIR = check_error
|
||||
|
||||
INCLUDES = includes
|
||||
|
||||
@@ -26,20 +26,21 @@ INCLUDES = includes
|
||||
# ALL FILES WITHOUT PATH
|
||||
#============================
|
||||
|
||||
SRC = main.c test_one.c ft_putnbr.c secure_write.c
|
||||
SRC = main.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
|
||||
PARSING = ft_atoi.c parsing.c ft_strncmp.c ft_split.c ft_strlen.c ft_substr.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
|
||||
MEDIUM_ALGO = utils_medium.c utils_struct_tab.c utils_medium_two.c sort_utils.c sort_utils_two.c medium_algo.c \
|
||||
move_patha.c move_pathb.c
|
||||
|
||||
COMPLEX_ALGO = radix.c
|
||||
|
||||
@@ -58,14 +59,40 @@ OBJ_DIR = obj
|
||||
|
||||
CC = cc
|
||||
|
||||
CFLAGS = -Wall -Werror -Wextra -g3 -I$(INCLUDES)
|
||||
CFLAGS = -Wall -Werror -Wextra -I$(INCLUDES)
|
||||
|
||||
NAME = push_swap
|
||||
|
||||
OBJ = $(addprefix $(OBJ_DIR)/, $(notdir $(ALL_FILES:.c=.o)))
|
||||
DEP = $(OBJ:.o=.d)
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
#============================
|
||||
# BONUS CONFIG
|
||||
#============================
|
||||
|
||||
BONUS_DIR = bonus
|
||||
|
||||
GNL_D = GNL
|
||||
|
||||
GNL_DIR = bonus/GNL
|
||||
|
||||
BONUS_FILES = checker_bonus.c
|
||||
|
||||
GNL_FILES = get_next_line.c get_next_line_utils.c
|
||||
|
||||
SRC_FOR_BONUS = ft_putnbr.c secure_write.c
|
||||
|
||||
ALL_BONUS_FILES = $(SRC_FOR_BONUS) $(BONUS_DIR)/$(BONUS_FILES) $(BONUS_DIR)/$(GNL_D)/$(GNL_FILES) \
|
||||
$(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)
|
||||
|
||||
BONUS_OBJ = $(addprefix $(OBJ_DIR)/, $(notdir $(ALL_BONUS_FILES:.c=.o)))
|
||||
|
||||
NAME_BONUS = checker
|
||||
|
||||
.PHONY: all clean fclean re bonus
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
@@ -75,6 +102,12 @@ $(NAME): $(OBJ)
|
||||
@echo "======= PUSH SWAP COMPILED ========="
|
||||
@echo "===================================="
|
||||
|
||||
$(NAME_BONUS): $(BONUS_OBJ)
|
||||
@$(CC) $(CFLAGS) $(BONUS_OBJ) -o $(NAME_BONUS)
|
||||
@echo "===================================="
|
||||
@echo "======= CHECKER COMPILED ==========="
|
||||
@echo "===================================="
|
||||
|
||||
$(OBJ_DIR)/%.o: $(PARS_DIR)/%.c | $(OBJ_DIR)
|
||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||
|
||||
@@ -102,9 +135,18 @@ $(OBJ_DIR)/%.o: $(FLAGS_DIR)/%.c | $(OBJ_DIR)
|
||||
$(OBJ_DIR)/%.o: $(CHECKER_DIR)/%.c | $(OBJ_DIR)
|
||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||
|
||||
$(OBJ_DIR)/%.o: $(BONUS_DIR)/%.c | $(OBJ_DIR)
|
||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||
|
||||
$(OBJ_DIR)/%.o: $(BONUS_DIR)/$(GNL_D)/%.c | $(OBJ_DIR)
|
||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||
|
||||
$(OBJ_DIR)/%.o: %.c | $(OBJ_DIR)
|
||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||
|
||||
$(OBJ_DIR_BONUS)/%.o: %.c | $(OBJ_DIR_BONUS)
|
||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||
|
||||
$(OBJ_DIR):
|
||||
@mkdir -p $(OBJ_DIR)
|
||||
|
||||
@@ -116,9 +158,12 @@ clean:
|
||||
|
||||
fclean: clean
|
||||
@rm -f $(NAME)
|
||||
@rm -f $(NAME_BONUS)
|
||||
@echo "========== EXEC DELETED ============"
|
||||
@echo "===================================="
|
||||
|
||||
re: fclean all
|
||||
|
||||
bonus: $(NAME_BONUS)
|
||||
|
||||
-include $(DEP)
|
||||
|
||||
83
README.md
83
README.md
@@ -1,5 +1,84 @@
|
||||
This project has been created as part of the 42 curriculum by mteriier[, dgaillet].
|
||||
*This project has been created as part of the 42 curriculum by dgaillet[, mteriier].*
|
||||
|
||||
## Description
|
||||
|
||||
The *Push swap* project goal is to sort values with push swap operations beetween 2 stack (*a* and *b*)
|
||||
The **Push swap** project goal is to sort values with push swap operations beetween 2 stack (*a* and *b*)
|
||||
with different algorithms. Depends on the difficulties to solve the stacks. We will select a certain algorithms.
|
||||
|
||||
The difficulty is calculate with a disorder function *(compute_disorder)*. Disorder result will be between 0 and 1.
|
||||
|
||||
Selected algo depends on the disorder:
|
||||
|
||||
**Disorder < 0.2 ( O(n2) ):**
|
||||
- Insertion sort adaptation ( len = 500 > complexity = 30 000)
|
||||
|
||||
**0.2 >= Disorder < 0.5 ( O(n√n) ):**
|
||||
- Bucket sort adaptations with √n buckets ( len = 500 > complexity = 11900)
|
||||
|
||||
**Disorder >= 0.5 ( O(n log n) ):**
|
||||
- Radix sort adaptation (LSD or MSD) ( len = 500 > complexity = 8000)
|
||||
|
||||
## Instructions
|
||||
|
||||
### 1. Command to compile push_swap :
|
||||
```
|
||||
make
|
||||
```
|
||||
### 2. Command to compile bonus :
|
||||
|
||||
```
|
||||
make bonus
|
||||
```
|
||||
### 3. Command to delete all objects and exec :
|
||||
|
||||
```
|
||||
make fclean
|
||||
```
|
||||
### 3. Command exemples how to use push_swap :
|
||||
|
||||
```
|
||||
./push_swap 7 5 8 12 30 -20
|
||||
```
|
||||
```
|
||||
ARG="7 5 8 12 30 -20"; ./push_swap $ARG
|
||||
```
|
||||
|
||||
### 4. Command exemples how to use checker :
|
||||
|
||||
```
|
||||
./checker 7 5 8 12 30 -20
|
||||
(put the command like pa,ra,rr,rrr ...)
|
||||
```
|
||||
### 5. Command exemples how to use checker and push_swap together :
|
||||
|
||||
```
|
||||
ARG="7 5 8 12 30 -20"; ./push_swap $ARG | ./checker $ARG
|
||||
```
|
||||
|
||||
## Resources
|
||||
|
||||
- **For the algorithms**
|
||||
https://en.wikipedia.org/wiki/Sorting_algorithm
|
||||
|
||||
- **For BUCKET SORT**
|
||||
https://www.meilisearch.com/blog/bucket-sort-guide
|
||||
|
||||
- **For RADIX**
|
||||
https://www.geeksforgeeks.org/dsa/radix-sort/
|
||||
|
||||
### AI
|
||||
|
||||
AI was utilized to provide a reminder on how to include obj files within a subfolder for the Makefile.
|
||||
|
||||
## Additional sections may be required depending on the project
|
||||
|
||||
### Justification to why we chose this algorithms
|
||||
- **Insertion sort**
|
||||
We select **Insertion** cause that was the more simple to implement with the restrictions.
|
||||
|
||||
- **Bucket sort**
|
||||
We select **Bucket** cause i like how the algorithms sort.
|
||||
Define the range and the number of buckets.
|
||||
|
||||
- **Radix sort**
|
||||
We select **Radix** cause that was the more simple to implement with the restrictions.
|
||||
|
||||
@@ -14,67 +14,6 @@
|
||||
#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;
|
||||
@@ -95,7 +34,7 @@ void bucket_algo(t_stacks *stacks, t_tab *preset, int range)
|
||||
t_tab *tmp;
|
||||
|
||||
tmp = preset;
|
||||
while (stacks->a)
|
||||
while(stacks->a)
|
||||
pb(stacks);
|
||||
while (preset)
|
||||
{
|
||||
@@ -108,7 +47,5 @@ void bucket_algo(t_stacks *stacks, t_tab *preset, int range)
|
||||
free(preset);
|
||||
preset = tmp;
|
||||
}
|
||||
while (stacks->b)
|
||||
pa(stacks);
|
||||
return ;
|
||||
}
|
||||
|
||||
94
algorithms/medium/move_patha.c
Normal file
94
algorithms/medium/move_patha.c
Normal file
@@ -0,0 +1,94 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* move_patha.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/16 06:20:47 by mteriier #+# #+# */
|
||||
/* Updated: 2026/01/16 06:20:55 by mteriier ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "medium_headers.h"
|
||||
#include "push_swap.h"
|
||||
#include <stdio.h>
|
||||
static int is_border_a(int value, t_stacks *stacks)
|
||||
{
|
||||
t_stack *tmp;
|
||||
|
||||
if (!stacks->a)
|
||||
return (1);
|
||||
tmp = assign_stack(stacks, 'a');
|
||||
if ((value < tmp->index && check_order(tmp))
|
||||
|| (value > tmp->index && value > tmp->previous->index
|
||||
&& check_order(stacks->a)))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int move_left_to_right(int value, t_stacks *stacks, int is_move)
|
||||
{
|
||||
int i;
|
||||
t_stack *tmp;
|
||||
|
||||
i = 0;
|
||||
tmp = assign_stack(stacks, 'a');
|
||||
while (value > tmp->index && !is_move)
|
||||
{
|
||||
tmp = tmp->next;
|
||||
i++;
|
||||
}
|
||||
while (value > stacks->a->index && is_move)
|
||||
ra(stacks);
|
||||
return (i);
|
||||
}
|
||||
|
||||
int move_right_to_left(int value, t_stacks *stacks, int is_move)
|
||||
{
|
||||
int i;
|
||||
t_stack *tmp;
|
||||
|
||||
tmp = assign_stack(stacks, 'a');
|
||||
i = 0;
|
||||
while (value < tmp->previous->index && !is_move)
|
||||
{
|
||||
tmp = tmp->previous;
|
||||
i++;
|
||||
}
|
||||
while (value < stacks->a->previous->index && is_move)
|
||||
rra(stacks);
|
||||
return (i);
|
||||
}
|
||||
|
||||
int wich_path_a(int value, t_stacks *stacks)
|
||||
{
|
||||
t_stack *tmp;
|
||||
int move_from_left;
|
||||
int move_from_right;
|
||||
|
||||
tmp = assign_stack(stacks, 'a');
|
||||
if (is_border_a(value, stacks))
|
||||
return (-1);
|
||||
move_from_left = move_left_to_right(value, stacks, 0);
|
||||
move_from_right = move_right_to_left(value, stacks, 0);
|
||||
if ((check_order(stacks->a) && move_from_left < move_from_right)
|
||||
|| (move_from_left > move_from_right && !check_order(stacks->a)))
|
||||
return (1);
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
|
||||
void move_ra_rra(int value, t_stacks *stacks)
|
||||
{
|
||||
if (is_border_a(value, stacks))
|
||||
return ;
|
||||
if (wich_path_a(value, stacks) == 1)
|
||||
{
|
||||
move_left_to_right(value, stacks, 1);
|
||||
}
|
||||
else if (wich_path_a(value, stacks) == 0)
|
||||
{
|
||||
move_right_to_left(value, stacks, 1);
|
||||
}
|
||||
}
|
||||
113
algorithms/medium/move_pathb.c
Normal file
113
algorithms/medium/move_pathb.c
Normal file
@@ -0,0 +1,113 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* move_pathb.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/16 06:20:38 by mteriier #+# #+# */
|
||||
/* Updated: 2026/01/16 06:20:42 by mteriier ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "medium_headers.h"
|
||||
#include "push_swap.h"
|
||||
|
||||
static int path_to_end(t_stacks *stacks, int max_range, int range, char c)
|
||||
{
|
||||
t_stack *tmp;
|
||||
t_stack *start;
|
||||
int i;
|
||||
int first_pass;
|
||||
|
||||
tmp = assign_stack(stacks, c);
|
||||
start = assign_stack(stacks, c);
|
||||
tmp = tmp->previous;
|
||||
i = 0;
|
||||
first_pass = 1;
|
||||
while (first_pass || tmp != start->previous)
|
||||
{
|
||||
first_pass = 0;
|
||||
if (in_range(tmp->index, max_range, range))
|
||||
tmp = start;
|
||||
tmp = tmp->previous;
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
static int path_to_start(t_stacks *stacks, int max_range, int range, char c)
|
||||
{
|
||||
t_stack *tmp;
|
||||
t_stack *start;
|
||||
int i;
|
||||
int first_pass;
|
||||
|
||||
tmp = assign_stack(stacks, c);
|
||||
start = assign_stack(stacks, c);
|
||||
i = 0;
|
||||
first_pass = 1;
|
||||
while (first_pass || tmp != start)
|
||||
{
|
||||
first_pass = 0;
|
||||
if (in_range(tmp->index, max_range, range))
|
||||
{
|
||||
tmp = start->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);
|
||||
}
|
||||
|
||||
void normal_move_path(t_stacks *stacks, t_tab *one_preset, int range)
|
||||
{
|
||||
if (wich_path(stacks, one_preset->max_range, range, 'b'))
|
||||
{
|
||||
while (!in_range(stacks->b->index, one_preset->max_range, range))
|
||||
rb(stacks);
|
||||
}
|
||||
else
|
||||
{
|
||||
while (!in_range(stacks->b->index, one_preset->max_range, range))
|
||||
{
|
||||
rrb(stacks);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int get_pre_move_b(t_stacks *stacks, t_tab *one_preset, int range)
|
||||
{
|
||||
int i;
|
||||
t_stack *tmp;
|
||||
|
||||
i = 0;
|
||||
tmp = assign_stack(stacks, 'b');
|
||||
if (wich_path(stacks, one_preset->max_range, range, 'b'))
|
||||
{
|
||||
while (!in_range(tmp->index, one_preset->max_range, range))
|
||||
{
|
||||
i++;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
}
|
||||
else
|
||||
while (!in_range(tmp->index, one_preset->max_range, range))
|
||||
{
|
||||
i++;
|
||||
tmp = tmp->previous;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
@@ -12,84 +12,81 @@
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "medium_headers.h"
|
||||
|
||||
static int number_move_reverse(t_stacks *stacks, int value, char start_end)
|
||||
#include <stdio.h>
|
||||
void move_rr_rrr(t_stacks *stacks, int move, char c)
|
||||
{
|
||||
int i;
|
||||
t_stack *tmp;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
tmp = stacks->a;
|
||||
if (start_end == 's')
|
||||
while (i < move && c == 's')
|
||||
{
|
||||
while (tmp->value < value)
|
||||
{
|
||||
tmp = tmp->next;
|
||||
i++;
|
||||
}
|
||||
rr(stacks);
|
||||
i++;
|
||||
}
|
||||
else
|
||||
while (i < move && c == 'e')
|
||||
{
|
||||
tmp = tmp->previous;
|
||||
while (tmp->value > value)
|
||||
{
|
||||
tmp = tmp->previous;
|
||||
i++;
|
||||
}
|
||||
i++;
|
||||
rrr(stacks);
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
static int sort_path(t_stacks *stacks, int value)
|
||||
void put_in_order_ra_rra(t_stacks *stacks)
|
||||
{
|
||||
int start_path;
|
||||
int end_path;
|
||||
int i;
|
||||
|
||||
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)
|
||||
i = 0;
|
||||
while (!check_order(stacks->a) && i < 50)
|
||||
{
|
||||
pa(stacks);
|
||||
return ;
|
||||
}
|
||||
if (stacks->a->previous->value < stacks->b->value)
|
||||
{
|
||||
pa(stacks);
|
||||
ra(stacks);
|
||||
return ;
|
||||
i++;
|
||||
}
|
||||
if (sort_path(stacks, stacks->b->value))
|
||||
sort_from_left(stacks);
|
||||
else
|
||||
sort_from_right(stacks);
|
||||
}
|
||||
|
||||
void path_rr_rrr(int value, t_stacks *stacks, t_tab *one_preset, int range)
|
||||
{
|
||||
int move_a;
|
||||
int final_move;
|
||||
|
||||
final_move = get_pre_move_b(stacks, one_preset, range);
|
||||
if (wich_path_a(value, stacks) == 1
|
||||
&& wich_path(stacks, one_preset->max_range, range, 'b'))
|
||||
{
|
||||
move_a = move_left_to_right(value, stacks, 0);
|
||||
if (move_a < final_move)
|
||||
final_move = move_a;
|
||||
move_rr_rrr(stacks, final_move, 's');
|
||||
}
|
||||
else if (wich_path_a(value, stacks) == 0
|
||||
&& !wich_path(stacks, one_preset->max_range, range, 'b'))
|
||||
{
|
||||
move_a = move_right_to_left(value, stacks, 0);
|
||||
if (move_a < final_move)
|
||||
final_move = move_a;
|
||||
move_rr_rrr(stacks, final_move, 'e');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void push_range_to_b(t_stacks *stacks, t_tab *one_preset, int range)
|
||||
{
|
||||
int value;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (one_preset->nb_in > 0)
|
||||
{
|
||||
if (wich_path(stacks, one_preset->max_range, range, 'b'))
|
||||
if (stacks->a)
|
||||
{
|
||||
while (!in_range(stacks->b->value, one_preset->max_range, range))
|
||||
rb(stacks);
|
||||
value = get_value_finded(stacks, one_preset, range);
|
||||
path_rr_rrr(value, stacks, one_preset, range);
|
||||
}
|
||||
else
|
||||
{
|
||||
while (!in_range(stacks->b->value, one_preset->max_range, range))
|
||||
{
|
||||
rrb(stacks);
|
||||
}
|
||||
}
|
||||
sort_little_pile(stacks);
|
||||
normal_move_path(stacks, one_preset, range);
|
||||
move_ra_rra(stacks->b->index, stacks);
|
||||
pa(stacks);
|
||||
put_in_order_ra_rra(stacks);
|
||||
one_preset->nb_in--;
|
||||
i++;
|
||||
if (!check_order(stacks->a))
|
||||
return ;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,39 +11,33 @@
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "medium_headers.h"
|
||||
|
||||
void sort_from_left(t_stacks *stacks)
|
||||
int get_value_finded(t_stacks *stacks, t_tab *one_preset, int range)
|
||||
{
|
||||
int i;
|
||||
int value;
|
||||
t_stack *tmp;
|
||||
|
||||
i = 0;
|
||||
while (stacks->b->value > stacks->a->value)
|
||||
tmp = assign_stack(stacks, 'b');
|
||||
if (wich_path(stacks, one_preset->max_range, range, 'b'))
|
||||
{
|
||||
ra(stacks);
|
||||
i++;
|
||||
}
|
||||
pa(stacks);
|
||||
while (i > 0)
|
||||
{
|
||||
rra(stacks);
|
||||
i--;
|
||||
while (!in_range(tmp->index, one_preset->max_range, range))
|
||||
tmp = tmp->next;
|
||||
}
|
||||
else
|
||||
while (!in_range(tmp->index, one_preset->max_range, range))
|
||||
tmp = tmp->previous;
|
||||
value = tmp->index;
|
||||
return (value);
|
||||
}
|
||||
|
||||
void sort_from_right(t_stacks *stacks)
|
||||
t_stack *assign_stack(t_stacks *stacks, char c)
|
||||
{
|
||||
int i;
|
||||
t_stack *tmp;
|
||||
|
||||
i = 0;
|
||||
while (stacks->b->value < stacks->a->previous->value)
|
||||
{
|
||||
rra(stacks);
|
||||
i++;
|
||||
}
|
||||
pa(stacks);
|
||||
while (i >= 0)
|
||||
{
|
||||
ra(stacks);
|
||||
i--;
|
||||
}
|
||||
if (c == 'a')
|
||||
tmp = stacks->a;
|
||||
else
|
||||
tmp = stacks->b;
|
||||
return (tmp);
|
||||
}
|
||||
|
||||
@@ -19,13 +19,13 @@ int get_first_lower(t_stack *first)
|
||||
int pass;
|
||||
|
||||
tmp = first;
|
||||
lower = tmp->value;
|
||||
lower = tmp->index;
|
||||
pass = 1;
|
||||
while (tmp != first || pass == 1)
|
||||
{
|
||||
pass = 0;
|
||||
if (lower > tmp->value)
|
||||
lower = tmp->value;
|
||||
if (lower > tmp->index)
|
||||
lower = tmp->index;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return (lower);
|
||||
@@ -43,9 +43,9 @@ int get_next_lower(t_stack *first, int old_lower)
|
||||
while (tmp != first || skip_first)
|
||||
{
|
||||
skip_first = 0;
|
||||
if (old_lower < tmp->value && tmp->value <= next_lower)
|
||||
if (old_lower < tmp->index && tmp->index <= next_lower)
|
||||
{
|
||||
next_lower = tmp->value;
|
||||
next_lower = tmp->index;
|
||||
if (next_lower == -2147483648)
|
||||
return (next_lower);
|
||||
}
|
||||
@@ -96,7 +96,7 @@ int get_number_in_range(int max_range, t_stack *a, int range)
|
||||
while (tmp != first || pass == 1)
|
||||
{
|
||||
pass = 0;
|
||||
if (in_range(tmp->value, max_range, range))
|
||||
if (in_range(tmp->index, max_range, range))
|
||||
nb_in++;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
|
||||
@@ -20,13 +20,13 @@ static int get_max_number(t_stack *first)
|
||||
t_stack *a;
|
||||
|
||||
a = first;
|
||||
max = a->value;
|
||||
max = a->index;
|
||||
pass = 1;
|
||||
while (a != first || pass == 1)
|
||||
{
|
||||
pass = 0;
|
||||
if (max < a->value)
|
||||
max = a->value;
|
||||
if (max < a->index)
|
||||
max = a->index;
|
||||
a = a->next;
|
||||
}
|
||||
return (max);
|
||||
@@ -39,13 +39,13 @@ static int get_min_number(t_stack *first)
|
||||
int pass;
|
||||
|
||||
a = first;
|
||||
min = a->value;
|
||||
min = a->index;
|
||||
pass = 1;
|
||||
while (a != first || pass == 1)
|
||||
{
|
||||
pass = 0;
|
||||
if (min > a->value)
|
||||
min = a->value;
|
||||
if (min > a->index)
|
||||
min = a->index;
|
||||
a = a->next;
|
||||
}
|
||||
return (min);
|
||||
|
||||
@@ -12,17 +12,17 @@
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
static int still_unit_value(t_stacks *stacks, int unit)
|
||||
static int still_unit_index(t_stacks *stacks, int unit)
|
||||
{
|
||||
t_stack *temp;
|
||||
|
||||
temp = stacks->a;
|
||||
if (!temp || temp->value >= unit)
|
||||
if (!temp || temp->index >= unit)
|
||||
return (1);
|
||||
temp = temp->next;
|
||||
while (temp != stacks->a)
|
||||
{
|
||||
if (temp->value >= unit)
|
||||
if (temp->index >= unit)
|
||||
return (1);
|
||||
temp = temp->next;
|
||||
}
|
||||
@@ -33,19 +33,12 @@ 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;
|
||||
}
|
||||
s_len = stack_a_len(stacks);
|
||||
i = 0;
|
||||
while (i < s_len && still_unit_value(stacks, unit))
|
||||
while (i < s_len && still_unit_index(stacks, unit))
|
||||
{
|
||||
if (stacks->a && (stacks->a->value % (unit * 10)) / unit == nb)
|
||||
if (stacks->a && (stacks->a->index % (unit * 10)) / unit == nb)
|
||||
pb(stacks);
|
||||
else
|
||||
ra(stacks);
|
||||
@@ -58,7 +51,7 @@ static void rec_sort(t_stacks *stacks, int unit)
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (!still_unit_value(stacks, unit))
|
||||
if (!still_unit_index(stacks, unit))
|
||||
return ;
|
||||
while (i <= 9)
|
||||
{
|
||||
|
||||
112
bonus/GNL/get_next_line.c
Normal file
112
bonus/GNL/get_next_line.c
Normal file
@@ -0,0 +1,112 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/21 17:20:41 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/11/28 13:47:15 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line.h"
|
||||
#include "parsing.h"
|
||||
#include <unistd.h>
|
||||
|
||||
static int del_before_nl(char buf[BUFFER_SIZE])
|
||||
{
|
||||
int nl_i;
|
||||
int i;
|
||||
char *temp;
|
||||
|
||||
nl_i = index_of_nl(buf, BUFFER_SIZE);
|
||||
if (nl_i >= 0)
|
||||
{
|
||||
temp = ft_substr_gnl(buf, nl_i + 1, BUFFER_SIZE - nl_i);
|
||||
if (!temp)
|
||||
return (-1);
|
||||
ft_bzero(buf, BUFFER_SIZE);
|
||||
i = 0;
|
||||
while (temp[i])
|
||||
{
|
||||
buf[i] = temp[i];
|
||||
i++;
|
||||
}
|
||||
free(temp);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
static char *ft_read_one(char buf[BUFFER_SIZE], int fd, char *str)
|
||||
{
|
||||
int temp;
|
||||
|
||||
str = ft_strjoin_new(str, buf, BUFFER_SIZE);
|
||||
if (!str)
|
||||
return (NULL);
|
||||
ft_bzero(buf, BUFFER_SIZE);
|
||||
temp = read(fd, buf, BUFFER_SIZE);
|
||||
if (temp < 0)
|
||||
{
|
||||
free(str);
|
||||
return (NULL);
|
||||
}
|
||||
if ((!str || ft_strlen_1(str) == 0) && temp == 0)
|
||||
{
|
||||
free(str);
|
||||
return (NULL);
|
||||
}
|
||||
if (temp == 0)
|
||||
return (str);
|
||||
return (str);
|
||||
}
|
||||
|
||||
static char *extract_all_nl(char buf[BUFFER_SIZE], int fd, char *str, int nl_i)
|
||||
{
|
||||
int temp;
|
||||
|
||||
if (!str)
|
||||
return (NULL);
|
||||
while (nl_i < 0)
|
||||
{
|
||||
str = ft_read_one(buf, fd, str);
|
||||
if (!str)
|
||||
return (NULL);
|
||||
nl_i = index_of_nl(buf, BUFFER_SIZE);
|
||||
if (nl_i < 0 && !ft_strlen_1(buf))
|
||||
return (str);
|
||||
}
|
||||
str = ft_strjoin_new(str, buf, nl_i);
|
||||
if (!str)
|
||||
return (NULL);
|
||||
temp = del_before_nl(buf);
|
||||
if (temp < 0)
|
||||
{
|
||||
free(str);
|
||||
return (NULL);
|
||||
}
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *get_next_line(int fd)
|
||||
{
|
||||
static char buf[BUFFER_SIZE + 1];
|
||||
int nl_i;
|
||||
int temp;
|
||||
char *str;
|
||||
|
||||
if (fd < 0 || BUFFER_SIZE <= 0)
|
||||
return (NULL);
|
||||
nl_i = index_of_nl(buf, BUFFER_SIZE);
|
||||
if (nl_i >= 0)
|
||||
{
|
||||
str = ft_substr_gnl(buf, 0, nl_i + 1);
|
||||
temp = del_before_nl(buf);
|
||||
if (temp < 0)
|
||||
return (free(str), NULL);
|
||||
}
|
||||
else
|
||||
str = extract_all_nl(buf, fd, ft_substr_gnl("", 0, 1), nl_i);
|
||||
return (str);
|
||||
}
|
||||
102
bonus/GNL/get_next_line_utils.c
Normal file
102
bonus/GNL/get_next_line_utils.c
Normal file
@@ -0,0 +1,102 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/21 17:21:38 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/11/27 18:06:51 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line.h"
|
||||
#include <stdint.h>
|
||||
|
||||
int index_of_nl(char *str, int limit)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!str)
|
||||
return (-1);
|
||||
i = 0;
|
||||
while (i < limit && str[i])
|
||||
{
|
||||
if (str[i] == '\n')
|
||||
return (i);
|
||||
i++;
|
||||
}
|
||||
return (-1);
|
||||
}
|
||||
|
||||
size_t ft_strlen_1(const char *s)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (s[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
char *ft_strjoin_new(char const *s1, char const *s2, size_t limit)
|
||||
{
|
||||
char *str;
|
||||
size_t i;
|
||||
size_t j;
|
||||
|
||||
str = malloc(sizeof(char) * (ft_strlen_1(s1) + ft_strlen_1(s2) + 1));
|
||||
if (!str)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (s1[i])
|
||||
{
|
||||
str[i] = s1[i];
|
||||
i++;
|
||||
}
|
||||
while (j <= limit && s2[j])
|
||||
{
|
||||
str[i + j] = s2[j];
|
||||
j++;
|
||||
}
|
||||
str[i + j] = '\0';
|
||||
free((void *) s1);
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_substr_gnl(char const *s, unsigned int start, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
char *sub_str;
|
||||
size_t s_len;
|
||||
|
||||
if (!s)
|
||||
return (NULL);
|
||||
s_len = ft_strlen_1(s);
|
||||
if (start > s_len)
|
||||
len = 0;
|
||||
else if (s_len < (start + len))
|
||||
len = s_len - start;
|
||||
sub_str = malloc(sizeof(char) * (len + 1));
|
||||
if (!sub_str)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
sub_str[i] = s[start + i];
|
||||
i++;
|
||||
}
|
||||
sub_str[i] = '\0';
|
||||
return (sub_str);
|
||||
}
|
||||
|
||||
void ft_bzero(void *s, size_t n)
|
||||
{
|
||||
while (n > 0)
|
||||
{
|
||||
*((unsigned char *) s) = '\0';
|
||||
s++;
|
||||
n--;
|
||||
}
|
||||
}
|
||||
110
bonus/checker_bonus.c
Normal file
110
bonus/checker_bonus.c
Normal file
@@ -0,0 +1,110 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* checker_bonus.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/09 11:46:36 by dgaillet #+# #+# */
|
||||
/* Updated: 2026/01/09 14:51:32 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include "parsing.h"
|
||||
#include "get_next_line.h"
|
||||
#include "check_error.h"
|
||||
#include <unistd.h>
|
||||
|
||||
static int apply_operation(t_stacks *stacks, char buf[1024])
|
||||
{
|
||||
if (ft_strncmp("sa\n", buf, ft_strlen(buf)))
|
||||
return (sa(stacks), 1);
|
||||
if (ft_strncmp("sb\n", buf, ft_strlen(buf)))
|
||||
return (sb(stacks), 1);
|
||||
if (ft_strncmp("ss\n", buf, ft_strlen(buf)))
|
||||
return (ss(stacks), 1);
|
||||
if (ft_strncmp("pa\n", buf, ft_strlen(buf)))
|
||||
return (pa(stacks), 1);
|
||||
if (ft_strncmp("pb\n", buf, ft_strlen(buf)))
|
||||
return (pb(stacks), 1);
|
||||
if (ft_strncmp("ra\n", buf, ft_strlen(buf)))
|
||||
return (ra(stacks), 1);
|
||||
if (ft_strncmp("rb\n", buf, ft_strlen(buf)))
|
||||
return (rb(stacks), 1);
|
||||
if (ft_strncmp("rr\n", buf, ft_strlen(buf)))
|
||||
return (rr(stacks), 1);
|
||||
if (ft_strncmp("rra\n", buf, ft_strlen(buf)))
|
||||
return (rra(stacks), 1);
|
||||
if (ft_strncmp("rrb\n", buf, ft_strlen(buf)))
|
||||
return (rrb(stacks), 1);
|
||||
if (ft_strncmp("rrr\n", buf, ft_strlen(buf)))
|
||||
return (rrr(stacks), 1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int is_stacks_b_empty(t_stacks *stacks)
|
||||
{
|
||||
if (stacks->b != NULL)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int tester(t_stacks *stacks)
|
||||
{
|
||||
char *buf;
|
||||
|
||||
buf = get_next_line(0);
|
||||
while (buf)
|
||||
{
|
||||
if (!apply_operation(stacks, buf))
|
||||
break ;
|
||||
free(buf);
|
||||
buf = get_next_line(0);
|
||||
}
|
||||
if (!is_stacks_b_empty(stacks))
|
||||
{
|
||||
secure_write(1, "KO\n", 3);
|
||||
return (0);
|
||||
}
|
||||
if (!check_order(stacks->a))
|
||||
{
|
||||
secure_write(1, "KO\n", 3);
|
||||
return (0);
|
||||
}
|
||||
secure_write(1, "OK\n", 3);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int bonus(char **tab)
|
||||
{
|
||||
t_stacks *stacks;
|
||||
int len;
|
||||
|
||||
stacks = NULL;
|
||||
len = len_split(tab);
|
||||
stacks = init_stacks(len, tab, 0);
|
||||
stacks->print = 0;
|
||||
if (!stacks)
|
||||
return (0);
|
||||
tester(stacks);
|
||||
free_all(stacks);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char **tab;
|
||||
|
||||
if (argc < 2)
|
||||
return (write(2, "Error\n", 7));
|
||||
tab = split_all(join_all(argc, argv));
|
||||
if (!tab)
|
||||
return (0);
|
||||
if (!check_error_bonus(tab))
|
||||
secure_write(2, "Error\n", 7);
|
||||
else
|
||||
bonus(tab);
|
||||
free_tab(tab);
|
||||
return (0);
|
||||
}
|
||||
@@ -23,6 +23,17 @@ int verif_is_number(char **tab)
|
||||
return (1);
|
||||
}
|
||||
|
||||
int check_error_bonus(char **tab)
|
||||
{
|
||||
if (!verif_is_digit(tab, 0))
|
||||
return (0);
|
||||
if (!verif_overflow(tab, 0))
|
||||
return (0);
|
||||
if (!verif_double(tab, 0))
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int check_error(char **tab, int mod)
|
||||
{
|
||||
if (!verif_flag(tab, mod))
|
||||
@@ -26,6 +26,16 @@ static int is_exist_flag(char **tab, int pos)
|
||||
return (verif);
|
||||
}
|
||||
|
||||
static int is_algo_flag(char *str)
|
||||
{
|
||||
if (ft_strncmp(str, "--simple", 30)
|
||||
|| ft_strncmp(str, "--medium", 30)
|
||||
|| ft_strncmp(str, "--adaptive", 30)
|
||||
|| ft_strncmp(str, "--complex", 30))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int verif_exist_flag(char **tab, int mod)
|
||||
{
|
||||
int verif;
|
||||
@@ -37,6 +47,8 @@ static int verif_exist_flag(char **tab, int mod)
|
||||
{
|
||||
if (is_exist_flag(tab, 1) && is_exist_flag(tab, 2))
|
||||
verif = 1;
|
||||
if (is_algo_flag(tab[1]) && is_algo_flag(tab[2]))
|
||||
verif = 0;
|
||||
}
|
||||
return (verif);
|
||||
}
|
||||
@@ -19,7 +19,6 @@ void simple(t_stacks *stacks)
|
||||
int len;
|
||||
|
||||
len = stack_a_len(stacks);
|
||||
stacks->algo = 1;
|
||||
insertion(stacks, len);
|
||||
}
|
||||
|
||||
@@ -29,7 +28,6 @@ void medium(t_stacks *stacks)
|
||||
int range;
|
||||
int len;
|
||||
|
||||
stacks->algo = 2;
|
||||
len = stack_a_len(stacks);
|
||||
if (len == 2)
|
||||
sort_two(stacks);
|
||||
@@ -45,7 +43,6 @@ void medium(t_stacks *stacks)
|
||||
|
||||
void complex(t_stacks *stacks)
|
||||
{
|
||||
stacks->algo = 3;
|
||||
radix(stacks);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,8 @@ static void print_disorder(t_stacks *stacks)
|
||||
int nbr;
|
||||
char *str;
|
||||
|
||||
nbr = (int) stacks->disorder * 10000;
|
||||
stacks->disorder *= 10000;
|
||||
nbr = (int) stacks->disorder;
|
||||
str = ft_itoa(nbr);
|
||||
if (!str)
|
||||
exit (EXIT_FAILURE);
|
||||
@@ -31,8 +32,9 @@ static void print_disorder(t_stacks *stacks)
|
||||
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, &str[ft_strlen(str) - 2], ft_strlen(str) - 2);
|
||||
secure_write(2, "%\n", 2);
|
||||
stacks->disorder /= 10000;
|
||||
free(str);
|
||||
}
|
||||
|
||||
@@ -41,20 +43,20 @@ static void print_algo(t_stacks *stacks)
|
||||
secure_write(2, "[bench] algo: ", 14);
|
||||
if (stacks->algo == 0)
|
||||
{
|
||||
secure_write(2, "Adaptative", 10);
|
||||
secure_write(2, "Adaptive", 8);
|
||||
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
|
||||
secure_write(2, " / O(n√n)\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);
|
||||
secure_write(2, "Medium / O(n√n)\n", 18);
|
||||
else if (stacks->algo == 3)
|
||||
secure_write(2, "Complex / O(n√n)\n", 17);
|
||||
secure_write(2, "Complex / O(nlogn)\n", 19);
|
||||
}
|
||||
|
||||
static void print_total_ops(t_stacks *stacks)
|
||||
|
||||
12
flags/flag.c
12
flags/flag.c
@@ -19,7 +19,8 @@ int calcul_mod(int argc, char **argv)
|
||||
int mod;
|
||||
|
||||
mod = 0;
|
||||
while (argv[mod] && !ft_isdigit(argv[mod][0]) && mod < argc)
|
||||
while (argv[mod] && !ft_isdigit(argv[mod][0])
|
||||
&& !ft_isdigit(argv[mod][1]) && argv[mod][1] && mod < argc)
|
||||
mod++;
|
||||
mod--;
|
||||
return (mod);
|
||||
@@ -62,11 +63,20 @@ int pos_bench(char **argv, int mod)
|
||||
void flags(int pos, int pos_b, char **argv, t_stacks *stacks)
|
||||
{
|
||||
if (ft_strncmp(argv[pos], "--simple", 30) && pos > 0)
|
||||
{
|
||||
stacks->algo = 1;
|
||||
simple(stacks);
|
||||
}
|
||||
else if (ft_strncmp(argv[pos], "--medium", 30) && pos > 0)
|
||||
{
|
||||
stacks->algo = 2;
|
||||
medium(stacks);
|
||||
}
|
||||
else if (ft_strncmp(argv[pos], "--complex", 30) && pos > 0)
|
||||
{
|
||||
stacks->algo = 3;
|
||||
complex(stacks);
|
||||
}
|
||||
else if (ft_strncmp(argv[pos], "--adaptive", 30) && pos > 0)
|
||||
adaptive(stacks, argv);
|
||||
else
|
||||
|
||||
@@ -18,7 +18,7 @@ static void rec_ft_putnbr_fd(int nbr, int fd)
|
||||
|
||||
if (!nbr)
|
||||
return ;
|
||||
ft_putnbr_fd(nbr / 10, fd);
|
||||
rec_ft_putnbr_fd(nbr / 10, fd);
|
||||
c = (nbr % 10) + '0';
|
||||
secure_write(fd, &c, 1);
|
||||
}
|
||||
|
||||
@@ -19,5 +19,6 @@ 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);
|
||||
int check_error_bonus(char **tab);
|
||||
|
||||
#endif
|
||||
|
||||
30
includes/get_next_line.h
Normal file
30
includes/get_next_line.h
Normal file
@@ -0,0 +1,30 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/21 17:22:03 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/11/27 18:05:13 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef GET_NEXT_LINE_H
|
||||
# define GET_NEXT_LINE_H
|
||||
|
||||
# ifndef BUFFER_SIZE
|
||||
# define BUFFER_SIZE 42
|
||||
# endif
|
||||
|
||||
# include <stdlib.h>
|
||||
|
||||
char *get_next_line(int fd);
|
||||
|
||||
size_t ft_strlen_1(const char *s);
|
||||
char *ft_strjoin_new(char const *s1, char const *s2, size_t limit);
|
||||
void ft_bzero(void *s, size_t n);
|
||||
int index_of_nl(char *str, int limit);
|
||||
char *ft_substr_gnl(char const *s, unsigned int start, size_t len);
|
||||
|
||||
#endif
|
||||
@@ -13,6 +13,8 @@
|
||||
#ifndef MEDIUM_HEADERS_H
|
||||
# define MEDIUM_HEADERS_H
|
||||
|
||||
# include "push_swap.h"
|
||||
|
||||
typedef struct s_tab
|
||||
{
|
||||
int max_range;
|
||||
@@ -25,9 +27,10 @@ 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);
|
||||
void normal_move_path(t_stacks *stacks, t_tab *one_preset, int range);
|
||||
t_stack *assign_stack(t_stacks *stacks, char c);
|
||||
int get_value_finded(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);
|
||||
@@ -37,5 +40,13 @@ 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);
|
||||
/* MOVE PATH FILES*/
|
||||
int get_pre_move_b(t_stacks *stacks, t_tab *one_preset, int range);
|
||||
int move_left_to_right(int value, t_stacks *stacks, int is_move);
|
||||
int move_right_to_left(int value, t_stacks *stacks, int is_move);
|
||||
int wich_path_a(int value, t_stacks *stacks);
|
||||
void move_ra_rra(int value, t_stacks *stacks);
|
||||
void path_rr_rrr(int value, t_stacks *stacks, t_tab *one_preset, int range);
|
||||
void put_in_order_ra_rra(t_stacks *stacks);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -34,5 +34,6 @@ 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
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
typedef struct s_stack
|
||||
{
|
||||
int value;
|
||||
long long index;
|
||||
struct s_stack *next;
|
||||
struct s_stack *previous;
|
||||
} t_stack;
|
||||
@@ -26,6 +27,7 @@ typedef struct s_stacks
|
||||
t_stack *b;
|
||||
int algo;
|
||||
int bench;
|
||||
int print;
|
||||
float disorder;
|
||||
unsigned int ra;
|
||||
unsigned int rb;
|
||||
|
||||
28
main.c
28
main.c
@@ -16,6 +16,30 @@
|
||||
#include "flags.h"
|
||||
#include <unistd.h>
|
||||
|
||||
static int push_swap(char **tab, int len, int mod)
|
||||
{
|
||||
t_stacks *stacks;
|
||||
|
||||
stacks = NULL;
|
||||
if (mod == -1)
|
||||
return (0);
|
||||
stacks = init_stacks(len, tab, mod);
|
||||
if (!stacks)
|
||||
return (0);
|
||||
indexer(stacks);
|
||||
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);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char **tab;
|
||||
@@ -30,9 +54,9 @@ int main(int argc, char **argv)
|
||||
len = len_split(tab);
|
||||
mod = calcul_mod(len, tab);
|
||||
if (check_error(tab, mod))
|
||||
test1(tab, len, mod);
|
||||
push_swap(tab, len, mod);
|
||||
else
|
||||
write(2, "Error\n", 7);
|
||||
write(2, "Error\n", 6);
|
||||
free_tab(tab);
|
||||
return (0);
|
||||
}
|
||||
|
||||
54
parsing/indexer.c
Normal file
54
parsing/indexer.c
Normal 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++;
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,7 @@ static void set_t_stacks(t_stacks *stacks)
|
||||
stacks->algo = 0;
|
||||
stacks->bench = 0;
|
||||
stacks->disorder = 0;
|
||||
stacks->print = 1;
|
||||
stacks->sa = 0;
|
||||
stacks->sb = 0;
|
||||
stacks->ss = 0;
|
||||
|
||||
455
playtest.sh
Executable file
455
playtest.sh
Executable file
@@ -0,0 +1,455 @@
|
||||
#!/bin/bash
|
||||
|
||||
while true
|
||||
do
|
||||
echo "=========================="
|
||||
echo " 0 - ----- ONLY NEGATIF"
|
||||
echo " 1 - ----- ONLY POSITIF"
|
||||
echo " 2 - ------------ MIXTE"
|
||||
echo " 3 - -------- SANS FLAG"
|
||||
echo " 4 - ------ SIMUL ERROR"
|
||||
echo " 5 - ------- TEST BENCH"
|
||||
echo " 6 - TEST BENCH SORTIE"
|
||||
echo " 7 - ------- TEST LEN 3"
|
||||
echo " 8 - ------- TEST LEN 5"
|
||||
echo " 9 - ----- TEST LEN 500"
|
||||
echo "10 - ------ CLOSE TEST"
|
||||
echo "ENTRER LE TEST QUE TU VEUX FAIRE : "
|
||||
read nombre
|
||||
|
||||
case "$nombre" in
|
||||
0)
|
||||
echo "ONLY NEGATIF"
|
||||
echo "#################################################"
|
||||
echo "#################################################"
|
||||
echo "========================"
|
||||
echo "======TEST SIMPLE N====="
|
||||
echo "========================"
|
||||
ARG="2147483647 -2147483648 8 9 -35 40 -20 7 5 30 10"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
ARG="-147 -71 -59 -194 -86 -14 -49 -32 -137 -113"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
ARG="-127 -57 -151 -163 -159 -66 -128 -147 -68 -83"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
ARG="-64 -109 -102 -176 -15 -19 -103 -38 -192 -37"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST MEDIUM N====="
|
||||
echo "========================"
|
||||
ARG="2147483647 -2147483648 8 9 -35 40 -20 7 5 30 10"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
ARG="-147 -71 -59 -194 -86 -14 -49 -32 -137 -113"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
ARG="-127 -57 -151 -163 -159 -66 -128 -147 -68 -83"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
ARG="-64 -109 -102 -176 -15 -19 -103 -38 -192 -37"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST COMPLEX N===="
|
||||
echo "========================"
|
||||
ARG="2147483647 -2147483648 8 9 -35 40 -20 7 5 30 10"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
ARG="-147 -71 -59 -194 -86 -14 -49 -32 -137 -113"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
ARG="-127 -57 -151 -163 -159 -66 -128 -147 -68 -83"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
ARG="-64 -109 -102 -176 -15 -19 -103 -38 -192 -37"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
;;
|
||||
1)
|
||||
echo "#################################################"
|
||||
echo "#################################################"
|
||||
echo "ONLY POSITIF"
|
||||
echo "#################################################"
|
||||
echo "#################################################"
|
||||
echo "========================"
|
||||
echo "======TEST SIMPLE P====="
|
||||
echo "========================"
|
||||
ARG="147 71 59 194 86 14 49 32 137 113"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
ARG="127 57 151 163 159 66 128 147 68 83"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
ARG="64 109 102 176 15 19 103 38 192 37"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST MEDIUM P====="
|
||||
echo "========================"
|
||||
ARG="147 71 59 194 86 14 49 32 137 113"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
ARG="127 57 151 163 159 66 128 147 68 83"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
ARG="64 109 102 176 15 19 103 38 192 37"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST COMPLEX P===="
|
||||
echo "========================"
|
||||
ARG="147 71 59 194 86 14 49 32 137 113"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
ARG="127 57 151 163 159 66 128 147 68 83"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
ARG="64 109 102 176 15 19 103 38 192 37"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
;;
|
||||
2)
|
||||
echo "#################################################"
|
||||
echo "#################################################"
|
||||
echo "MIXTE"
|
||||
echo "#################################################"
|
||||
echo "#################################################"
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST SIMPLE P====="
|
||||
echo "========================"
|
||||
ARG="0 147 71 -59 194 -86 -14 49 32 137 113"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
ARG="127 57 -151 163 -159 0 66 -128 147 68 83"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
ARG="64 109 102 176 15 19 -103 38 192 37 0"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST MEDIUM P====="
|
||||
echo "========================"
|
||||
ARG="0 147 71 -59 194 86 14 49 32 137 113"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
ARG="127 57 151 -163 0 159 66 128 147 68 83"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
ARG="64 109 -102 176 15 19 -103 38 192 37 0"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST COMPLEX P===="
|
||||
echo "========================"
|
||||
ARG="0 147 71 59 -194 86 14 49 32 137 113"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
ARG="127 57 151 0 163 159 -66 128 -147 68 83"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
ARG="64 -109 102 -176 15 19 -103 38 192 37 0"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
;;
|
||||
3)
|
||||
echo "#################################################"
|
||||
echo "#################################################"
|
||||
echo "SANS FLAG"
|
||||
echo "#################################################"
|
||||
echo "#################################################"
|
||||
|
||||
ARG="0 147 71 -59 194 -86 -14 49 32 137 113"; ./push_swap $ARG | ./checker $ARG
|
||||
ARG="127 57 -151 163 -159 0 66 -128 147 68 83"; ./push_swap $ARG | ./checker $ARG
|
||||
ARG="64 109 102 176 15 19 -103 38 192 37 0"; ./push_swap $ARG | ./checker $ARG
|
||||
;;
|
||||
4)
|
||||
echo "#################################################"
|
||||
echo "#################################################"
|
||||
echo "SIMUL ERROR ON PUSH_SWAP"
|
||||
echo "#################################################"
|
||||
echo "#################################################"
|
||||
echo "DOUBLE FLAG"
|
||||
|
||||
ARG="0 147 71 59 -194 86 14 49 32 137 113"; ./push_swap --complex --complex $ARG
|
||||
ARG="0 147 71 59 -194 86 14 49 32 137 113"; ./push_swap --simple --simple $ARG
|
||||
ARG="0 147 71 59 -194 86 14 49 32 137 113"; ./push_swap --medium --mediun $ARG
|
||||
ARG="0 147 71 59 -194 86 14 49 32 137 113"; ./push_swap --adaptive --adaptive $ARG
|
||||
ARG="0 147 71 59 -194 86 14 49 32 137 113"; ./push_swap --bench --bench $ARG
|
||||
echo "#################################################"
|
||||
echo "SAME TYPE FLAG"
|
||||
|
||||
ARG="0 147 71 59 -194 86 14 49 32 137 113"; ./push_swap --simple --complex $ARG
|
||||
echo "#################################################"
|
||||
echo "TOO MUCH FLAG"
|
||||
|
||||
ARG="0 147 71 59 -194 86 14 49 32 137 113"; ./push_swap --simple --bench --adaptive $ARG
|
||||
echo "#################################################"
|
||||
echo "WRONG FLAG"
|
||||
|
||||
ARG="0 147 71 59 -194 86 14 49 32 137 113"; ./push_swap --simple --test --error $ARG
|
||||
echo "#################################################"
|
||||
echo "DOUBLE NUMBER"
|
||||
|
||||
ARG="0 147 71 59 -194 86 14 49 32 137 0 113"; ./push_swap --simple --test --error $ARG
|
||||
echo "#################################################"
|
||||
echo "TEST WRONG INPUT ON THE NUMBERS"
|
||||
./push_swap --simple 5 3 4 2 d
|
||||
./push_swap --simple 5 3 c 4 2
|
||||
./push_swap --simple v 5 3 4 2
|
||||
./push_swap --simple 5 3 4 2 455313212345
|
||||
./push_swap --simple 5 3 4 2 "++++432"
|
||||
./push_swap --simple 5 3 4 2 +++--43
|
||||
./push_swap --simple 5 3 4 2 +
|
||||
;;
|
||||
5)
|
||||
echo "#################################################"
|
||||
echo "TEST BENCH"
|
||||
echo "#################################################"
|
||||
echo "========================"
|
||||
echo "======TEST SIMPLE ======"
|
||||
echo "========================"
|
||||
ARG="147 71 59 194 86 14 49 32 137 113"; ./push_swap --simple --bench $ARG 1> test.txt
|
||||
echo "#################################################"
|
||||
ARG="127 57 151 163 159 66 128 147 68 83"; ./push_swap --simple --bench $ARG 1> test.txt
|
||||
echo "#################################################"
|
||||
ARG="64 109 102 176 15 19 103 38 192 37"; ./push_swap --simple --bench $ARG 1> test.txt
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST MEDIUM ======"
|
||||
echo "========================"
|
||||
ARG="147 71 59 194 86 14 49 32 137 113"; ./push_swap --medium --bench $ARG 1> test.txt
|
||||
echo "#################################################"
|
||||
ARG="127 57 151 163 159 66 128 147 68 83"; ./push_swap --medium --bench $ARG 1> test.txt
|
||||
echo "#################################################"
|
||||
ARG="64 109 102 176 15 19 103 38 192 37"; ./push_swap --medium --bench $ARG 1> test.txt
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST COMPLEX ====="
|
||||
echo "========================"
|
||||
ARG="147 71 59 194 86 14 49 32 137 113"; ./push_swap --complex --bench $ARG 1> test.txt
|
||||
echo "#################################################"
|
||||
ARG="127 57 151 163 159 66 128 147 68 83"; ./push_swap --complex --bench $ARG 1> test.txt
|
||||
echo "#################################################"
|
||||
ARG="64 109 102 176 15 19 103 38 192 37"; ./push_swap --complex --bench $ARG 1> test.txt
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST ADAPTIV ====="
|
||||
echo "========================"
|
||||
echo "#################################################"
|
||||
ARG="147 71 59 194 86 14 49 32 137 113"; ./push_swap --bench $ARG 1> test.txt
|
||||
echo "#################################################"
|
||||
ARG="127 57 151 163 159 66 128 147 68 83"; ./push_swap --bench $ARG 1> test.txt
|
||||
echo "#################################################"
|
||||
ARG="64 109 102 176 15 19 103 38 192 37"; ./push_swap --bench $ARG 1> test.txt
|
||||
echo "#################################################"
|
||||
rm test.txt
|
||||
;;
|
||||
6)
|
||||
echo "#################################################"
|
||||
echo "TEST BENCH SI SORTIE 2"
|
||||
echo "#################################################"
|
||||
ARG="147 71 59 194 86 14 49 32 137 113"; ./push_swap --bench $ARG > test.txt 2> bench.txt
|
||||
echo "CAT BENCH bench.txt"
|
||||
cat bench.txt
|
||||
ARG="127 57 151 163 159 66 128 147 68 83"; ./push_swap --bench $ARG > test.txt 2> bench.txt
|
||||
echo "CAT BENCH bench.txt"
|
||||
cat bench.txt
|
||||
ARG="64 109 102 176 15 19 103 38 192 37"; ./push_swap --bench $ARG > test.txt 2> bench.txt
|
||||
echo "CAT BENCH bench.txt"
|
||||
cat bench.txt
|
||||
rm bench.txt
|
||||
rm test.txt
|
||||
;;
|
||||
7)
|
||||
echo "TEST LEN 3"
|
||||
echo "#################################################"
|
||||
echo "#################################################"
|
||||
echo "========================"
|
||||
echo "======TEST SIMPLE ======"
|
||||
echo "========================"
|
||||
ARG="2 0 1"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="2 0 1"; ./push_swap --simple $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="0 4 2"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="0 4 2"; ./push_swap --simple $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="27 5 -2"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="27 5 -2"; ./push_swap --simple $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="17 20 0"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="17 20 0"; ./push_swap --simple $ARG | wc -l
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST MEDIUM ======"
|
||||
echo "========================"
|
||||
ARG="2 0 1"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="2 0 1"; ./push_swap --medium $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="0 4 2"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="0 4 2"; ./push_swap --medium $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="27 5 -2"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="27 5 -2"; ./push_swap --medium $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="17 20 0"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="17 20 0"; ./push_swap --medium $ARG | wc -l
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST COMPLEX ====="
|
||||
echo "========================"
|
||||
ARG="2 0 1"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="2 0 1"; ./push_swap --complex $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="0 4 2"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="0 4 2"; ./push_swap --complex $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="27 5 -2"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="27 5 -2"; ./push_swap --complex $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="17 20 0"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="17 20 0"; ./push_swap --complex $ARG | wc -l
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST ADAPTIV ====="
|
||||
echo "========================"
|
||||
ARG="2 0 1"; ./push_swap $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="2 0 1"; ./push_swap $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="0 4 2"; ./push_swap $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="0 4 2"; ./push_swap $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="27 5 -2"; ./push_swap $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="27 5 -2"; ./push_swap $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="17 20 0"; ./push_swap $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="17 20 0"; ./push_swap $ARG | wc -l
|
||||
;;
|
||||
8)
|
||||
echo "TEST LEN 5"
|
||||
echo "#################################################"
|
||||
echo "#################################################"
|
||||
echo "========================"
|
||||
echo "======TEST SIMPLE ======"
|
||||
echo "========================"
|
||||
ARG="2 0 1 5 20"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="2 0 1 5 20"; ./push_swap --simple $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="0 4 2 -100 7"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="0 4 2 -100 7"; ./push_swap --simple $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="27 5 -2 10 3"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="27 5 -2 10 3"; ./push_swap --simple $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="17 20 0 -400 -245"; ./push_swap --simple $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="17 20 0 -400 -245"; ./push_swap --simple $ARG | wc -l
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST MEDIUM ======"
|
||||
echo "========================"
|
||||
ARG="2 0 1 5 20"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="2 0 1 5 20"; ./push_swap --medium $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="0 4 2 -100 7"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="0 4 2 -100 7"; ./push_swap --medium $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="27 5 -2 10 3"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="27 5 -2 10 3"; ./push_swap --medium $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="17 20 0 -400 -245"; ./push_swap --medium $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="17 20 0 -400 -245"; ./push_swap --medium $ARG | wc -l
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST COMPLEX ====="
|
||||
echo "========================"
|
||||
ARG="2 0 1 5 20"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="2 0 1 5 20"; ./push_swap --complex $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="0 4 2 -100 7"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="0 4 2 -100 7"; ./push_swap --complex $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="27 5 -2 10 3"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="27 5 -2 10 3"; ./push_swap --complex $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="17 20 0 -400 -245"; ./push_swap --complex $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="17 20 0 -400 -245"; ./push_swap --complex $ARG | wc -l
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST ADAPTIV ====="
|
||||
echo "========================"
|
||||
ARG="2 0 1 5 20"; ./push_swap $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="2 0 1 5 20"; ./push_swap $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="0 4 2 -100 7"; ./push_swap $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="0 4 2 -100 7"; ./push_swap $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="27 5 -2 10 3"; ./push_swap $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="27 5 -2 10 3"; ./push_swap $ARG | wc -l
|
||||
echo "##################"
|
||||
ARG="17 20 0 -400 -245"; ./push_swap $ARG | ./checker $ARG
|
||||
echo -n "MOUVEMENT :"
|
||||
ARG="17 20 0 -400 -245"; ./push_swap $ARG | wc -l
|
||||
;;
|
||||
9)
|
||||
echo "TEST LEN 500"
|
||||
echo "#################################################"
|
||||
echo "#################################################"
|
||||
|
||||
echo "========================"
|
||||
echo "======TEST ADAPTIV ====="
|
||||
echo "========================"
|
||||
shuf -i 0-9999 -n 500 > args.txt ; ./push_swap $(cat args.txt) | ./checker $(cat args.txt)
|
||||
echo -n "MOUVEMENT :"
|
||||
./push_swap $(cat args.txt) | wc -l
|
||||
echo "##################"
|
||||
shuf -i 0-9999 -n 500 > args.txt ; ./push_swap $(cat args.txt) | ./checker $(cat args.txt)
|
||||
echo -n "MOUVEMENT :"
|
||||
./push_swap $(cat args.txt) | wc -l
|
||||
echo "##################"
|
||||
shuf -i 0-9999 -n 500 > args.txt ; ./push_swap $(cat args.txt) | ./checker $(cat args.txt)
|
||||
echo -n "MOUVEMENT :"
|
||||
./push_swap $(cat args.txt) | wc -l
|
||||
echo "##################"
|
||||
shuf -i 0-9999 -n 500 > args.txt ; ./push_swap $(cat args.txt) | ./checker $(cat args.txt)
|
||||
echo -n "MOUVEMENT :"
|
||||
./push_swap $(cat args.txt) | wc -l
|
||||
echo "##################"
|
||||
shuf -i 0-9999 -n 500 > args.txt ; ./push_swap $(cat args.txt) | ./checker $(cat args.txt)
|
||||
echo -n "MOUVEMENT :"
|
||||
./push_swap $(cat args.txt) | wc -l
|
||||
echo "##################"
|
||||
shuf -i 0-9999 -n 500 > args.txt ; ./push_swap $(cat args.txt) | ./checker $(cat args.txt)
|
||||
echo -n "MOUVEMENT :"
|
||||
./push_swap $(cat args.txt) | wc -l
|
||||
echo "##################"
|
||||
shuf -i 0-9999 -n 500 > args.txt ; ./push_swap $(cat args.txt) | ./checker $(cat args.txt)
|
||||
echo -n "MOUVEMENT :"
|
||||
./push_swap $(cat args.txt) | wc -l
|
||||
echo "##################"
|
||||
shuf -i 0-9999 -n 500 > args.txt ; ./push_swap $(cat args.txt) | ./checker $(cat args.txt)
|
||||
echo -n "MOUVEMENT :"
|
||||
./push_swap $(cat args.txt) | wc -l
|
||||
echo "##################"
|
||||
shuf -i 0-9999 -n 500 > args.txt ; ./push_swap $(cat args.txt) | ./checker $(cat args.txt)
|
||||
echo -n "MOUVEMENT :"
|
||||
./push_swap $(cat args.txt) | wc -l
|
||||
echo "##################"
|
||||
shuf -i 0-9999 -n 500 > args.txt ; ./push_swap $(cat args.txt) | ./checker $(cat args.txt)
|
||||
echo -n "MOUVEMENT :"
|
||||
./push_swap $(cat args.txt) | wc -l
|
||||
echo "##################"
|
||||
shuf -i 0-9999 -n 500 > args.txt ; ./push_swap $(cat args.txt) | ./checker $(cat args.txt)
|
||||
echo -n "MOUVEMENT :"
|
||||
./push_swap $(cat args.txt) | wc -l
|
||||
echo "##################"
|
||||
shuf -i 0-9999 -n 500 > args.txt ; ./push_swap $(cat args.txt) | ./checker $(cat args.txt)
|
||||
echo -n "MOUVEMENT :"
|
||||
./push_swap $(cat args.txt) | wc -l
|
||||
echo "##################"
|
||||
shuf -i 0-9999 -n 500 > args.txt ; ./push_swap $(cat args.txt) | ./checker $(cat args.txt)
|
||||
echo -n "MOUVEMENT :"
|
||||
./push_swap $(cat args.txt) | wc -l
|
||||
echo "##################"
|
||||
shuf -i 0-9999 -n 500 > args.txt ; ./push_swap $(cat args.txt) | ./checker $(cat args.txt)
|
||||
echo -n "MOUVEMENT :"
|
||||
./push_swap $(cat args.txt) | wc -l
|
||||
echo "##################"
|
||||
shuf -i 0-9999 -n 500 > args.txt ; ./push_swap $(cat args.txt) | ./checker $(cat args.txt)
|
||||
echo -n "MOUVEMENT :"
|
||||
./push_swap $(cat args.txt) | wc -l
|
||||
echo "##################"
|
||||
shuf -i 0-9999 -n 500 > args.txt ; ./push_swap $(cat args.txt) | ./checker $(cat args.txt)
|
||||
echo -n "MOUVEMENT :"
|
||||
./push_swap $(cat args.txt) | wc -l
|
||||
rm args.txt
|
||||
;;
|
||||
10)
|
||||
echo "Au revoir"
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
echo "==============================="
|
||||
echo "TAPE NIMPORTE POUR GO NEXT : "
|
||||
read tape
|
||||
clear
|
||||
done
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/08 14:30:38 by dgaillet #+# #+# */
|
||||
/* Updated: 2026/01/09 12:05:56 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2026/01/09 14:55:16 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -17,6 +17,6 @@ void secure_write(int fd, char *str, int len)
|
||||
{
|
||||
if (len < 0)
|
||||
return ;
|
||||
if (write(fd, str, len) < 0)
|
||||
if (!str || write(fd, str, len) < 0)
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -31,7 +31,8 @@ void pa(t_stacks *stacks)
|
||||
}
|
||||
stack_add_front(&(stacks->a), b_push);
|
||||
stacks->pa++;
|
||||
write(1, "pa\n", 3);
|
||||
if (stacks->print)
|
||||
secure_write(1, "pa\n", 3);
|
||||
}
|
||||
|
||||
void pb(t_stacks *stacks)
|
||||
@@ -51,5 +52,6 @@ void pb(t_stacks *stacks)
|
||||
}
|
||||
stack_add_front(&(stacks->b), a_push);
|
||||
stacks->pb++;
|
||||
write(1, "pb\n", 3);
|
||||
if (stacks->print)
|
||||
secure_write(1, "pb\n", 3);
|
||||
}
|
||||
|
||||
@@ -18,7 +18,8 @@ void rra(t_stacks *stacks)
|
||||
if (stacks && stacks->a && stacks->a->previous)
|
||||
stacks->a = stacks->a->previous;
|
||||
stacks->rra++;
|
||||
write(1, "rra\n", 4);
|
||||
if (stacks->print)
|
||||
secure_write(1, "rra\n", 4);
|
||||
}
|
||||
|
||||
void rrb(t_stacks *stacks)
|
||||
@@ -26,7 +27,8 @@ void rrb(t_stacks *stacks)
|
||||
if (stacks && stacks->b && stacks->b->previous)
|
||||
stacks->b = stacks->b->previous;
|
||||
stacks->rrb++;
|
||||
write(1, "rrb\n", 4);
|
||||
if (stacks->print)
|
||||
secure_write(1, "rrb\n", 4);
|
||||
}
|
||||
|
||||
void rrr(t_stacks *stacks)
|
||||
@@ -36,5 +38,6 @@ void rrr(t_stacks *stacks)
|
||||
if (stacks && stacks->a && stacks->a->previous)
|
||||
stacks->a = stacks->a->previous;
|
||||
stacks->rrr++;
|
||||
write(1, "rrr\n", 4);
|
||||
if (stacks->print)
|
||||
secure_write(1, "rrr\n", 4);
|
||||
}
|
||||
|
||||
@@ -18,7 +18,8 @@ void ra(t_stacks *stacks)
|
||||
if (stacks && stacks->a && stacks->a->next)
|
||||
stacks->a = stacks->a->next;
|
||||
stacks->ra++;
|
||||
write(1, "ra\n", 3);
|
||||
if (stacks->print)
|
||||
secure_write(1, "ra\n", 3);
|
||||
}
|
||||
|
||||
void rb(t_stacks *stacks)
|
||||
@@ -26,7 +27,8 @@ void rb(t_stacks *stacks)
|
||||
if (stacks && stacks->b && stacks->b->next)
|
||||
stacks->b = stacks->b->next;
|
||||
stacks->rb++;
|
||||
write(1, "rb\n", 3);
|
||||
if (stacks->print)
|
||||
secure_write(1, "rb\n", 3);
|
||||
}
|
||||
|
||||
void rr(t_stacks *stacks)
|
||||
@@ -36,5 +38,6 @@ void rr(t_stacks *stacks)
|
||||
if (stacks && stacks->b && stacks->b->next)
|
||||
stacks->b = stacks->b->next;
|
||||
stacks->rr++;
|
||||
write(1, "rr\n", 3);
|
||||
if (stacks->print)
|
||||
secure_write(1, "rr\n", 3);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -25,7 +25,8 @@ void sa(t_stacks *stacks)
|
||||
a->value = a->next->value;
|
||||
a->next->value = stock;
|
||||
stacks->sa++;
|
||||
write(1, "sa\n", 3);
|
||||
if (stacks->print)
|
||||
secure_write(1, "sa\n", 3);
|
||||
}
|
||||
|
||||
void sb(t_stacks *stacks)
|
||||
@@ -40,7 +41,8 @@ void sb(t_stacks *stacks)
|
||||
b->value = b->next->value;
|
||||
b->next->value = stock;
|
||||
stacks->sb++;
|
||||
write(1, "sb\n", 3);
|
||||
if (stacks->print)
|
||||
secure_write(1, "sb\n", 3);
|
||||
}
|
||||
|
||||
void ss(t_stacks *stacks)
|
||||
@@ -66,5 +68,6 @@ void ss(t_stacks *stacks)
|
||||
a->next->value = stock;
|
||||
}
|
||||
stacks->ss++;
|
||||
write(1, "ss\n", 3);
|
||||
if (stacks->print)
|
||||
secure_write(1, "ss\n", 3);
|
||||
}
|
||||
|
||||
39
test_one.c
39
test_one.c
@@ -1,39 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user