Push Swap
This commit is contained in:
30
Makefile
Normal file
30
Makefile
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
NAME = push_swap
|
||||||
|
|
||||||
|
SRCS = cost.c main.c init.c moves.c position.c push.c rotate.c rrotate.c sort.c stack.c string.c swap.c three_sort.c utils.c
|
||||||
|
|
||||||
|
HEADER = ./push_swap.h
|
||||||
|
|
||||||
|
CC = cc
|
||||||
|
CFLAGS = -Wall -Wextra -Werror
|
||||||
|
RM = rm -f
|
||||||
|
|
||||||
|
%o: %.c
|
||||||
|
${CC} ${CFLAGS} -c $< -o $@
|
||||||
|
|
||||||
|
OBJS = ${SRCS:.c=.o}
|
||||||
|
|
||||||
|
all: ${NAME}
|
||||||
|
|
||||||
|
${NAME}: ${OBJS} $(HEADER)
|
||||||
|
${CC} ${SRCS} -o ${NAME}
|
||||||
|
|
||||||
|
clean:
|
||||||
|
${RM} ${OBJS}
|
||||||
|
|
||||||
|
fclean:
|
||||||
|
${RM} ${OBJS}
|
||||||
|
${RM} ${NAME}
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
|
|
||||||
|
.PHONY: all clean fclean re
|
||||||
58
cost.c
Normal file
58
cost.c
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* cost.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/08/14 06:25:35 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2024/08/15 06:27:14 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
void cost(t_stack **stack_a, t_stack **stack_b)
|
||||||
|
{
|
||||||
|
t_stack *tmp_a;
|
||||||
|
t_stack *tmp_b;
|
||||||
|
int size_a;
|
||||||
|
int size_b;
|
||||||
|
|
||||||
|
tmp_a = *stack_a;
|
||||||
|
tmp_b = *stack_b;
|
||||||
|
size_a = get_stack_size(tmp_a);
|
||||||
|
size_b = get_stack_size(tmp_b);
|
||||||
|
while (tmp_b)
|
||||||
|
{
|
||||||
|
tmp_b->cost_b = tmp_b->pos;
|
||||||
|
if (tmp_b->pos > size_b / 2)
|
||||||
|
tmp_b->cost_b = (size_b - tmp_b->pos) * -1;
|
||||||
|
tmp_b->cost_a = tmp_b->target;
|
||||||
|
if (tmp_b->target > size_a / 2)
|
||||||
|
tmp_b->cost_a = (size_a - tmp_b->target) * -1;
|
||||||
|
tmp_b = tmp_b->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void cheapest_move(t_stack **stack_a, t_stack **stack_b)
|
||||||
|
{
|
||||||
|
t_stack *tmp;
|
||||||
|
int cheapest;
|
||||||
|
int cost_a;
|
||||||
|
int cost_b;
|
||||||
|
|
||||||
|
tmp = *stack_b;
|
||||||
|
cheapest = INT_MAX;
|
||||||
|
while (tmp)
|
||||||
|
{
|
||||||
|
if (abs(tmp->cost_a) + abs(tmp->cost_b) < cheapest)
|
||||||
|
{
|
||||||
|
cheapest = abs(tmp->cost_a) + abs(tmp->cost_b);
|
||||||
|
cost_a = tmp->cost_a;
|
||||||
|
cost_b = tmp->cost_b;
|
||||||
|
}
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
do_move(stack_a, stack_b, cost_a, cost_b);
|
||||||
|
}
|
||||||
74
init.c
Normal file
74
init.c
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* init.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/08/12 05:40:30 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2024/08/16 06:36:55 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
long input_is_correct(char *str)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if ((str[i] == '-' || str[i] == '+') && (ft_strlen(str) > 1))
|
||||||
|
i++;
|
||||||
|
while (str[i] != '\0')
|
||||||
|
{
|
||||||
|
while (str[i] < '0' || str[i] > '9')
|
||||||
|
return (0);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_duplicated(t_stack *column)
|
||||||
|
{
|
||||||
|
t_stack *tmp;
|
||||||
|
t_stack *tmp2;
|
||||||
|
|
||||||
|
tmp = column;
|
||||||
|
while (tmp)
|
||||||
|
{
|
||||||
|
tmp2 = tmp->next;
|
||||||
|
while (tmp2)
|
||||||
|
{
|
||||||
|
if (tmp->value == tmp2->value)
|
||||||
|
return (1);
|
||||||
|
tmp2 = tmp2->next;
|
||||||
|
}
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_index(t_stack *stack_a, int stack_size)
|
||||||
|
{
|
||||||
|
t_stack *ptr;
|
||||||
|
t_stack *biggest;
|
||||||
|
int min_value;
|
||||||
|
|
||||||
|
while (--stack_size > 0)
|
||||||
|
{
|
||||||
|
ptr = stack_a;
|
||||||
|
biggest = NULL;
|
||||||
|
min_value = INT_MIN;
|
||||||
|
while (ptr)
|
||||||
|
{
|
||||||
|
if (ptr->value > min_value && ptr->index == 0)
|
||||||
|
{
|
||||||
|
min_value = ptr->value;
|
||||||
|
biggest = ptr;
|
||||||
|
}
|
||||||
|
ptr = ptr->next;
|
||||||
|
}
|
||||||
|
if (biggest != NULL)
|
||||||
|
biggest->index = stack_size;
|
||||||
|
}
|
||||||
|
}
|
||||||
81
main.c
Normal file
81
main.c
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/08/13 05:46:55 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2024/08/16 06:16:45 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
int is_sorted(t_stack *stack)
|
||||||
|
{
|
||||||
|
while (stack->next != NULL)
|
||||||
|
{
|
||||||
|
if (stack->value > stack->next->value)
|
||||||
|
return (0);
|
||||||
|
stack = stack->next;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void push_swap(t_stack **stack_a, t_stack **stack_b, int stack_size)
|
||||||
|
{
|
||||||
|
if (stack_size == 2 && !is_sorted(*stack_a))
|
||||||
|
ft_sa(stack_a);
|
||||||
|
else if (stack_size == 3 && !is_sorted(*stack_a))
|
||||||
|
three_sort(stack_a);
|
||||||
|
else if (stack_size > 3 && !is_sorted(*stack_a))
|
||||||
|
sort(stack_a, stack_b);
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_numbers(char *argv, t_stack **stack_a)
|
||||||
|
{
|
||||||
|
char **param;
|
||||||
|
long int n;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
param = ft_split(argv, ' ');
|
||||||
|
i = 0;
|
||||||
|
while (param[i] != NULL)
|
||||||
|
{
|
||||||
|
if (input_is_correct(param[i]))
|
||||||
|
{
|
||||||
|
n = ft_atoi(param[i]);
|
||||||
|
if (n > INT_MAX || n < INT_MIN)
|
||||||
|
error_exit(stack_a, NULL);
|
||||||
|
stack_add(stack_a, stack_new(n));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
error_exit(NULL, NULL);
|
||||||
|
free(param[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
free(param);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
t_stack *stack_a;
|
||||||
|
t_stack *stack_b;
|
||||||
|
int stack_size;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 1;
|
||||||
|
stack_a = NULL;
|
||||||
|
stack_b = NULL;
|
||||||
|
while (i < argc)
|
||||||
|
get_numbers(argv[i++], &stack_a);
|
||||||
|
if (is_duplicated(stack_a))
|
||||||
|
error_exit(&stack_a, NULL);
|
||||||
|
stack_size = get_stack_size(stack_a);
|
||||||
|
get_index(stack_a, stack_size + 1);
|
||||||
|
push_swap(&stack_a, &stack_b, stack_size);
|
||||||
|
free_stack(&stack_a);
|
||||||
|
free_stack(&stack_b);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
78
moves.c
Normal file
78
moves.c
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* moves.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/08/14 06:19:17 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2024/08/14 06:24:42 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
static void reverse_both(t_stack **a, t_stack **b, int *cost_a, int *cost_b)
|
||||||
|
{
|
||||||
|
while (*cost_a < 0 && *cost_b < 0)
|
||||||
|
{
|
||||||
|
(*cost_a)++;
|
||||||
|
(*cost_b)++;
|
||||||
|
ft_rrr(a, b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rotate_both(t_stack **a, t_stack **b, int *cost_a, int *cost_b)
|
||||||
|
{
|
||||||
|
while (*cost_a > 0 && *cost_b > 0)
|
||||||
|
{
|
||||||
|
(*cost_a)--;
|
||||||
|
(*cost_b)--;
|
||||||
|
ft_rr(a, b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rotate_a(t_stack **a, int *cost)
|
||||||
|
{
|
||||||
|
while (*cost)
|
||||||
|
{
|
||||||
|
if (*cost > 0)
|
||||||
|
{
|
||||||
|
ft_ra(a);
|
||||||
|
(*cost)--;
|
||||||
|
}
|
||||||
|
else if (*cost < 0)
|
||||||
|
{
|
||||||
|
ft_rra(a);
|
||||||
|
(*cost)++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rotate_b(t_stack **b, int *cost)
|
||||||
|
{
|
||||||
|
while (*cost)
|
||||||
|
{
|
||||||
|
if (*cost > 0)
|
||||||
|
{
|
||||||
|
ft_rb(b);
|
||||||
|
(*cost)--;
|
||||||
|
}
|
||||||
|
else if (*cost < 0)
|
||||||
|
{
|
||||||
|
ft_rrb(b);
|
||||||
|
(*cost)++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_move(t_stack **a, t_stack **b, int cost_a, int cost_b)
|
||||||
|
{
|
||||||
|
if (cost_a < 0 && cost_b < 0)
|
||||||
|
reverse_both(a, b, &cost_a, &cost_b);
|
||||||
|
else if (cost_a > 0 && cost_b > 0)
|
||||||
|
rotate_both(a, b, &cost_a, &cost_b);
|
||||||
|
rotate_a(a, &cost_a);
|
||||||
|
rotate_b(b, &cost_b);
|
||||||
|
ft_pa(a, b);
|
||||||
|
}
|
||||||
96
position.c
Normal file
96
position.c
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* position.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/08/13 05:12:56 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2024/08/13 05:32:46 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
static void get_position(t_stack **stack)
|
||||||
|
{
|
||||||
|
t_stack *tmp;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
tmp = *stack;
|
||||||
|
i = 0;
|
||||||
|
while (tmp)
|
||||||
|
{
|
||||||
|
tmp->pos = i;
|
||||||
|
tmp = tmp->next;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int get_target(t_stack **stack_a, int index_b, int target_i, int target)
|
||||||
|
{
|
||||||
|
t_stack *tmp;
|
||||||
|
|
||||||
|
tmp = *stack_a;
|
||||||
|
while (tmp)
|
||||||
|
{
|
||||||
|
if (tmp->index > index_b && tmp->index < target_i)
|
||||||
|
{
|
||||||
|
target_i = tmp->index;
|
||||||
|
target = tmp->pos;
|
||||||
|
}
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
if (target_i != INT_MAX)
|
||||||
|
return (target);
|
||||||
|
tmp = *stack_a;
|
||||||
|
while (tmp)
|
||||||
|
{
|
||||||
|
if (tmp->index < target_i)
|
||||||
|
{
|
||||||
|
target_i = tmp->index;
|
||||||
|
target = tmp->pos;
|
||||||
|
}
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
return (target);
|
||||||
|
}
|
||||||
|
|
||||||
|
int position_lowest_index(t_stack **stack)
|
||||||
|
{
|
||||||
|
t_stack *tmp;
|
||||||
|
int lowest_i;
|
||||||
|
int lowest_p;
|
||||||
|
|
||||||
|
tmp = *stack;
|
||||||
|
lowest_i = INT_MAX;
|
||||||
|
get_position(stack);
|
||||||
|
lowest_p = tmp->pos;
|
||||||
|
while (tmp)
|
||||||
|
{
|
||||||
|
if (tmp->index < lowest_i)
|
||||||
|
{
|
||||||
|
lowest_i = tmp->index;
|
||||||
|
lowest_p = tmp->pos;
|
||||||
|
}
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
return (lowest_p);
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_target_position(t_stack **stack_a, t_stack **stack_b)
|
||||||
|
{
|
||||||
|
t_stack *tmp;
|
||||||
|
int target;
|
||||||
|
|
||||||
|
tmp = *stack_b;
|
||||||
|
get_position(stack_a);
|
||||||
|
get_position(stack_b);
|
||||||
|
target = 0;
|
||||||
|
while (tmp)
|
||||||
|
{
|
||||||
|
target = get_target(stack_a, tmp->index, INT_MAX, target);
|
||||||
|
tmp->target = target;
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
37
push.c
Normal file
37
push.c
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* push.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/08/12 06:25:15 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2024/08/13 05:35:48 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
static void push(t_stack **src, t_stack **dst)
|
||||||
|
{
|
||||||
|
t_stack *tmp;
|
||||||
|
|
||||||
|
if (*src == NULL)
|
||||||
|
return ;
|
||||||
|
tmp = (*src)->next;
|
||||||
|
(*src)->next = *dst;
|
||||||
|
*dst = *src;
|
||||||
|
*src = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_pa(t_stack **stack_a, t_stack **stack_b)
|
||||||
|
{
|
||||||
|
push(stack_b, stack_a);
|
||||||
|
ft_putstr("pa\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_pb(t_stack **stack_a, t_stack **stack_b)
|
||||||
|
{
|
||||||
|
push(stack_a, stack_b);
|
||||||
|
ft_putstr("pb\n");
|
||||||
|
}
|
||||||
81
push_swap.h
Normal file
81
push_swap.h
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* push_swap.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/08/08 06:50:52 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2024/08/15 06:26:49 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef PUSH_SWAP_H
|
||||||
|
# define PUSH_SWAP_H
|
||||||
|
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <limits.h>
|
||||||
|
# include <stdio.h>
|
||||||
|
|
||||||
|
typedef struct s_stack
|
||||||
|
{
|
||||||
|
int value;
|
||||||
|
int index;
|
||||||
|
int pos;
|
||||||
|
int target;
|
||||||
|
int cost_a;
|
||||||
|
int cost_b;
|
||||||
|
struct s_stack *next;
|
||||||
|
} t_stack;
|
||||||
|
|
||||||
|
// Sorts
|
||||||
|
void three_sort(t_stack **stack);
|
||||||
|
void sort(t_stack **stack_a, t_stack **stack_b);
|
||||||
|
|
||||||
|
void do_move(t_stack **a, t_stack **b, int cost_a, int cost_b);
|
||||||
|
void cost(t_stack **stack_a, t_stack **stack_b);
|
||||||
|
void cheapest_move(t_stack **stack_a, t_stack **stack_b);
|
||||||
|
|
||||||
|
// Swap
|
||||||
|
void ft_sa(t_stack **stack_a);
|
||||||
|
void ft_sb(t_stack **stack_b);
|
||||||
|
void ft_ss(t_stack **stack_a, t_stack **stack_b);
|
||||||
|
void ft_pa(t_stack **stack_a, t_stack **stack_b);
|
||||||
|
void ft_pb(t_stack **stack_a, t_stack **stack_b);
|
||||||
|
void ft_ra(t_stack **stack_a);
|
||||||
|
void ft_rb(t_stack **stack_b);
|
||||||
|
void ft_rr(t_stack **stack_a, t_stack **stack_b);
|
||||||
|
void ft_rra(t_stack **stack_a);
|
||||||
|
void ft_rrb(t_stack **stack_b);
|
||||||
|
void ft_rrr(t_stack **stack_a, t_stack **stack_b);
|
||||||
|
|
||||||
|
// Stack
|
||||||
|
t_stack *stack_new(int value);
|
||||||
|
void stack_add(t_stack **stack, t_stack *new);
|
||||||
|
t_stack *get_bottom(t_stack *stack);
|
||||||
|
t_stack *before_bottom(t_stack *stack);
|
||||||
|
|
||||||
|
// Utils
|
||||||
|
void free_stack(t_stack **stack);
|
||||||
|
void error_exit(t_stack **stack_a, t_stack **stack_b);
|
||||||
|
|
||||||
|
long int ft_atoi(const char *str);
|
||||||
|
void ft_putstr(char *str);
|
||||||
|
int abs(int nb);
|
||||||
|
size_t ft_strlen(const char *str);
|
||||||
|
|
||||||
|
char **ft_split(char const *s, char c);
|
||||||
|
|
||||||
|
// Utils Stack
|
||||||
|
|
||||||
|
long input_is_correct(char *str);
|
||||||
|
int is_duplicated(t_stack *column);
|
||||||
|
void get_index(t_stack *stack_a, int stack_size);
|
||||||
|
|
||||||
|
int position_lowest_index(t_stack **stack);
|
||||||
|
void get_target_position(t_stack **stack_a, t_stack **stack_b);
|
||||||
|
int get_stack_size(t_stack *stack);
|
||||||
|
int is_sorted(t_stack *stack);
|
||||||
|
|
||||||
|
#endif
|
||||||
44
rotate.c
Normal file
44
rotate.c
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* rotate.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/08/13 05:37:54 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2024/08/13 05:43:18 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
static void rotate(t_stack **stack)
|
||||||
|
{
|
||||||
|
t_stack *tmp;
|
||||||
|
t_stack *tail;
|
||||||
|
|
||||||
|
tmp = *stack;
|
||||||
|
*stack = (*stack)->next;
|
||||||
|
tail = get_bottom(*stack);
|
||||||
|
tmp->next = NULL;
|
||||||
|
tail->next = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_ra(t_stack **stack_a)
|
||||||
|
{
|
||||||
|
rotate(stack_a);
|
||||||
|
ft_putstr("ra\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_rb(t_stack **stack_b)
|
||||||
|
{
|
||||||
|
rotate(stack_b);
|
||||||
|
ft_putstr("rb\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_rr(t_stack **stack_a, t_stack **stack_b)
|
||||||
|
{
|
||||||
|
rotate(stack_a);
|
||||||
|
rotate(stack_b);
|
||||||
|
ft_putstr("rr\n");
|
||||||
|
}
|
||||||
46
rrotate.c
Normal file
46
rrotate.c
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* rrotate.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/08/13 05:30:07 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2024/08/13 05:37:15 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
static void r_rotate(t_stack **stack)
|
||||||
|
{
|
||||||
|
t_stack *tmp;
|
||||||
|
t_stack *tail;
|
||||||
|
t_stack *new_tail;
|
||||||
|
|
||||||
|
tail = get_bottom(*stack);
|
||||||
|
new_tail = before_bottom(*stack);
|
||||||
|
tmp = *stack;
|
||||||
|
*stack = tail;
|
||||||
|
(*stack)->next = tmp;
|
||||||
|
new_tail->next = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_rra(t_stack **stack_a)
|
||||||
|
{
|
||||||
|
r_rotate(stack_a);
|
||||||
|
ft_putstr("rra\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_rrb(t_stack **stack_b)
|
||||||
|
{
|
||||||
|
r_rotate(stack_b);
|
||||||
|
ft_putstr("rrb\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_rrr(t_stack **stack_a, t_stack **stack_b)
|
||||||
|
{
|
||||||
|
r_rotate(stack_a);
|
||||||
|
r_rotate(stack_b);
|
||||||
|
ft_putstr("rrr\n");
|
||||||
|
}
|
||||||
79
sort.c
Normal file
79
sort.c
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* sort.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/08/14 05:58:30 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2024/08/14 06:30:25 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
static void push_init(t_stack **stack_a, t_stack **stack_b)
|
||||||
|
{
|
||||||
|
int stack_size;
|
||||||
|
int push;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
push = 0;
|
||||||
|
i = 0;
|
||||||
|
stack_size = get_stack_size(*stack_a);
|
||||||
|
while (stack_size < 6 && i < stack_size && push < stack_size / 2)
|
||||||
|
{
|
||||||
|
if ((*stack_a)->index <= stack_size / 2)
|
||||||
|
{
|
||||||
|
ft_pb(stack_a, stack_b);
|
||||||
|
push++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ft_ra(stack_a);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (stack_size - push > 3)
|
||||||
|
{
|
||||||
|
ft_pb(stack_a, stack_b);
|
||||||
|
push++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void sort_stack(t_stack **stack_a)
|
||||||
|
{
|
||||||
|
int lowest_p;
|
||||||
|
int stack_size;
|
||||||
|
|
||||||
|
stack_size = get_stack_size(*stack_a);
|
||||||
|
lowest_p = position_lowest_index(stack_a);
|
||||||
|
if (lowest_p > stack_size / 2)
|
||||||
|
{
|
||||||
|
while (lowest_p < stack_size)
|
||||||
|
{
|
||||||
|
ft_rra(stack_a);
|
||||||
|
lowest_p++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while (lowest_p > 0)
|
||||||
|
{
|
||||||
|
ft_ra(stack_a);
|
||||||
|
lowest_p--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sort(t_stack **stack_a, t_stack **stack_b)
|
||||||
|
{
|
||||||
|
push_init(stack_a, stack_b);
|
||||||
|
three_sort(stack_a);
|
||||||
|
while (*stack_b)
|
||||||
|
{
|
||||||
|
get_target_position(stack_a, stack_b);
|
||||||
|
cost(stack_a, stack_b);
|
||||||
|
cheapest_move(stack_a, stack_b);
|
||||||
|
}
|
||||||
|
if (!is_sorted(*stack_a))
|
||||||
|
sort_stack(stack_a);
|
||||||
|
}
|
||||||
74
stack.c
Normal file
74
stack.c
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* stack.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/08/12 06:02:23 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2024/08/12 06:22:20 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
t_stack *stack_new(int value)
|
||||||
|
{
|
||||||
|
t_stack *new;
|
||||||
|
|
||||||
|
new = malloc(sizeof(t_stack));
|
||||||
|
if (!new)
|
||||||
|
return (NULL);
|
||||||
|
new->value = value;
|
||||||
|
new->index = 0;
|
||||||
|
new->pos = -1;
|
||||||
|
new->target = -1;
|
||||||
|
new->cost_a = -1;
|
||||||
|
new->cost_b = -1;
|
||||||
|
new->next = NULL;
|
||||||
|
return (new);
|
||||||
|
}
|
||||||
|
|
||||||
|
void stack_add(t_stack **stack, t_stack *new)
|
||||||
|
{
|
||||||
|
t_stack *bottom;
|
||||||
|
|
||||||
|
if (!new)
|
||||||
|
return ;
|
||||||
|
if (!*stack)
|
||||||
|
{
|
||||||
|
*stack = new;
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
bottom = get_bottom(*stack);
|
||||||
|
bottom->next = new;
|
||||||
|
}
|
||||||
|
|
||||||
|
t_stack *get_bottom(t_stack *stack)
|
||||||
|
{
|
||||||
|
while (stack && stack->next != NULL)
|
||||||
|
stack = stack->next;
|
||||||
|
return (stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_stack *before_bottom(t_stack *stack)
|
||||||
|
{
|
||||||
|
while (stack && stack->next->next != NULL)
|
||||||
|
stack = stack->next;
|
||||||
|
return (stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_stack_size(t_stack *stack)
|
||||||
|
{
|
||||||
|
int size;
|
||||||
|
|
||||||
|
size = 0;
|
||||||
|
if (!stack)
|
||||||
|
return (0);
|
||||||
|
while (stack)
|
||||||
|
{
|
||||||
|
stack = stack->next;
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
return (size);
|
||||||
|
}
|
||||||
92
string.c
Normal file
92
string.c
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* string.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/08/12 05:44:58 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2024/08/14 05:56:04 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
size_t ft_strlen(const char *str)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str[i] != '\0')
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned int countwords(const char *s, char c)
|
||||||
|
{
|
||||||
|
unsigned int count;
|
||||||
|
|
||||||
|
count = 0;
|
||||||
|
while (*s)
|
||||||
|
{
|
||||||
|
while (*s && c == *s)
|
||||||
|
s++;
|
||||||
|
if (*s)
|
||||||
|
count++;
|
||||||
|
while (*s && *s != c)
|
||||||
|
s++;
|
||||||
|
}
|
||||||
|
return (count);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *word_dup(const char *str, int start, int finish)
|
||||||
|
{
|
||||||
|
char *word;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
word = malloc((finish - start + 1) * sizeof(char));
|
||||||
|
if (!word)
|
||||||
|
return (NULL);
|
||||||
|
while (start < finish)
|
||||||
|
word[i++] = str[start++];
|
||||||
|
word[i] = '\0';
|
||||||
|
return (word);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *ft_cpy(size_t i, char const *s, char c, char **split)
|
||||||
|
{
|
||||||
|
int index;
|
||||||
|
size_t j;
|
||||||
|
|
||||||
|
index = -1;
|
||||||
|
j = 0;
|
||||||
|
while (i <= ft_strlen(s))
|
||||||
|
{
|
||||||
|
if (s[i] != c && index < 0)
|
||||||
|
index = i;
|
||||||
|
else if ((s[i] == c || i == ft_strlen(s)) && index >= 0)
|
||||||
|
{
|
||||||
|
split[j++] = word_dup(s, index, i);
|
||||||
|
index = -1;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
split[j] = 0;
|
||||||
|
return (split[j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **ft_split(char const *s, char c)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
char **split;
|
||||||
|
|
||||||
|
if (!s)
|
||||||
|
return (NULL);
|
||||||
|
split = malloc((countwords(s, c) + 1) * sizeof(char *));
|
||||||
|
if (!split)
|
||||||
|
return (NULL);
|
||||||
|
i = 0;
|
||||||
|
ft_cpy(i, s, c, split);
|
||||||
|
return (split);
|
||||||
|
}
|
||||||
44
swap.c
Normal file
44
swap.c
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* swap.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/08/12 05:35:56 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2024/08/12 05:47:04 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
static void swap(t_stack **stack)
|
||||||
|
{
|
||||||
|
t_stack *tmp;
|
||||||
|
|
||||||
|
if (!*stack || (*stack)->next == NULL)
|
||||||
|
return ;
|
||||||
|
tmp = *stack;
|
||||||
|
*stack = (*stack)->next;
|
||||||
|
tmp->next = (*stack)->next;
|
||||||
|
(*stack)->next = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_sa(t_stack **stack_a)
|
||||||
|
{
|
||||||
|
swap(stack_a);
|
||||||
|
ft_putstr("sa\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_sb(t_stack **stack_b)
|
||||||
|
{
|
||||||
|
swap(stack_b);
|
||||||
|
ft_putstr("sb\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_ss(t_stack **stack_a, t_stack **stack_b)
|
||||||
|
{
|
||||||
|
swap(stack_a);
|
||||||
|
swap(stack_b);
|
||||||
|
ft_putstr("ss\n");
|
||||||
|
}
|
||||||
42
three_sort.c
Normal file
42
three_sort.c
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* three_sort.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/08/13 06:22:36 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2024/08/13 06:29:01 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
static int biggest_index(t_stack *stack)
|
||||||
|
{
|
||||||
|
int index;
|
||||||
|
|
||||||
|
index = stack->index;
|
||||||
|
while (stack)
|
||||||
|
{
|
||||||
|
if (stack->index > index)
|
||||||
|
index = stack->index;
|
||||||
|
stack = stack->next;
|
||||||
|
}
|
||||||
|
return (index);
|
||||||
|
}
|
||||||
|
|
||||||
|
void three_sort(t_stack **stack)
|
||||||
|
{
|
||||||
|
int biggest;
|
||||||
|
|
||||||
|
if (is_sorted(*stack))
|
||||||
|
return ;
|
||||||
|
biggest = biggest_index(*stack);
|
||||||
|
if ((*stack)->index == biggest)
|
||||||
|
ft_ra(stack);
|
||||||
|
else if ((*stack)->next->index == biggest)
|
||||||
|
ft_rra(stack);
|
||||||
|
if ((*stack)->index > (*stack)->next->index)
|
||||||
|
ft_sa(stack);
|
||||||
|
}
|
||||||
80
utils.c
Normal file
80
utils.c
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/08/09 20:27:06 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2024/08/13 06:18:17 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
void free_stack(t_stack **stack)
|
||||||
|
{
|
||||||
|
t_stack *tmp;
|
||||||
|
|
||||||
|
if (!stack || !(*stack))
|
||||||
|
return ;
|
||||||
|
while (*stack)
|
||||||
|
{
|
||||||
|
tmp = (*stack)->next;
|
||||||
|
free(*stack);
|
||||||
|
*stack = tmp;
|
||||||
|
}
|
||||||
|
*stack = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void error_exit(t_stack **stack_a, t_stack **stack_b)
|
||||||
|
{
|
||||||
|
if (stack_a == NULL || *stack_a != NULL)
|
||||||
|
free_stack(stack_a);
|
||||||
|
if (stack_b == NULL || *stack_b != NULL)
|
||||||
|
free_stack(stack_b);
|
||||||
|
write(2, "Error\n", 6);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
long int ft_atoi(const char *str)
|
||||||
|
{
|
||||||
|
long int nb;
|
||||||
|
int neg;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
nb = 0;
|
||||||
|
i = 0;
|
||||||
|
neg = 1;
|
||||||
|
if (str[i] == '+')
|
||||||
|
i++;
|
||||||
|
else if (str[i] == '-')
|
||||||
|
{
|
||||||
|
neg *= -1;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (str[i] >= '0' && str[i] <= '9')
|
||||||
|
{
|
||||||
|
nb = (nb * 10) + (str[i] - '0');
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (nb * neg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_putstr(char *str)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str[i])
|
||||||
|
{
|
||||||
|
write(1, &str[i++], 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int abs(int nb)
|
||||||
|
{
|
||||||
|
if (nb < 0)
|
||||||
|
return (nb * -1);
|
||||||
|
return (nb);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user