diff --git a/.github/workflows/norminette.yml b/.github/workflows/norminette.yml index 43caef9..f4fc828 100644 --- a/.github/workflows/norminette.yml +++ b/.github/workflows/norminette.yml @@ -15,4 +15,4 @@ jobs: - uses: actions/checkout@v3 - uses: alexandregv/norminette-action@v3 with: - flags: "sources/ includes/" \ No newline at end of file + flags: "sources/ includes/ lib/" \ No newline at end of file diff --git a/gfantoni.sh b/gfantoni.sh index 7d0b4fd..96f232e 100755 --- a/gfantoni.sh +++ b/gfantoni.sh @@ -1,3 +1,11 @@ -!#/bin/bash +#!/bin/bash -find . sources lib includes \( -name "*.c" -or -name "*.h" -or -name Makefile \) -exec sed -e 's/josfelip/gfantoni/; s///' {} --in-place \; \ No newline at end of file +find . sources lib includes \( -name "*.c" -or -name "*.h" -or -name Makefile \) -exec sed -e 's/josfelip/gfantoni/; s///' {} --in-place \; + +rm -rf .github +rm -rf .gitignore +rm -rf qa +rm README.md +rm LICENSE +rm gfantoni.sh +rm josfelip.sh \ No newline at end of file diff --git a/josfelip.sh b/josfelip.sh new file mode 100644 index 0000000..dcf5559 --- /dev/null +++ b/josfelip.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +find . sources lib includes \( -name "*.c" -or -name "*.h" -or -name Makefile \) -exec sed -e 's/gfantoni/josfelip/; s///' {} --in-place \; + +rm -rf .github +rm -rf .gitignore +rm -rf qa +rm README.md +rm LICENSE +rm gfantoni.sh +rm josfelip.sh \ No newline at end of file diff --git a/mia/Makefile b/mia/Makefile deleted file mode 100644 index 5a3401c..0000000 --- a/mia/Makefile +++ /dev/null @@ -1,77 +0,0 @@ -CC = gcc -CFLAGS = -Wall -Werror -Wextra -NAME = push_swap - -SRC_PATH = src/ -OBJ_PATH = obj/ - -SRC = main.c \ - input_check.c input_check_utils.c \ - initialization.c \ - stack.c \ - swap.c push.c rotate.c reverse_rotate.c \ - sort_tiny.c sort.c \ - position.c cost.c do_move.c \ - utils.c -SRCS = $(addprefix $(SRC_PATH), $(SRC)) -OBJ = $(SRC:.c=.o) -OBJS = $(addprefix $(OBJ_PATH), $(OBJ)) -INCS = -I ./includes/ - -all: $(OBJ_PATH) $(NAME) - -$(OBJ_PATH)%.o: $(SRC_PATH)%.c - $(CC) $(CFLAGS) -c $< -o $@ $(INCS) - -$(OBJ_PATH): - mkdir $(OBJ_PATH) - -$(NAME): $(OBJS) - $(CC) $(CFLAGS) $(OBJS) -o $(NAME) - -clean: - rm -rf $(OBJ_PATH) - -fclean: clean - rm -f $(NAME) - -re: fclean all - -test2: $(NAME) - $(eval ARG = $(shell shuf -i 0-100 -n 2)) - ./push_swap $(ARG) | ./checker_linux $(ARG) - @echo -n "Instructions: " - @./push_swap $(ARG) | wc -l - -test3: $(NAME) - $(eval ARG = $(shell shuf -i 0-100 -n 3)) - ./push_swap $(ARG) | ./checker_linux $(ARG) - @echo -n "Instructions: " - @./push_swap $(ARG) | wc -l - -test5: $(NAME) - $(eval ARG = $(shell shuf -i 0-5000 -n 5)) - ./push_swap $(ARG) | ./checker_linux $(ARG) - @echo -n "Instructions: " - @./push_swap $(ARG) | wc -l - -test100: $(NAME) - $(eval ARG = $(shell shuf -i 0-5000 -n 100)) - ./push_swap $(ARG) | ./checker_linux $(ARG) - @echo -n "Instructions: " - @./push_swap $(ARG) | wc -l - -test500: $(NAME) - $(eval ARG = $(shell shuf -i 0-5000 -n 500)) - ./push_swap $(ARG) | ./checker_linux $(ARG) - @echo -n "Instructions: " - @./push_swap $(ARG) | wc -l - -test1000: $(NAME) - $(eval ARG = $(shell shuf -i 0-5000 -n 1000)) - ./push_swap $(ARG) | ./checker_linux $(ARG) - @echo -n "Instructions: " - @./push_swap $(ARG) | wc -l - - -.PHONY: all clean fclean re test2 test3 test5 test100 test500 test1000 \ No newline at end of file diff --git a/mia/README.md b/mia/README.md deleted file mode 100644 index 9e5a588..0000000 --- a/mia/README.md +++ /dev/null @@ -1,80 +0,0 @@ -# push_swap - -

- Push_swap 42 project badge -

- -push_swap is a 42 school algorithm project where we must sort a given list of random numbers with a limited set of instructions, using the lowest possible number of actions. - -## Status -Finished 05/09/2022. Grade: 100% - -## Usage - -Git clone the repository and cd into it. Then use ```make``` to compile. - -Then run it with : - -```shell -./push_swap -``` - -The numbers provided can be positive or negative integers. There must not be any duplicates. For example : - -```shell -./push_swap 9 0 -217 2147483647 -2147483648 -``` - -If the arguments are valid, the program will output the most efficient list of actions to sort the list. - -The program can be checked with the provided checker, like this : - -```shell -ARG="3 0 9 2 -1"; ./push_swap $ARG | ./checker $ARG -``` - -You can also simply ```make test3```, ```make test5```, ```make test100``` or ```make test500``` to quickly test the program with that number of values to sort. - -You can visualize this push_swap algorithm in action with [O-reo's push_swap visualizer](https://github.com/o-reo/push_swap_visualizer): - -![Push_swap visualization by mcombeau](https://github.com/mcombeau/push_swap/blob/main/subject/push_swap_visualization.gif) - -## Push_Swap Rules and Grading - -The program is only allowed two stacks to work with, stack A and stack B. All the numbers are initially added to stack A, and B is empty. - -The possible actions are: - -* ```pa``` (push A): Take the first element at the top of B and put it at the top of A. Do nothing if B is empty. -* ```pb``` (push B): Take the first element at the top of A and put it at the top of B. Do nothing if A is empty. -* ```sa``` (swap A): Swap the first 2 elements at the top of stack A. Do nothing if there is only one or no elements. -* ```sb``` (swap B): Swap the first 2 elements at the top of stack B. Do nothing if there is only one or no elements. -* ```ss```: ```sa``` and ```sb``` at the same time. -* ```ra``` (rotate A): Shift all elements of stack A up by 1. The first element becomes the last one. -* ```rb``` (rotate B): Shift all elements of stack B up by 1. The first element becomes the last one. -* ```rr```: ```ra``` and ```rb``` at the same time. -* ```rra``` (reverse rotate A): Shift all elements of stack A down by 1. The last element becomes the first one. -* ```rrb``` (reverse rotate B): Shift all elements of stack b down by 1. The last element becomes the first one. -* ```rrr``` : ```rra``` and ```rrb``` at the same time. - -The grade depends on how efficient the program's sorting process is. - -* Sorting 3 values: no more than 3 actions. -* Sorting 5 values: no more than 12 actions. -* Sorting 100 values: rating from 1 to 5 points depending on the number of actions: - * 5 points for less than 700 actions - * 4 points for less than 900 - * 3 points for less than 1100 - * 2 points for less than 1300 - * 1 point for less than 1500 -* Sorting 500 values: rating from 1 to 5 points depending on the number of actions: - * 5 points for less than 5500 actions - * 4 points for less than 7000 - * 3 points for less than 8500 - * 2 points for less than 10000 - * 1 point for less than 11500 - -Validating the project requires at least 80/100. - ---- -Made by mcombeau: mcombeau@student.42.fr | LinkedIn: [mcombeau](https://www.linkedin.com/in/mia-combeau-86653420b/) | Website: [codequoi.com](https://www.codequoi.com) diff --git a/mia/includes/push_swap.h b/mia/includes/push_swap.h deleted file mode 100644 index aa1b471..0000000 --- a/mia/includes/push_swap.h +++ /dev/null @@ -1,98 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* push_swap.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcombeau +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/04/25 13:47:42 by mcombeau #+# #+# */ -/* Updated: 2022/04/30 17:39:25 by mcombeau ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef PUSH_SWAP_H -# define PUSH_SWAP_H - -# include -# include -# include -# include -# include - -typedef struct s_stack -{ - int value; - int index; - int pos; - int target_pos; - int cost_a; - int cost_b; - struct s_stack *next; -} t_stack; - -/* Initialization */ - -t_stack *fill_stack_values(int ac, char **av); -void assign_index(t_stack *stack_a, int ac); - -/* Sorting Algorithms */ - -int is_sorted(t_stack *stack); -void tiny_sort(t_stack **stack); -void sort(t_stack **stack_a, t_stack **stack_b); - -/* Position */ - -int get_lowest_index_position(t_stack **stack); -void get_target_position(t_stack **stack_a, t_stack **stack_b); - -/* Cost */ - -void get_cost(t_stack **stack_a, t_stack **stack_b); -void do_cheapest_move(t_stack **stack_a, t_stack **stack_b); - -/* Calculate Move */ - -void do_move(t_stack **a, t_stack **b, int cost_a, int cost_b); - -/* Operations */ - -void do_pa(t_stack **stack_a, t_stack **stack_b); -void do_pb(t_stack **stack_a, t_stack **stack_b); -void do_sa(t_stack **stack_a); -void do_sb(t_stack **stack_b); -void do_ss(t_stack **stack_a, t_stack **stack_b); -void do_ra(t_stack **stack_a); -void do_rb(t_stack **stack_b); -void do_rr(t_stack **stack_a, t_stack **stack_b); -void do_rrr(t_stack **stack_a, t_stack **stack_b); -void do_rra(t_stack **stack_a); -void do_rrb(t_stack **stack_b); - -/* Stack Functions */ - -t_stack *get_stack_bottom(t_stack *stack); -t_stack *get_stack_before_bottom(t_stack *stack); -t_stack *stack_new(int value); -void stack_add_bottom(t_stack **stack, t_stack *new); -int get_stack_size(t_stack *stack); - -/* Utils */ - -void free_stack(t_stack **stack); -long int ft_atoi(const char *str); -void ft_putstr(char *str); -int nb_abs(int nb); - -/* Error */ - -void exit_error(t_stack **stack_a, t_stack **stack_b); - -/* Input Check */ - -int is_correct_input(char **av); -int is_digit(char c); -int is_sign(char c); -int nbstr_cmp(const char *s1, const char *s2); - -#endif \ No newline at end of file diff --git a/mia/src/cost.c b/mia/src/cost.c deleted file mode 100644 index e339500..0000000 --- a/mia/src/cost.c +++ /dev/null @@ -1,71 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* cost.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcombeau +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/04/26 16:09:18 by mcombeau #+# #+# */ -/* Updated: 2022/04/30 17:48:37 by mcombeau ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -/* get_cost: -* Calculates the cost of moving each element of stack B into the correct -* position in stack A. -* Two costs are calculated: -* cost_b represents the cost of getting the element to the top of the B stack -* cost_a represents the cost of getting to the right position in stack A. -* If the element is in the bottom half of the stack, the cost will be negative, -* if it is in the top half, the cost is positive. -*/ -void get_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_pos; - if (tmp_b->target_pos > size_a / 2) - tmp_b->cost_a = (size_a - tmp_b->target_pos) * -1; - tmp_b = tmp_b->next; - } -} - -/* do_cheapest_move: -* Finds the element in stack B with the cheapest cost to move to stack A -* and moves it to the correct position in stack A. -*/ -void do_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 (nb_abs(tmp->cost_a) + nb_abs(tmp->cost_b) < nb_abs(cheapest)) - { - cheapest = nb_abs(tmp->cost_b) + nb_abs(tmp->cost_a); - cost_a = tmp->cost_a; - cost_b = tmp->cost_b; - } - tmp = tmp->next; - } - do_move(stack_a, stack_b, cost_a, cost_b); -} diff --git a/mia/src/do_move.c b/mia/src/do_move.c deleted file mode 100644 index 5b8f5c4..0000000 --- a/mia/src/do_move.c +++ /dev/null @@ -1,111 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* do_move.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcombeau +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/04/26 16:26:29 by mcombeau #+# #+# */ -/* Updated: 2022/04/30 18:19:44 by mcombeau ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -/* do_rev_rotate_both: -* Reverse rotates both stack A and B until one of them is in position. -* The given cost is negative since both positions are in the bottom half -* of their respective stacks. The cost is increased as the stacks are -* rotated, when one reaches 0, the stack has been rotated as far as possible -* and the top position is correct. -*/ -static void do_rev_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)++; - do_rrr(a, b); - } -} - -/* do_rotate_both: -* Rotates both stack A and B until one of them is in position. -* The given cost is positive since both positions are in the top half -* of their respective stacks. The cost is decreased as the stacks are -* rotated, when one reaches 0, the stack has been rotated as far as possible -* and the top position is correct. -*/ -static void do_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)--; - do_rr(a, b); - } -} - -/* do_rotate_a: -* Rotates stack A until it is in position. If the cost is negative, -* the stack will be reverse rotated, if it is positive, it will be -* rotated. -*/ -static void do_rotate_a(t_stack **a, int *cost) -{ - while (*cost) - { - if (*cost > 0) - { - do_ra(a); - (*cost)--; - } - else if (*cost < 0) - { - do_rra(a); - (*cost)++; - } - } -} - -/* do_rotate_b: -* Rotates stack B until it is in position. If the cost is negative, -* the stack will be reverse rotated, if it is positive, it will be -* rotated. -*/ -static void do_rotate_b(t_stack **b, int *cost) -{ - while (*cost) - { - if (*cost > 0) - { - do_rb(b); - (*cost)--; - } - else if (*cost < 0) - { - do_rrb(b); - (*cost)++; - } - } -} - -/* do_move: -* Chooses which move to make to get the B stack element to the correct -* position in stack A. -* If the costs of moving stack A and B into position match (i.e. both negative -* of both positive), both will be rotated or reverse rotated at the same time. -* They might also be rotated separately, before finally pushing the top B element -* to the top stack A. -*/ -void do_move(t_stack **a, t_stack **b, int cost_a, int cost_b) -{ - if (cost_a < 0 && cost_b < 0) - do_rev_rotate_both(a, b, &cost_a, &cost_b); - else if (cost_a > 0 && cost_b > 0) - do_rotate_both(a, b, &cost_a, &cost_b); - do_rotate_a(a, &cost_a); - do_rotate_b(b, &cost_b); - do_pa(a, b); -} diff --git a/mia/src/initialization.c b/mia/src/initialization.c deleted file mode 100644 index 63846c6..0000000 --- a/mia/src/initialization.c +++ /dev/null @@ -1,77 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* initialization.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcombeau +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/04/25 14:44:42 by mcombeau #+# #+# */ -/* Updated: 2022/04/30 13:55:45 by mcombeau ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -/* fill_stack_values: -* Fills stack_a with the provided values. -* If the values are out of integer range, prints and error and exits the program. -*/ -t_stack *fill_stack_values(int ac, char **av) -{ - t_stack *stack_a; - long int nb; - int i; - - stack_a = NULL; - nb = 0; - i = 1; - while (i < ac) - { - nb = ft_atoi(av[i]); - if (nb > INT_MAX || nb < INT_MIN) - exit_error(&stack_a, NULL); - if (i == 1) - stack_a = stack_new((int)nb); - else - stack_add_bottom(&stack_a, stack_new((int)nb)); - i++; - } - return (stack_a); -} - -/* assign_index: -* Assigns an index to each value in stack a. This is a convenient way to order -* the stack because indexes can be checked and compared instead of actual values, -* which may or may not be adjacent to each other. -* ex. values: -3 0 9 2 -* indexes: [1] [2] [4] [3] -* The indexes are assigned from highest (stack_size) to lowest (1). -*/ -void assign_index(t_stack *stack_a, int stack_size) -{ - t_stack *ptr; - t_stack *highest; - int value; - - while (--stack_size > 0) - { - ptr = stack_a; - value = INT_MIN; - highest = NULL; - while (ptr) - { - if (ptr->value == INT_MIN && ptr->index == 0) - ptr->index = 1; - if (ptr->value > value && ptr->index == 0) - { - value = ptr->value; - highest = ptr; - ptr = stack_a; - } - else - ptr = ptr->next; - } - if (highest != NULL) - highest->index = stack_size; - } -} diff --git a/mia/src/input_check.c b/mia/src/input_check.c deleted file mode 100644 index 5abd925..0000000 --- a/mia/src/input_check.c +++ /dev/null @@ -1,100 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* input_check.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcombeau +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/04/25 13:27:24 by mcombeau #+# #+# */ -/* Updated: 2022/05/13 15:57:10 by mcombeau ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -/* arg_is_number: -* Checks if the argument is a number. +1 1 and -1 are all valid numbers. -* Return: 1 if the argument is a number, 0 if not. -*/ -static int arg_is_number(char *av) -{ - int i; - - i = 0; - if (is_sign(av[i]) && av[i + 1] != '\0') - i++; - while (av[i] && is_digit(av[i])) - i++; - if (av[i] != '\0' && !is_digit(av[i])) - return (0); - return (1); -} - -/* have_duplicates: -* Checks if the argument list has duplicate numbers. -* Return: 1 if a duplicate is found, 0 if there are none. -*/ -static int have_duplicates(char **av) -{ - int i; - int j; - - i = 1; - while (av[i]) - { - j = 1; - while (av[j]) - { - if (j != i && nbstr_cmp(av[i], av[j]) == 0) - return (1); - j++; - } - i++; - } - return (0); -} - -/* arg_is_zero: -* Checks the argument is a 0 to avoid 0 +0 -0 duplicates -* and 0 0000 +000 -00000000 too. -* Return: 1 if the argument is a zero, 0 if it contains -* anything else than a zero. -*/ -static int arg_is_zero(char *av) -{ - int i; - - i = 0; - if (is_sign(av[i])) - i++; - while (av[i] && av[i] == '0') - i++; - if (av[i] != '\0') - return (0); - return (1); -} - -/* is_correct_input: -* Checks if the given arguments are all numbers, without duplicates. -* Return: 1 if the arguments are valid, 0 if not. -*/ -int is_correct_input(char **av) -{ - int i; - int nb_zeros; - - nb_zeros = 0; - i = 1; - while (av[i]) - { - if (!arg_is_number(av[i])) - return (0); - nb_zeros += arg_is_zero(av[i]); - i++; - } - if (nb_zeros > 1) - return (0); - if (have_duplicates(av)) - return (0); - return (1); -} diff --git a/mia/src/input_check_utils.c b/mia/src/input_check_utils.c deleted file mode 100644 index b0424f3..0000000 --- a/mia/src/input_check_utils.c +++ /dev/null @@ -1,63 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* input_check_utils.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcombeau +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/04/25 13:25:28 by mcombeau #+# #+# */ -/* Updated: 2022/04/25 17:15:03 by mcombeau ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -/* is_digit: -* Checks if the character is a digit. -* Return: 1 if the character is a digit, 0 if not. -*/ -int is_digit(char c) -{ - return (c >= '0' && c <= '9'); -} - -/* is_sign: -* Checks if the character is number sign, + or -. -* Return: 1 if the character is a sign, 0 if not. -*/ -int is_sign(char c) -{ - return (c == '+' || c == '-'); -} - -/* nbstr_cmp: -* Compares two strings of digits to check if they are the same or not. -* If one of the strings starts with a '+' sign, it is ignored and the rest -* of the number string is checked. -* This is so that +123 == 123 but -123 != 123. -* Return: 0 if the strings match, another value if they do not. -*/ -int nbstr_cmp(const char *s1, const char *s2) -{ - int i; - int j; - - i = 0; - j = i; - if (s1[i] == '+') - { - if (s2[j] != '+') - i++; - } - else - { - if (s2[j] == '+') - j++; - } - while (s1[i] != '\0' && s2[j] != '\0' && s1[i] == s2[j]) - { - i++; - j++; - } - return ((unsigned char)s1[i] - (unsigned char)s2[j]); -} diff --git a/mia/src/main.c b/mia/src/main.c deleted file mode 100644 index 362ac12..0000000 --- a/mia/src/main.c +++ /dev/null @@ -1,67 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* main.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcombeau +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/04/25 14:04:00 by mcombeau #+# #+# */ -/* Updated: 2022/04/30 15:11:18 by mcombeau ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -/* is_sorted: -* Checks if a stack is sorted. -* Returns 0 if the stack is not sorted, 1 if it is sorted. -*/ -int is_sorted(t_stack *stack) -{ - while (stack->next != NULL) - { - if (stack->value > stack->next->value) - return (0); - stack = stack->next; - } - return (1); -} - -/* push_swap: -* Chooses a sorting method depending on the number -* of values to be sorted. -*/ -static void push_swap(t_stack **stack_a, t_stack **stack_b, int stack_size) -{ - if (stack_size == 2 && !is_sorted(*stack_a)) - do_sa(stack_a); - else if (stack_size == 3) - tiny_sort(stack_a); - else if (stack_size > 3 && !is_sorted(*stack_a)) - sort(stack_a, stack_b); -} - -/* main: -* Checks if the input is correct, in which case it initializes stacks a and b, -* assigns each value indexes and sorts the stacks. When sorting is done, frees -* the stacks and exits. -*/ -int main(int ac, char **av) -{ - t_stack *stack_a; - t_stack *stack_b; - int stack_size; - - if (ac < 2) - return (0); - if (!is_correct_input(av)) - exit_error(NULL, NULL); - stack_b = NULL; - stack_a = fill_stack_values(ac, av); - stack_size = get_stack_size(stack_a); - assign_index(stack_a, stack_size + 1); - push_swap(&stack_a, &stack_b, stack_size); - free_stack(&stack_a); - free_stack(&stack_b); - return (0); -} diff --git a/mia/src/position.c b/mia/src/position.c deleted file mode 100644 index 0ca5953..0000000 --- a/mia/src/position.c +++ /dev/null @@ -1,150 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* position.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcombeau +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/04/26 15:26:20 by mcombeau #+# #+# */ -/* Updated: 2022/04/30 17:26:18 by mcombeau ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -/* get_position: -* Assigns a position to each element of a stack from top to bottom -* in ascending order. -* Example stack: -* value: 3 0 9 1 -* index: [3] [1] [4] [2] -* position: <0> <1> <2> <3> -* This is used to calculate the cost of moving a certain number to -* a certain position. If the above example is stack b, it would cost -* nothing (0) to push the first element to stack a. However if we want to -* push the highest value, 9, which is in third position, it would cost 2 extra -* moves to bring that 9 to the top of b before pushing it to stack a. -*/ -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++; - } -} - -/* get_lowest_index_position: -* Gets the current position of the element with the lowest index -* within a stack. -*/ -int get_lowest_index_position(t_stack **stack) -{ - t_stack *tmp; - int lowest_index; - int lowest_pos; - - tmp = *stack; - lowest_index = INT_MAX; - get_position(stack); - lowest_pos = tmp->pos; - while (tmp) - { - if (tmp->index < lowest_index) - { - lowest_index = tmp->index; - lowest_pos = tmp->pos; - } - tmp = tmp->next; - } - return (lowest_pos); -} - -/* get_target: -* Picks the best target position in stack A for the given index of -* an element in stack B. First checks if the index of the B element can -* be placed somewhere in between elements in stack A, by checking whether -* there is an element in stack A with a bigger index. If not, it finds the -* element with the smallest index in A and assigns that as the target position. -* --- Example: -* target_pos starts at INT_MAX -* B index: 3 -* A contains indexes: 9 4 2 1 0 -* 9 > 3 && 9 < INT_MAX : target_pos updated to 9 -* 4 > 3 && 4 < 9 : target pos updated to 4 -* 2 < 3 && 2 < 4 : no update! -* So target_pos needs to be the position of index 4, since it is -* the closest index. -* --- Example: -* target_pos starts at INT_MAX -* B index: 20 -* A contains indexes: 16 4 3 -* 16 < 20 : no update! target_pos = INT_MAX -* 4 < 20 : no update! target_pos = INT_MAX -* 3 < 20 : no update! target_pos = INT_MAX -* ... target_pos stays at INT MAX, need to switch strategies. -* 16 < 20 : target_pos updated to 20 -* 4 < 20 : target_pos updated to 4 -* 3 < 20 : target_pos updated to 3 -* So target_pos needs to be the position of index 3, since that is -* the "end" of the stack. -*/ -static int get_target(t_stack **a, int b_idx, - int target_idx, int target_pos) -{ - t_stack *tmp_a; - - tmp_a = *a; - while (tmp_a) - { - if (tmp_a->index > b_idx && tmp_a->index < target_idx) - { - target_idx = tmp_a->index; - target_pos = tmp_a->pos; - } - tmp_a = tmp_a->next; - } - if (target_idx != INT_MAX) - return (target_pos); - tmp_a = *a; - while (tmp_a) - { - if (tmp_a->index < target_idx) - { - target_idx = tmp_a->index; - target_pos = tmp_a->pos; - } - tmp_a = tmp_a->next; - } - return (target_pos); -} - -/* get_target_position: -* Assigns a target position in stack A to each element of stack A. -* The target position is the spot the element in B needs to -* get to in order to be sorted correctly. This position will -* be used to calculate the cost of moving each element to -* its target position in stack A. -*/ -void get_target_position(t_stack **a, t_stack **b) -{ - t_stack *tmp_b; - int target_pos; - - tmp_b = *b; - get_position(a); - get_position(b); - target_pos = 0; - while (tmp_b) - { - target_pos = get_target(a, tmp_b->index, INT_MAX, target_pos); - tmp_b->target_pos = target_pos; - tmp_b = tmp_b->next; - } -} diff --git a/mia/src/push.c b/mia/src/push.c deleted file mode 100644 index f872534..0000000 --- a/mia/src/push.c +++ /dev/null @@ -1,48 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* push.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcombeau +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/04/25 17:05:01 by mcombeau #+# #+# */ -/* Updated: 2022/04/30 13:22:28 by mcombeau ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -/* push: -* Pushes the top element of src stack to the top of dest stack. -*/ -static void push(t_stack **src, t_stack **dest) -{ - t_stack *tmp; - - if (*src == NULL) - return ; - tmp = (*src)->next; - (*src)->next = *dest; - *dest = *src; - *src = tmp; -} - -/* do_pa: -* Pushes the top element of stack b to the top of stack a. -* Prints "pa" to the standard output. -*/ -void do_pa(t_stack **stack_a, t_stack **stack_b) -{ - push(stack_b, stack_a); - ft_putstr("pa\n"); -} - -/* do_pb: -* Pushes the top element of stack a to the top of stack b. -* Prints "pb" to the standard output. -*/ -void do_pb(t_stack **stack_a, t_stack **stack_b) -{ - push(stack_a, stack_b); - ft_putstr("pb\n"); -} diff --git a/mia/src/reverse_rotate.c b/mia/src/reverse_rotate.c deleted file mode 100644 index f069ff1..0000000 --- a/mia/src/reverse_rotate.c +++ /dev/null @@ -1,62 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* reverse_rotate.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcombeau +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/04/25 17:26:52 by mcombeau #+# #+# */ -/* Updated: 2022/04/30 13:29:05 by mcombeau ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -/* rev_rotate: -* Brings the bottom element of a stack to the top. -*/ -static void rev_rotate(t_stack **stack) -{ - t_stack *tmp; - t_stack *tail; - t_stack *before_tail; - - tail = get_stack_bottom(*stack); - before_tail = get_stack_before_bottom(*stack); - tmp = *stack; - *stack = tail; - (*stack)->next = tmp; - before_tail->next = NULL; -} - -/* do_rra: -* Brings the bottom element of stack a to the top. -* Prints "rra" to the standard output. -*/ -void do_rra(t_stack **stack_a) -{ - rev_rotate(stack_a); - ft_putstr("rra\n"); -} - -/* do_rrb: -* Brings the bottom element of stack b to the top. -* Prints "rrb" to the standard output. -*/ -void do_rrb(t_stack **stack_b) -{ - rev_rotate(stack_b); - ft_putstr("rrb\n"); -} - -/* do_rrr: -* Brings the bottom element of both stack a and stack be -* to the top of their respective stacks. -* Prints "rrr" to the standard output. -*/ -void do_rrr(t_stack **stack_a, t_stack **stack_b) -{ - rev_rotate(stack_a); - rev_rotate(stack_b); - ft_putstr("rrr\n"); -} diff --git a/mia/src/rotate.c b/mia/src/rotate.c deleted file mode 100644 index 237d6fe..0000000 --- a/mia/src/rotate.c +++ /dev/null @@ -1,60 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* rotate.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcombeau +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/04/25 17:18:17 by mcombeau #+# #+# */ -/* Updated: 2022/09/28 11:13:19 by mcombeau ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -/* rotate: -* The top element of the stack is sent to the bottom. -*/ -static void rotate(t_stack **stack) -{ - t_stack *tmp; - t_stack *tail; - - tmp = *stack; - *stack = (*stack)->next; - tail = get_stack_bottom(*stack); - tmp->next = NULL; - tail->next = tmp; -} - -/* do_ra: -* Sends the top element of stack a to the bottom. -* Prints "ra" to the standard output. -*/ -void do_ra(t_stack **stack_a) -{ - rotate(stack_a); - ft_putstr("ra\n"); -} - -/* do_rb: -* Sends the top element of stack b to the bottom. -* Prints "rb" to the standard output. -*/ -void do_rb(t_stack **stack_b) -{ - rotate(stack_b); - ft_putstr("rb\n"); -} - -/* do_rr: -* Sends the top element of both stack a and stack b to the bottom -* of their respective stacks. -* Prints "rr" to the standard output. -*/ -void do_rr(t_stack **stack_a, t_stack **stack_b) -{ - rotate(stack_a); - rotate(stack_b); - ft_putstr("rr\n"); -} diff --git a/mia/src/sort.c b/mia/src/sort.c deleted file mode 100644 index 42870b6..0000000 --- a/mia/src/sort.c +++ /dev/null @@ -1,97 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* sort.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcombeau +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/04/26 13:32:10 by mcombeau #+# #+# */ -/* Updated: 2022/04/30 17:39:26 by mcombeau ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -/* push_all_save_three: -* Pushes all the elements of stack a into stack b, except the three last ones. -* Pushes the smaller values first, and then the larger values to help with -* sorting efficiency. -*/ -static void push_all_save_three(t_stack **stack_a, t_stack **stack_b) -{ - int stack_size; - int pushed; - int i; - - stack_size = get_stack_size(*stack_a); - pushed = 0; - i = 0; - while (stack_size > 6 && i < stack_size && pushed < stack_size / 2) - { - if ((*stack_a)->index <= stack_size / 2) - { - do_pb(stack_a, stack_b); - pushed++; - } - else - do_ra(stack_a); - i++; - } - while (stack_size - pushed > 3) - { - do_pb(stack_a, stack_b); - pushed++; - } -} - -/* shift_stack: -* After the bulk of the stack is sorted, shifts stack a until the lowest -* value is at the top. If it is in the bottom half of the stack, reverse -* rotate it into position, otherwise rotate until it is at the top of the -* stack. -*/ -static void shift_stack(t_stack **stack_a) -{ - int lowest_pos; - int stack_size; - - stack_size = get_stack_size(*stack_a); - lowest_pos = get_lowest_index_position(stack_a); - if (lowest_pos > stack_size / 2) - { - while (lowest_pos < stack_size) - { - do_rra(stack_a); - lowest_pos++; - } - } - else - { - while (lowest_pos > 0) - { - do_ra(stack_a); - lowest_pos--; - } - } -} - -/* sort: -* Sorting algorithm for a stack larger than 3. -* Push everything but 3 numbers to stack B. -* Sort the 3 numbers left in stack A. -* Calculate the most cost-effective move. -* Shift elements until stack A is in order. -*/ -void sort(t_stack **stack_a, t_stack **stack_b) -{ - push_all_save_three(stack_a, stack_b); - tiny_sort(stack_a); - while (*stack_b) - { - get_target_position(stack_a, stack_b); - get_cost(stack_a, stack_b); - do_cheapest_move(stack_a, stack_b); - } - if (!is_sorted(*stack_a)) - shift_stack(stack_a); -} diff --git a/mia/src/sort_tiny.c b/mia/src/sort_tiny.c deleted file mode 100644 index eb93174..0000000 --- a/mia/src/sort_tiny.c +++ /dev/null @@ -1,58 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* sort_tiny.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcombeau +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/04/25 17:41:01 by mcombeau #+# #+# */ -/* Updated: 2022/04/30 14:03:55 by mcombeau ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -/* find_highest_index: -* Returns the highest index in a stack. -*/ -static int find_highest_index(t_stack *stack) -{ - int index; - - index = stack->index; - while (stack) - { - if (stack->index > index) - index = stack->index; - stack = stack->next; - } - return (index); -} - -/* tiny_sort: -* Sorts a stack of 3 numbers in 2 or fewer moves. The sorting is done by index -* rather than value. Example: -* values: 0 9 2 -* indexes: [1] [3] [2] -* Solution, 2 moves: -* rra: -* values: 2 0 9 -* indexes: [2] [1] [3] -* sa: -* values: 0 2 9 -* indexes: [1] [2] [3] -*/ -void tiny_sort(t_stack **stack) -{ - int highest; - - if (is_sorted(*stack)) - return ; - highest = find_highest_index(*stack); - if ((*stack)->index == highest) - do_ra(stack); - else if ((*stack)->next->index == highest) - do_rra(stack); - if ((*stack)->index > (*stack)->next->index) - do_sa(stack); -} diff --git a/mia/src/stack.c b/mia/src/stack.c deleted file mode 100644 index 0350265..0000000 --- a/mia/src/stack.c +++ /dev/null @@ -1,90 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* stack.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcombeau +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/04/25 14:26:12 by mcombeau #+# #+# */ -/* Updated: 2022/04/30 13:30:26 by mcombeau ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -/* get_stack_bottom: -* Returns the last element of the stack. -*/ -t_stack *get_stack_bottom(t_stack *stack) -{ - while (stack && stack->next != NULL) - stack = stack->next; - return (stack); -} - -/* get_stack_before_bottom: -* Returns the second to last element of the stack. -*/ -t_stack *get_stack_before_bottom(t_stack *stack) -{ - while (stack && stack->next && stack->next->next != NULL) - stack = stack->next; - return (stack); -} - -/* stack_new: -* Creates a stack elements with the provided value. -* Returns the newly created stack element. -*/ -t_stack *stack_new(int value) -{ - t_stack *new; - - new = malloc(sizeof * new); - if (!new) - return (NULL); - new->value = value; - new->index = 0; - new->pos = -1; - new->target_pos = -1; - new->cost_a = -1; - new->cost_b = -1; - new->next = NULL; - return (new); -} - -/* add_stack_bottom: -* Adds an element to the bottom of a stack. -*/ -void stack_add_bottom(t_stack **stack, t_stack *new) -{ - t_stack *tail; - - if (!new) - return ; - if (!*stack) - { - *stack = new; - return ; - } - tail = get_stack_bottom(*stack); - tail->next = new; -} - -/* get_stack_size: -* Returns the number of elements in a stack. -*/ -int get_stack_size(t_stack *stack) -{ - int size; - - size = 0; - if (!stack) - return (0); - while (stack) - { - stack = stack->next; - size++; - } - return (size); -} diff --git a/mia/src/swap.c b/mia/src/swap.c deleted file mode 100644 index 97c2239..0000000 --- a/mia/src/swap.c +++ /dev/null @@ -1,63 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* swap.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcombeau +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/04/25 16:55:55 by mcombeau #+# #+# */ -/* Updated: 2022/04/30 13:25:21 by mcombeau ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -/* swap: -* Swaps the top 2 elements of a stack. -* Does nothing if there is only one or no elements. -*/ -static void swap(t_stack *stack) -{ - int tmp; - - if (stack == NULL || stack->next == NULL) - return ; - tmp = stack->value; - stack->value = stack->next->value; - stack->next->value = tmp; - tmp = stack->index; - stack->index = stack->next->index; - stack->next->index = tmp; -} - -/* do_sa: -* Swaps the top 2 elements of stack a. -* Prints "sa" to the standard output. -*/ -void do_sa(t_stack **stack_a) -{ - swap(*stack_a); - ft_putstr("sa\n"); -} - -/* do_sb: -* Swaps the top 2 elements of stack b. -* Prints "sb" to the standard output. -*/ -void do_sb(t_stack **stack_b) -{ - swap(*stack_b); - ft_putstr("sb\n"); -} - -/* do_ss: -* Swaps the top 2 elements of stack a and the top 2 elements -* of stack b. -* Prints "ss" to the standard output. -*/ -void do_ss(t_stack **stack_a, t_stack **stack_b) -{ - swap(*stack_a); - swap(*stack_b); - ft_putstr("ss\n"); -} diff --git a/mia/src/utils.c b/mia/src/utils.c deleted file mode 100644 index 01014d8..0000000 --- a/mia/src/utils.c +++ /dev/null @@ -1,99 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* utils.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mcombeau +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/04/25 13:40:24 by mcombeau #+# #+# */ -/* Updated: 2022/06/13 19:19:03 by mcombeau ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -/* free_stack: -* Frees each element in a given stack and sets the stack pointer to NULL. -*/ -void free_stack(t_stack **stack) -{ - t_stack *tmp; - - if (!stack || !(*stack)) - return ; - while (*stack) - { - tmp = (*stack)->next; - free(*stack); - *stack = tmp; - } - *stack = NULL; -} - -/* exit_error: -* Writes "Error\n" to the standard output after freeing stack a and b. -* Exits with standard error code 1. -*/ -void exit_error(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); -} - -/* ft_atoi: -* Converts an alphanumeric string of characters into a long integer. -*/ -long int ft_atoi(const char *str) -{ - long int nb; - int isneg; - int i; - - nb = 0; - isneg = 1; - i = 0; - if (str[i] == '+') - i++; - else if (str[i] == '-') - { - isneg *= -1; - i++; - } - while (is_digit(str[i])) - { - nb = (nb * 10) + (str[i] - '0'); - i++; - } - return (nb * isneg); -} - -/* ft_putstr: -* Prints a given string of characters to the standard output. -*/ -void ft_putstr(char *str) -{ - int i; - - i = 0; - while (str[i]) - { - write(1, &str[i], 1); - i++; - } -} - -/* nb_abs: -* Returns the absolute value of a given number. -* The absolute value of a number is used to measure the distance of that -* number from 0, whether it is positive or negative (abs value of -6 is 6). -*/ -int nb_abs(int nb) -{ - if (nb < 0) - return (nb * -1); - return (nb); -}