tu connqis

This commit is contained in:
Geraud Chavialle 2025-09-08 22:39:24 +02:00
commit 93f14b8261
18 changed files with 644 additions and 0 deletions

31
Makefile Normal file
View File

@ -0,0 +1,31 @@
NAME = push_swap
CC = cc
CFLAGS = -Wall -Wextra -Werror
INCLUDES = -I include
SRC_DIR = src
SRCS = $(SRC_DIR)/push_swap.c \
$(SRC_DIR)/parse.c \
$(SRC_DIR)/utils.c \
$(SRC_DIR)/normalize.c \
$(SRC_DIR)/ops.c \
$(SRC_DIR)/sort_small.c \
$(SRC_DIR)/radix.c
OBJS = $(SRCS:.c=.o)
all: $(NAME)
$(NAME): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $(NAME)
%.o: %.c
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

BIN
checker_linux Executable file

Binary file not shown.

41
include/push_swap.h Normal file
View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gechavia <chaviallegeraud@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/04 17:36:00 by gechavia #+# #+# */
/* Updated: 2025/09/04 17:36:26 by gechavia ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PUSH_SWAP_H
# define PUSH_SWAP_H
# include <unistd.h>
# include <stdlib.h>
# include <limits.h>
typedef struct s_stack
{
int *array;
int size;
int capacity;
} t_stack;
void init_stacks(t_stack *a, t_stack *b);
void error_exit(t_stack *a, t_stack *b);
int parse_and_validate(char **argv, t_stack *a, t_stack *b);
void free_stacks(t_stack *a, t_stack *b);
int has_duplicates(t_stack *a);
void write_op(const char *s);
void normalize_stack(t_stack *a);
void swap(t_stack *s);
void push(t_stack *from, t_stack *to);
void rotate(t_stack *s);
void rrotate(t_stack *s);
void sort_three(t_stack *a);
void sort_five(t_stack *a, t_stack *b);
void sort_stack(t_stack *a, t_stack *b);
#endif

BIN
push_swap Executable file

Binary file not shown.

81
src/normalize.c Normal file
View File

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* normalize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gechavia <chaviallegeraud@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/04 17:32:38 by gechavia #+# #+# */
/* Updated: 2025/09/04 17:32:39 by gechavia ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void swap_vals(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
static void sort_int_array(int *arr, int n)
{
int i;
int j;
i = 0;
while (i < n)
{
j = i + 1;
while (j < n)
{
if (arr[j] < arr[i])
swap_vals(&arr[i], &arr[j]);
j = j + 1;
}
i = i + 1;
}
}
static int index_in_array(int *arr, int n, int v)
{
int i;
i = 0;
while (i < n)
{
if (arr[i] == v)
return (i);
i = i + 1;
}
return (-1);
}
void normalize_stack(t_stack *a)
{
int *tmp;
int j;
int idx;
tmp = (int *)malloc(sizeof(int) * a->size);
if (!tmp)
exit(1);
j = 0;
while (j < a->size)
{
tmp[j] = a->array[j];
j = j + 1;
}
sort_int_array(tmp, a->size);
j = 0;
while (j < a->size)
{
idx = index_in_array(tmp, a->size, a->array[j]);
a->array[j] = idx;
j = j + 1;
}
free(tmp);
}

BIN
src/normalize.o Normal file

Binary file not shown.

91
src/ops.c Normal file
View File

@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ops.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gechavia <chaviallegeraud@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/04 17:32:45 by gechavia #+# #+# */
/* Updated: 2025/09/04 17:32:46 by gechavia ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void write_op(const char *s)
{
int len;
len = 0;
while (s[len])
len = len + 1;
write(1, s, len);
}
void swap(t_stack *s)
{
int t;
if (s->size < 2)
return ;
t = s->array[0];
s->array[0] = s->array[1];
s->array[1] = t;
}
void push(t_stack *from, t_stack *to)
{
int i;
if (from->size == 0)
return ;
i = to->size;
while (i > 0)
{
to->array[i] = to->array[i - 1];
i = i - 1;
}
to->array[0] = from->array[0];
to->size = to->size + 1;
i = 0;
while (i + 1 < from->size)
{
from->array[i] = from->array[i + 1];
i = i + 1;
}
from->size = from->size - 1;
}
void rotate(t_stack *s)
{
int t;
int i;
if (s->size < 2)
return ;
t = s->array[0];
i = 0;
while (i + 1 < s->size)
{
s->array[i] = s->array[i + 1];
i = i + 1;
}
s->array[s->size - 1] = t;
}
void rrotate(t_stack *s)
{
int t;
int i;
if (s->size < 2)
return ;
t = s->array[s->size - 1];
i = s->size - 1;
while (i > 0)
{
s->array[i] = s->array[i - 1];
i = i - 1;
}
s->array[0] = t;
}

BIN
src/ops.o Normal file

Binary file not shown.

115
src/parse.c Normal file
View File

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gechavia <chaviallegeraud@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/04 17:32:51 by gechavia #+# #+# */
/* Updated: 2025/09/04 17:34:24 by gechavia ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int is_space(char c)
{
if (c == ' ')
return (1);
if (c == '\t')
return (1);
if (c == '\n')
return (1);
if (c == '\r')
return (1);
if (c == '\v')
return (1);
if (c == '\f')
return (1);
return (0);
}
static int is_digit(char c)
{
if (c >= '0' && c <= '9')
return (1);
return (0);
}
static long read_num(const char *s, int *i, int *ok)
{
long sign;
long res;
sign = 1;
res = 0;
*ok = 1;
if (s[*i] == '+' || s[*i] == '-')
{
if (s[*i] == '-')
sign = -1;
*i = *i + 1;
}
if (!is_digit(s[*i]))
*ok = 0;
while (is_digit(s[*i]))
{
res = res * 10 + (s[*i] - '0');
if ((sign == 1 && res > INT_MAX) || (sign == -1 && (-res) < INT_MIN))
*ok = 0;
*i = *i + 1;
}
return (res * sign);
}
static int push_val(t_stack *a, int v)
{
int *new_arr;
int i;
if (a->size >= a->capacity)
{
a->capacity = a->capacity * 2 + 8;
new_arr = (int *)malloc(sizeof(int) * a->capacity);
if (!new_arr)
return (0);
i = 0;
while (i < a->size)
{
new_arr[i] = a->array[i];
i = i + 1;
}
free(a->array);
a->array = new_arr;
}
a->array[a->size] = v;
a->size = a->size + 1;
return (1);
}
int parse_and_validate(char **argv, t_stack *a, t_stack *b)
{
int i;
int ok;
long v;
i = 0;
while (argv[1][i])
{
while (argv[1][i] && is_space(argv[1][i]))
i = i + 1;
if (!argv[1][i])
break ;
v = read_num(argv[1], &i, &ok);
if (!ok || v < INT_MIN || v > INT_MAX || !push_val(a, (int)v))
return (0);
}
if (a->size == 0 || has_duplicates(a))
return (0);
b->capacity = a->size;
b->array = (int *)malloc(sizeof(int) * b->capacity);
if (!b->array)
return (0);
b->size = 0;
return (1);
}

BIN
src/parse.o Normal file

Binary file not shown.

49
src/push_swap.c Normal file
View File

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gechavia <chaviallegeraud@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/04 17:32:54 by gechavia #+# #+# */
/* Updated: 2025/09/08 16:42:06 by gechavia ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void init_stacks(t_stack *a, t_stack *b)
{
a->array = NULL;
a->size = 0;
a->capacity = 0;
b->array = NULL;
b->size = 0;
b->capacity = 0;
}
void error_exit(t_stack *a, t_stack *b)
{
write(2, "Error\n", 6);
free_stacks(a, b);
exit(1);
}
int main(int argc, char **argv)
{
t_stack a;
t_stack b;
if (argc != 2)
{
write(2, "Usage: ./push_swap \"numbers\"\n", 29);
return (1);
}
init_stacks(&a, &b);
if (!parse_and_validate(argv, &a, &b))
error_exit(&a, &b);
normalize_stack(&a);
sort_stack(&a, &b);
free_stacks(&a, &b);
return (0);
}

BIN
src/push_swap.o Normal file

Binary file not shown.

90
src/radix.c Normal file
View File

@ -0,0 +1,90 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* radix.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gechavia <chaviallegeraud@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/04 17:32:56 by gechavia #+# #+# */
/* Updated: 2025/09/08 17:26:26 by gechavia ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int get_max_bits(t_stack *a)
{
int max;
int i;
int bits;
i = 0;
max = 0;
bits = 0;
while (i < a->size)
{
if (a->array[i] > max)
max = a->array[i];
i++;
}
while ((max >> bits) != 0)
bits++;
return (bits);
}
static void push_radix(t_stack *a, t_stack *b)
{
while (b->size)
{
push(b, a);
write_op("pa\n");
}
}
static void radix_sort_2(t_stack *a, t_stack *b, int bit)
{
int size;
int j;
size = a->size;
j = 0;
while (j < size)
{
if (((a->array[0] >> bit) & 1) == 1)
{
rotate(a);
write_op("ra\n");
}
else
{
push(a, b);
write_op("pb\n");
}
j++;
}
}
static void radix_sort(t_stack *a, t_stack *b)
{
int bits;
int i;
i = 0;
bits = get_max_bits(a);
while (i < bits)
{
radix_sort_2(a, b, i);
push_radix(a, b);
i++;
}
}
void sort_stack(t_stack *a, t_stack *b)
{
if (a->size <= 1)
return ;
if (a->size <= 5)
sort_five(a, b);
else
radix_sort(a, b);
}

BIN
src/radix.o Normal file

Binary file not shown.

105
src/sort_small.c Normal file
View File

@ -0,0 +1,105 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_small.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gechavia <chaviallegeraud@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/04 17:33:02 by gechavia #+# #+# */
/* Updated: 2025/09/04 17:33:03 by gechavia ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int min_index(t_stack *a)
{
int i;
int m;
int idx;
i = 0;
m = a->array[0];
idx = 0;
while (i < a->size)
{
if (a->array[i] < m)
{
m = a->array[i];
idx = i;
}
i++;
}
return (idx);
}
static void op1(t_stack *s, void (*f)(t_stack *), const char *name)
{
f(s);
write_op(name);
}
void sort_three(t_stack *a)
{
int x;
int y;
int z;
x = a->array[0];
y = a->array[1];
z = a->array[2];
if (x > y && y > z)
{
op1(a, swap, "sa\n");
op1(a, rrotate, "rra\n");
}
else if (x > y && x < z)
op1(a, swap, "sa\n");
else if (x > z && y < z)
op1(a, rotate, "ra\n");
else if (x < y && x > z)
op1(a, rrotate, "rra\n");
else
{
op1(a, swap, "sa\n");
op1(a, rotate, "ra\n");
}
}
static void push_min_to_b(t_stack *a, t_stack *b)
{
int idx;
int steps;
idx = min_index(a);
if (idx <= a->size / 2)
steps = idx;
else
steps = a->size - idx;
while (steps-- > 0)
{
if (idx <= a->size / 2)
op1(a, rotate, "ra\n");
else
op1(a, rrotate, "rra\n");
}
push(a, b);
write_op("pb\n");
}
void sort_five(t_stack *a, t_stack *b)
{
if (a->size == 2 && a->array[0] > a->array[1])
{
op1(a, swap, "sa\n");
return ;
}
while (a->size > 3)
push_min_to_b(a, b);
sort_three(a);
while (b->size)
{
push(b, a);
write_op("pa\n");
}
}

BIN
src/sort_small.o Normal file

Binary file not shown.

41
src/utils.c Normal file
View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gechavia <chaviallegeraud@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/04 17:33:05 by gechavia #+# #+# */
/* Updated: 2025/09/04 17:35:51 by gechavia ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void free_stacks(t_stack *a, t_stack *b)
{
if (a && a->array)
free(a->array);
if (b && b->array)
free(b->array);
}
int has_duplicates(t_stack *a)
{
int i;
int j;
i = 0;
while (i < a->size)
{
j = i + 1;
while (j < a->size)
{
if (a->array[i] == a->array[j])
return (1);
j++;
}
i++;
}
return (0);
}

BIN
src/utils.o Normal file

Binary file not shown.