221 lines
3.8 KiB
C
221 lines
3.8 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* push_swap.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2022/11/23 16:48:12 by erey-bet #+# #+# */
|
|
/* Updated: 2022/12/05 22:58:30 by erey-bet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "libft/libft.h"
|
|
#include "push_swap.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
void ft_putchar(char c)
|
|
{
|
|
write(1, &c, 1);
|
|
}
|
|
|
|
void print_stack(t_stack sa, t_stack sb)
|
|
{
|
|
write(1, "A: ", 3);
|
|
for (int i = 0; i < sa.len; i++)
|
|
{
|
|
ft_putchar(sa.list[i] + '0');
|
|
write(1, ", ", 2);
|
|
}
|
|
ft_putchar('\n');
|
|
if (sb.len > 0)
|
|
{
|
|
write(1, "B: ", 3);
|
|
for (int i = 0; i < sb.len; i++)
|
|
{
|
|
ft_putchar(sb.list[i] + '0');
|
|
write(1, ", ", 2);
|
|
}
|
|
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)
|
|
{
|
|
unsigned int 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);
|
|
}
|
|
|
|
|
|
void push_swap(long *tab, int len)
|
|
{
|
|
int i;
|
|
int y;
|
|
int x;
|
|
int len_sa;
|
|
t_stack sa;
|
|
t_stack sb;
|
|
|
|
sa.list = get_index(tab, len);
|
|
sa.len = len;
|
|
sb.list = malloc(sizeof(unsigned int) * (len + 1));
|
|
sb.len = 0;
|
|
print_stack(sa, sb);
|
|
x = max_len_binary(sa);
|
|
y = 0;
|
|
while (x-- > 0)
|
|
{
|
|
i = 0;
|
|
len_sa = sa.len;
|
|
while (i < len_sa)
|
|
{
|
|
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++;
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
long *tab;
|
|
int i;
|
|
int y;
|
|
|
|
i = 0;
|
|
y = 0;
|
|
tab = malloc(sizeof(long) * (argc + 1));
|
|
if (argc > 1)
|
|
{
|
|
while (++i < argc)
|
|
{
|
|
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);
|
|
}
|