Presque fini, il reste plus qu'a géré les doubles
This commit is contained in:
parent
8f7727c38e
commit
dd914622ed
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
*.o
|
||||||
|
checker_linux
|
||||||
|
*.swp
|
BIN
.push_swap.c.swm
Normal file
BIN
.push_swap.c.swm
Normal file
Binary file not shown.
2
Makefile
2
Makefile
|
@ -10,7 +10,7 @@
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
SRCS = push_swap.c
|
SRCS = push_swap.c operations.c tools.c parsing.c
|
||||||
OBJS = ${SRCS:.c=.o}
|
OBJS = ${SRCS:.c=.o}
|
||||||
CC = clang
|
CC = clang
|
||||||
CFLAGS = -g -Wall -Wextra -Werror
|
CFLAGS = -g -Wall -Wextra -Werror
|
||||||
|
|
1
libft
Submodule
1
libft
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit ca53e947c847ec3b893d5df34bc8051eaf30bf73
|
60
operations.c
Normal file
60
operations.c
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* operations.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/12/07 18:36:09 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2022/12/07 18:36:10 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
void swap(t_stack *s, char *message)
|
||||||
|
{
|
||||||
|
long c;
|
||||||
|
|
||||||
|
c = (*s).list[0];
|
||||||
|
(*s).list[0] = (*s).list[1];
|
||||||
|
(*s).list[1] = c;
|
||||||
|
write(1, "s", 1);
|
||||||
|
write(1, message, 1);
|
||||||
|
write(1, "\n", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void push(t_stack *s_push, t_stack *s_receive, char *message)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
long push;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
push = (*s_push).list[0];
|
||||||
|
while (++i < (*s_push).len)
|
||||||
|
(*s_push).list[i - 1] = (*s_push).list[i];
|
||||||
|
i = (*s_receive).len;
|
||||||
|
while (i-- > 0)
|
||||||
|
(*s_receive).list[i + 1] = (*s_receive).list[i];
|
||||||
|
(*s_receive).list[0] = push;
|
||||||
|
(*s_receive).len += 1;
|
||||||
|
(*s_push).len -= 1;
|
||||||
|
write(1, "p", 1);
|
||||||
|
write(1, message, 1);
|
||||||
|
write(1, "\n", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void rotate(t_stack *s, char *message)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
long rotate;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
rotate = (*s).list[0];
|
||||||
|
while (++i < (*s).len)
|
||||||
|
(*s).list[i - 1] = (*s).list[i];
|
||||||
|
(*s).list[(*s).len - 1] = rotate;
|
||||||
|
write(1, "r", 1);
|
||||||
|
write(1, message, 1);
|
||||||
|
write(1, "\n", 1);
|
||||||
|
}
|
59
parsing.c
Normal file
59
parsing.c
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parsing.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/12/07 19:23:59 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2022/12/07 19:24:04 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
void choice(long *tab, int y)
|
||||||
|
{
|
||||||
|
if (y == 4 || y > 5)
|
||||||
|
pre_push_swap(tab, y);
|
||||||
|
else if (y == 5)
|
||||||
|
push_swap_5(tab, y);
|
||||||
|
else if (y == 3)
|
||||||
|
push_swap_3(tab, y);
|
||||||
|
else if (y == 2)
|
||||||
|
if (tab[0] > tab[1])
|
||||||
|
write(1, "ra\n", 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
int parsing(int argc, char *argv[], long *tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int y;
|
||||||
|
int x;
|
||||||
|
char **tmp;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
y = 0;
|
||||||
|
while (++i < argc)
|
||||||
|
{
|
||||||
|
x = 0;
|
||||||
|
while (argv[i][x])
|
||||||
|
if (ft_strchr("\a\b\t\n\v\f\r", argv[i][x++]) != NULL)
|
||||||
|
argv[i][x - 1] = ' ';
|
||||||
|
x = 0;
|
||||||
|
tmp = ft_split(argv[i], ' ');
|
||||||
|
while (tmp[x] != NULL)
|
||||||
|
{
|
||||||
|
if (ft_atoi_check(tmp[x]))
|
||||||
|
tab[y++] = ft_atoi(tmp[x]);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
write(1, "Error\n", 6);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
x++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
choice(tab, y);
|
||||||
|
return (0);
|
||||||
|
}
|
248
push_swap.c
248
push_swap.c
|
@ -6,21 +6,18 @@
|
||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/23 16:48:12 by erey-bet #+# #+# */
|
/* Created: 2022/11/23 16:48:12 by erey-bet #+# #+# */
|
||||||
/* Updated: 2022/12/05 22:58:30 by erey-bet ### ########.fr */
|
/* Updated: 2022/12/07 18:35:18 by erey-bet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft/libft.h"
|
|
||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
/*void ft_putchar(char c)
|
||||||
|
|
||||||
void ft_putchar(char c)
|
|
||||||
{
|
{
|
||||||
write(1, &c, 1);
|
write(1, &c, 1);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
void print_stack(t_stack sa, t_stack sb)
|
/*void print_stack(t_stack sa, t_stack sb)
|
||||||
{
|
{
|
||||||
write(1, "A: ", 3);
|
write(1, "A: ", 3);
|
||||||
for (int i = 0; i < sa.len; i++)
|
for (int i = 0; i < sa.len; i++)
|
||||||
|
@ -39,182 +36,115 @@ void print_stack(t_stack sa, t_stack sb)
|
||||||
}
|
}
|
||||||
ft_putchar('\n');
|
ft_putchar('\n');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int get_min(long *tab, int len)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
int index;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
index = 0;
|
|
||||||
while (++i < len)
|
|
||||||
if (tab[i] <= tab[index])
|
|
||||||
index = i;
|
|
||||||
return (index);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int *get_index(long *tab, int len)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
int min;
|
|
||||||
unsigned int *new;
|
|
||||||
|
|
||||||
i = -1;
|
|
||||||
min = tab[0];
|
|
||||||
new = malloc(sizeof(unsigned int) * (len + 1));
|
|
||||||
while (++i < len)
|
|
||||||
{
|
|
||||||
min = get_min(tab, len);
|
|
||||||
tab[min] = 2147483648;
|
|
||||||
new[min] = i;
|
|
||||||
}
|
|
||||||
return (new);
|
|
||||||
}
|
|
||||||
|
|
||||||
void swap(t_stack *s, char *message)
|
|
||||||
{
|
|
||||||
unsigned int c;
|
|
||||||
|
|
||||||
c = (*s).list[0];
|
|
||||||
(*s).list[0] = (*s).list[1];
|
|
||||||
(*s).list[1] = c;
|
|
||||||
write(1, "s", 1);
|
|
||||||
write(1, message, 1);
|
|
||||||
write(1, "\n", 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void push(t_stack *s_push, t_stack *s_receive, char *message)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
unsigned int push;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
push = (*s_push).list[0];
|
|
||||||
while (++i < (*s_push).len)
|
|
||||||
(*s_push).list[i - 1] = (*s_push).list[i];
|
|
||||||
i = (*s_receive).len;
|
|
||||||
while (i-- > 0)
|
|
||||||
(*s_receive).list[i + 1] = (*s_receive).list[i];
|
|
||||||
(*s_receive).list[0] = push;
|
|
||||||
(*s_receive).len += 1;
|
|
||||||
(*s_push).len -= 1;
|
|
||||||
write(1, "p", 1);
|
|
||||||
write(1, message, 1);
|
|
||||||
write(1, "\n", 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void rotate(t_stack *s, char *message)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
unsigned int push;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
push = (*s).list[0];
|
|
||||||
while (++i < (*s).len)
|
|
||||||
(*s).list[i - 1] = (*s).list[i];
|
|
||||||
(*s).list[(*s).len - 1] = push;
|
|
||||||
write(1, "r", 1);
|
|
||||||
write(1, message, 1);
|
|
||||||
write(1, "\n", 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*void push_number(t_stack *sa, int i)
|
|
||||||
{
|
|
||||||
|
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
int max_len_binary(t_stack s)
|
/*void print_one_stack(t_stack s)
|
||||||
{
|
{
|
||||||
unsigned int i;
|
write(1, "A: ", 3);
|
||||||
int x;
|
for (int i = 0; i < s.len; i++)
|
||||||
int y;
|
|
||||||
int max;
|
|
||||||
|
|
||||||
y = -1;
|
|
||||||
max = 1;
|
|
||||||
while (++y < s.len)
|
|
||||||
{
|
{
|
||||||
i = 1;
|
ft_putchar(s.list[i] + '0');
|
||||||
x = 1;
|
write(1, ", ", 2);
|
||||||
while (i < s.list[y])
|
}
|
||||||
{
|
ft_putchar('\n');
|
||||||
i *= 2;
|
}*/
|
||||||
x++;
|
|
||||||
}
|
void post_push_swap(t_stack sa, t_stack sb, int x, int sa_len)
|
||||||
if (x > max)
|
{
|
||||||
max = x;
|
int i;
|
||||||
|
int y;
|
||||||
|
|
||||||
|
y = 0;
|
||||||
|
while (x-- > 0 && !check_sa(sa))
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
while (i < sa_len && !(check_sa(sa) && sb.len == 0))
|
||||||
|
{
|
||||||
|
if (((sa.list[0] >> y) & 1) == 0)
|
||||||
|
push(&sa, &sb, "b");
|
||||||
|
else
|
||||||
|
rotate(&sa, "a");
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (sb.len > 0)
|
||||||
|
push(&sb, &sa, "a");
|
||||||
|
y++;
|
||||||
}
|
}
|
||||||
return (max);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void pre_push_swap(long *tab, int len)
|
||||||
void push_swap(long *tab, int len)
|
|
||||||
{
|
{
|
||||||
int i;
|
|
||||||
int y;
|
|
||||||
int x;
|
int x;
|
||||||
int len_sa;
|
int sa_len;
|
||||||
|
t_stack sa;
|
||||||
|
t_stack sb;
|
||||||
|
|
||||||
|
sa.list = get_index(tab, len);
|
||||||
|
if (sa.list == NULL)
|
||||||
|
return ;
|
||||||
|
sa.len = len;
|
||||||
|
sa_len = len;
|
||||||
|
sb.list = malloc(sizeof(long) * (len + 1));
|
||||||
|
if (sb.list == NULL)
|
||||||
|
return ;
|
||||||
|
sb.len = 0;
|
||||||
|
x = max_len_binary(sa);
|
||||||
|
post_push_swap(sa, sb, x, sa_len);
|
||||||
|
free(sa.list);
|
||||||
|
free(sb.list);
|
||||||
|
}
|
||||||
|
|
||||||
|
void push_swap_3(long *tab, int len)
|
||||||
|
{
|
||||||
|
t_stack sa;
|
||||||
|
|
||||||
|
sa.list = get_index(tab, len);
|
||||||
|
sa.len = len;
|
||||||
|
while (!check_sa(sa))
|
||||||
|
{
|
||||||
|
if (sa.list[0] > sa.list[1])
|
||||||
|
{
|
||||||
|
rotate(&sa, "a");
|
||||||
|
if (sa.list[0] > sa.list[1])
|
||||||
|
swap(&sa, "a");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
swap(&sa, "a");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void push_swap_5(long *tab, int len)
|
||||||
|
{
|
||||||
t_stack sa;
|
t_stack sa;
|
||||||
t_stack sb;
|
t_stack sb;
|
||||||
|
|
||||||
sa.list = get_index(tab, len);
|
sa.list = get_index(tab, len);
|
||||||
sa.len = len;
|
sa.len = len;
|
||||||
sb.list = malloc(sizeof(unsigned int) * (len + 1));
|
sb.list = malloc(sizeof(long) * (len + 1));
|
||||||
|
if (sb.list == NULL)
|
||||||
|
return ;
|
||||||
sb.len = 0;
|
sb.len = 0;
|
||||||
print_stack(sa, sb);
|
while (sb.len != 2)
|
||||||
x = max_len_binary(sa);
|
|
||||||
y = 0;
|
|
||||||
while (x-- > 0)
|
|
||||||
{
|
{
|
||||||
i = 0;
|
while (get_min(sa.list, sa.len) != 0)
|
||||||
len_sa = sa.len;
|
rotate(&sa, "a");
|
||||||
while (i < len_sa)
|
push(&sa, &sb, "b");
|
||||||
{
|
|
||||||
if (((sa.list[0] >> y) & 1) == 0)
|
|
||||||
{
|
|
||||||
push(&sa, &sb, "b");
|
|
||||||
print_stack(sa, sb);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rotate(&sa, "a");
|
|
||||||
print_stack(sa, sb);
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
while (sb.len > 0)
|
|
||||||
{
|
|
||||||
push(&sb, &sa, "a");
|
|
||||||
print_stack(sa, sb);
|
|
||||||
}
|
|
||||||
y++;
|
|
||||||
}
|
}
|
||||||
|
push_swap_3(sa.list, sa.len);
|
||||||
|
while (sb.len > 0)
|
||||||
|
push(&sb, &sa, "a");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
long *tab;
|
long *tab;
|
||||||
int i;
|
|
||||||
int y;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
y = 0;
|
|
||||||
tab = malloc(sizeof(long) * (argc + 1));
|
tab = malloc(sizeof(long) * (argc + 1));
|
||||||
|
if (tab == NULL)
|
||||||
|
return (1);
|
||||||
if (argc > 1)
|
if (argc > 1)
|
||||||
{
|
if (parsing(argc, argv, tab) == 1)
|
||||||
while (++i < argc)
|
return (1);
|
||||||
{
|
free(tab);
|
||||||
if(ft_atoi_check(argv[i]))
|
|
||||||
tab[y++] = ft_atoi(argv[i]);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
write(1, "Error\n", 6);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
push_swap(tab, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
25
push_swap.h
25
push_swap.h
|
@ -15,11 +15,32 @@
|
||||||
|
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
# include <stdlib.h>
|
# include <stdlib.h>
|
||||||
|
# include "libft/libft.h"
|
||||||
|
|
||||||
typedef struct s_stack
|
typedef struct s_stack
|
||||||
{
|
{
|
||||||
unsigned int *list;
|
long *list;
|
||||||
int len;
|
int len;
|
||||||
} t_stack;
|
} t_stack;
|
||||||
|
|
||||||
|
// Push Swap
|
||||||
|
void post_push_swap(t_stack sa, t_stack sb, int x, int sa_len);
|
||||||
|
void pre_push_swap(long *tab, int len);
|
||||||
|
void push_swap_5(long *tab, int len);
|
||||||
|
void push_swap_3(long *tab, int len);
|
||||||
|
|
||||||
|
// Operations
|
||||||
|
void swap(t_stack *s, char *message);
|
||||||
|
void push(t_stack *s_push, t_stack *s_receive, char *message);
|
||||||
|
void rotate(t_stack *s, char *message);
|
||||||
|
|
||||||
|
// Tools
|
||||||
|
int get_min(long *tab, int len);
|
||||||
|
long *get_index(long *tab, int len);
|
||||||
|
int max_len_binary(t_stack s);
|
||||||
|
int check_sa(t_stack s);
|
||||||
|
|
||||||
|
// Parsing
|
||||||
|
int parsing(int argc, char *argv[], long *tab);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
BIN
push_swap.o
BIN
push_swap.o
Binary file not shown.
86
tools.c
Normal file
86
tools.c
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* tools.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/12/07 18:37:38 by erey-bet #+# #+# */
|
||||||
|
/* Updated: 2022/12/07 18:37:39 by erey-bet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
int get_min(long *tab, int len)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int index;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
index = 0;
|
||||||
|
while (++i < len)
|
||||||
|
if (tab[i] <= tab[index])
|
||||||
|
index = i;
|
||||||
|
return (index);
|
||||||
|
}
|
||||||
|
|
||||||
|
long *get_index(long *tab, int len)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int min;
|
||||||
|
long *new;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
min = tab[0];
|
||||||
|
new = malloc(sizeof(long) * (len + 1));
|
||||||
|
if (new == NULL)
|
||||||
|
return (NULL);
|
||||||
|
while (++i < len)
|
||||||
|
{
|
||||||
|
min = get_min(tab, len);
|
||||||
|
tab[min] = 2147483648;
|
||||||
|
new[min] = i;
|
||||||
|
}
|
||||||
|
return (new);
|
||||||
|
}
|
||||||
|
|
||||||
|
int max_len_binary(t_stack s)
|
||||||
|
{
|
||||||
|
long i;
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int max;
|
||||||
|
|
||||||
|
y = -1;
|
||||||
|
max = 1;
|
||||||
|
while (++y < s.len)
|
||||||
|
{
|
||||||
|
i = 1;
|
||||||
|
x = 1;
|
||||||
|
while (i < s.list[y])
|
||||||
|
{
|
||||||
|
i *= 2;
|
||||||
|
x++;
|
||||||
|
}
|
||||||
|
if (x > max)
|
||||||
|
max = x;
|
||||||
|
}
|
||||||
|
return (max);
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_sa(t_stack s)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < s.len - 1)
|
||||||
|
{
|
||||||
|
if (s.list[i] > s.list[i + 1])
|
||||||
|
{
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
Loading…
Reference in a new issue