implemented LIS optimalisation and split all the function into their own file

This commit is contained in:
Willem Haffmans 2024-11-24 15:19:43 +00:00
parent 33ac159055
commit 5f6bdaec71
34 changed files with 784 additions and 242 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ obj/
Session.vim Session.vim
test.txt test.txt
checker_linux checker_linux
.vscode/

View File

@ -20,8 +20,15 @@ OBJ_PATH = obj
VPATH = src:src/util:src/moves:src/sort VPATH = src:src/util:src/moves:src/sort
SOURCES = push_swap.c ft_lstat.c new_element.c push.c rotate.c reverse_rotate.c \ SOURCES = push_all_but_3_b.c push_all_but_lis.c push.c put_at_index_asc.c \
swap.c sort3.c stack_min.c utils.c sortmore.c rotate_utils.c reverse_rotate.c rotate_a.c rotate_a_to.c rotate_a_to_top.c \
rotate_b.c rotate.c shortest_rotate.c swap.c push_swap.c \
best_merge_a.c b_merge_a.c common_rotations.c count_moves_merge_a.c \
sort3.c sortmore.c count_gt.c error.c free_state.c ft_abs.c \
ft_lstat.c ft_max.c has_duplicates.c index_of.c initialise_state.c \
is_sorted.c lis_indices.c lis_lengths.c \
longest_incremental_subsequence.c new_element.c print_stack.c \
print_state.c stack_min.c
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */ /* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/04 13:58:35 by whaffman #+#+# #+#+# #+# #+# | */ /* Created: 2024/11/04 13:58:35 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/10 16:24:06 by whaffman ### ### ### ### / \ */ /* Updated: 2024/11/24 15:18:08 by whaffman ### ### ### ### / \ */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,6 +20,19 @@ typedef struct s_state
t_list *a; t_list *a;
t_list *b; t_list *b;
} t_state; } t_state;
t_state *initialise_state(t_state **state);
int *lis_lengths(t_list *stack, int size);
int *lis_indices(int *lengths, int size, int max_len);
void b_merge_a(int idx_b, t_state *state);
int best_merge_a(t_state *state);
int count_moves_merge_a(int idx_b, t_state *state);
int error(t_state *state);
void free_state(t_state *state);
int is_sorted(t_list *stack);
int has_duplicates(t_list *stack);
int *longest_incremental_subsequence(t_list *stack, int *lis_size);
void push_all_but_lis(t_state *state);
t_list *ft_lstat(t_list *list, int n); t_list *ft_lstat(t_list *list, int n);
int ft_abs(int a); int ft_abs(int a);
int ft_max(int a, int b); int ft_max(int a, int b);

34
src/checker.c Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* checker.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:05:06 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:05:43 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int main(int argc, char *argv[])
{
t_state *state;
state = NULL;
if (!initialise_state(&state))
return (1);
while (argc-- > 1)
{
if (!new_element(&(state->a), argv[argc]))
return (error(state));
if (has_duplicates(state->a))
return (error(state));
}
if (ft_lstsize(state->a) <= 1 || is_sorted(state->a))
return (0);
sortmore(state);
free_state(state);
return (0);
}

View File

@ -13,25 +13,6 @@
#include "libft.h" #include "libft.h"
#include "push_swap.h" #include "push_swap.h"
int put_at_index_asc(int number, t_list *stack)
{
const int i = index_of(stack_min(stack), stack);
const int size = ft_lstsize(stack);
const int gt = count_gt(number, stack);
return ((i + gt) % size);
}
void push_all_but_3_b(t_state *state)
{
const int size = ft_lstsize(state->a);
int i;
i = 0;
while (i++ < size - 3)
pb(state);
}
void pb(t_state *state) void pb(t_state *state)
{ {
push(&(state->a), &(state->b)); push(&(state->a), &(state->b));

View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* push_all_but_3_b.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:15:47 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:15:49 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "libft.h"
void push_all_but_3_b(t_state *state)
{
const int size = ft_lstsize(state->a);
int i;
i = 0;
while (i++ < size - 3)
pb(state);
}

View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* push_all_but_lis.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:15:53 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:16:11 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void push_all_but_lis(t_state *state)
{
const int size = ft_lstsize(state->a);
int lis_size;
int i;
int j;
int *lis_indices;
i = 0;
j = 0;
lis_indices = longest_incremental_subsequence(state->a, &lis_size);
while (i < size)
{
if (j < lis_size && i == lis_indices[j])
{
j++;
ra(state);
}
else
pb(state);
i++;
}
}

View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* put_at_index_asc.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:16:18 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:16:19 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "libft.h"
int put_at_index_asc(int number, t_list *stack)
{
const int i = index_of(stack_min(stack), stack);
const int size = ft_lstsize(stack);
const int gt = count_gt(number, stack);
return ((i + gt) % size);
}

27
src/moves/rotate_a.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* rotate_a.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:16:27 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:16:28 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void rotate_a(int n, t_state *state)
{
while (n > 0)
{
ra(state);
n--;
}
while (n < 0)
{
rra(state);
n++;
}
}

View File

@ -1,60 +1,17 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: o_ :::::: ::: */ /* ::: o_ :::::: ::: */
/* rotate_utils.c :+: / :+::+: :+: */ /* rotate_a_to.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */ /* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */ /* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/19 15:03:32 by whaffman #+#+# #+#+# #+# #+# | */ /* Created: 2024/11/24 15:16:34 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/19 15:03:35 by whaffman ### ### ### ### / \ */ /* Updated: 2024/11/24 15:16:37 by whaffman ### ### ### ### / \ */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h"
#include "push_swap.h" #include "push_swap.h"
int rotate_a_to_top(t_state *state)
{
rotate_a_to(index_of(stack_min(state->a), state->a), state);
return (1);
}
int shortest_rotate(int index, int size)
{
if (index <= size / 2)
return (index);
return (-1 * (size - index));
}
//TODO
void rotate_a(int n, t_state *state)
{
while (n > 0)
{
ra(state);
n--;
}
while (n < 0)
{
rra(state);
n++;
}
}
void rotate_b(int n, t_state *state)
{
while (n > 0)
{
rb(state);
n--;
}
while (n < 0)
{
rrb(state);
n++;
}
}
void rotate_a_to(int index, t_state *state) void rotate_a_to(int index, t_state *state)
{ {
int n; int n;

View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* rotate_a_to_top.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:16:43 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:16:44 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int rotate_a_to_top(t_state *state)
{
rotate_a_to(index_of(stack_min(state->a), state->a), state);
return (1);
}

27
src/moves/rotate_b.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* rotate_b.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:16:49 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:16:51 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void rotate_b(int n, t_state *state)
{
while (n > 0)
{
rb(state);
n--;
}
while (n < 0)
{
rrb(state);
n++;
}
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* shortest_rotate.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:16:56 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:16:57 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
int shortest_rotate(int index, int size)
{
if (index <= size / 2)
return (index);
return (-1 * (size - index));
}

View File

@ -6,54 +6,12 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */ /* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/04 13:04:11 by whaffman #+#+# #+#+# #+# #+# | */ /* Created: 2024/11/04 13:04:11 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/07 15:26:58 by whaffman ### ### ### ### / \ */ /* Updated: 2024/11/24 15:06:16 by whaffman ### ### ### ### / \ */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h" #include "libft.h"
#include "push_swap.h" #include "push_swap.h"
#include <stdio.h>
#include <stdlib.h>
void print_stack(const char *name, t_list *stack)
{
int lst_size;
t_list *node;
ft_printf("%s |", name);
if (!stack)
{
ft_printf("\n");
return ;
}
lst_size = ft_lstsize(stack);
while (lst_size > 0)
{
node = ft_lstat(stack, lst_size - 1);
ft_printf("%d ", *((int *) node->content));
lst_size--;
}
ft_printf("\n");
}
void print_state(t_state *state)
{
ft_printf("\n");
print_stack("A", state->a);
print_stack("B", state->b);
ft_printf("===================\n");
}
t_state *initialise_state(t_state **state)
{
*state = malloc(sizeof (t_state));
if (*state)
{
(*state)->a = NULL;
(*state)->b = NULL;
}
return (*state);
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -65,11 +23,13 @@ int main(int argc, char *argv[])
while (argc-- > 1) while (argc-- > 1)
{ {
if (!new_element(&(state->a), argv[argc])) if (!new_element(&(state->a), argv[argc]))
{ return (error(state));
ft_putstr_fd("Error\n", 2); if (has_duplicates(state->a))
return (1); return (error(state));
}
} }
if (ft_lstsize(state->a) <= 1 || is_sorted(state->a))
return (0);
sortmore(state); sortmore(state);
free_state(state);
return (0); return (0);
} }

28
src/sort/b_merge_a.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* b_merge_a.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:15:10 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:15:12 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void b_merge_a(int idx_b, t_state *state)
{
const int number_b = *(int *) ft_lstat(state->b, idx_b)->content;
const int idx_a = put_at_index_asc(number_b, state->a);
int shortest_a;
int shortest_b;
shortest_a = shortest_rotate(idx_a, ft_lstsize(state->a));
shortest_b = shortest_rotate(idx_b, ft_lstsize(state->b));
common_rotations(&shortest_a, &shortest_b, state);
rotate_a(shortest_a, state);
rotate_b(shortest_b, state);
pa(state);
}

38
src/sort/best_merge_a.c Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* best_merge_a.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:15:18 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:15:20 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include <limits.h>
int best_merge_a(t_state *state)
{
const int size = ft_lstsize(state->b);
int moves;
int best_b;
int min_moves;
int i;
min_moves = INT_MAX;
best_b = 0;
i = 0;
while (i < size)
{
moves = count_moves_merge_a(i, state);
if (moves < min_moves)
{
min_moves = moves;
best_b = i;
}
i++;
}
return (best_b);
}

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* common_rotations.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:15:24 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:15:26 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void common_rotations(int *shortest_a, int *shortest_b, t_state *state)
{
if (*shortest_a * *shortest_b > 0)
{
if (*shortest_a > 0)
{
while (*shortest_a > 0 && *shortest_b > 0)
{
rr(state);
(*shortest_a)--;
(*shortest_b)--;
}
}
else
{
while (*shortest_a < 0 && *shortest_b < 0)
{
rrr(state);
(*shortest_a)++;
(*shortest_b)++;
}
}
}
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* count_moves_merge_a.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:15:31 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:15:32 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int count_moves_merge_a(int idx_b, t_state *state)
{
const int number_b = *(int *) ft_lstat(state->b, idx_b)->content;
const int idx_a = put_at_index_asc(number_b, state->a);
const int shortest_a = shortest_rotate(idx_a, ft_lstsize(state->a));
const int shortest_b = shortest_rotate(idx_b, ft_lstsize(state->b));
if (shortest_a * shortest_b > 0)
return (ft_max(ft_abs(shortest_a), ft_abs(shortest_b)) + 1);
return (ft_abs(shortest_a) + ft_abs(shortest_b) + 1);
}

View File

@ -6,94 +6,22 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */ /* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/17 17:25:40 by whaffman #+#+# #+#+# #+# #+# | */ /* Created: 2024/11/17 17:25:40 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/17 17:39:12 by whaffman ### ### ### ### / \ */ /* Updated: 2024/11/24 15:15:39 by whaffman ### ### ### ### / \ */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #include "push_swap.h"
#include <limits.h>
int count_moves_merge_a(int idx_b, t_state *state)
{
const int number_b = *(int *) ft_lstat(state->b, idx_b)->content;
const int idx_a = put_at_index_asc(number_b, state->a);
const int shortest_a = shortest_rotate(idx_a, ft_lstsize(state->a));
const int shortest_b = shortest_rotate(idx_b, ft_lstsize(state->b));
if (shortest_a * shortest_b > 0)
return (ft_max(ft_abs(shortest_a), ft_abs(shortest_b)) + 1);
return (ft_abs(shortest_a) + ft_abs(shortest_b) + 1);
}
int best_merge_a(t_state *state)
{
const int size = ft_lstsize(state->b);
int moves;
int best_b;
int min_moves;
int i;
min_moves = INT_MAX;
best_b = 0;
i = 0;
while (i < size)
{
moves = count_moves_merge_a(i, state);
if (moves < min_moves)
{
min_moves = moves;
best_b = i;
}
i++;
}
return (best_b);
}
void b_merge_a(int idx_b, t_state *state)
{
const int number_b = *(int *) ft_lstat(state->b, idx_b)->content;
const int idx_a = put_at_index_asc(number_b, state->a);
int shortest_a;
int shortest_b;
shortest_a = shortest_rotate(idx_a, ft_lstsize(state->a));
shortest_b = shortest_rotate(idx_b, ft_lstsize(state->b));
common_rotations(&shortest_a, &shortest_b, state);
rotate_a(shortest_a, state);
rotate_b(shortest_b, state);
pa(state);
}
void common_rotations(int *shortest_a, int *shortest_b, t_state *state)
{
if (*shortest_a * *shortest_b > 0)
{
if (*shortest_a > 0)
{
while (*shortest_a > 0 && *shortest_b > 0)
{
rr(state);
(*shortest_a)--;
(*shortest_b)--;
}
}
else
{
while (*shortest_a < 0 && *shortest_b < 0)
{
rrr(state);
(*shortest_a)++;
(*shortest_b)++;
}
}
}
}
void sortmore(t_state *state) void sortmore(t_state *state)
{
if (ft_lstsize(state->a) <= 5)
{ {
push_all_but_3_b(state); push_all_but_3_b(state);
if (ft_lstsize(state->a) == 3) if (ft_lstsize(state->a) == 3)
sort3(state); sort3(state);
}
else
push_all_but_lis(state);
while (state->b) while (state->b)
b_merge_a(best_merge_a(state), state); b_merge_a(best_merge_a(state), state);
rotate_a_to_top(state); rotate_a_to_top(state);

27
src/util/count_gt.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* count_gt.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:15:02 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:15:04 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int count_gt(int n, t_list *stack)
{
int count;
count = 0;
while (stack)
{
if (*(int *) stack->content < n)
count++;
stack = stack->next;
}
return (count);
}

21
src/util/error.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* error.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:14:55 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:14:58 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "push_swap.h"
int error(t_state *state)
{
free_state(state);
ft_putstr_fd("Error\n", 2);
return (1);
}

26
src/util/free_state.c Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* free_state.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:14:32 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:14:51 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "push_swap.h"
#include <stdlib.h>
void free_state(t_state *state)
{
if (!state)
return ;
if (state->a)
ft_lstclear(&(state->a), free);
if (state->b)
ft_lstclear(&(state->b), free);
free(state);
}

18
src/util/ft_abs.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* ft_abs.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:14:26 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:14:27 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
int ft_abs(int a)
{
if (a >= 0)
return (a);
return (-a);
}

18
src/util/ft_max.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* ft_max.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:14:17 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:14:20 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
int ft_max(int a, int b)
{
if (a > b)
return (a);
return (b);
}

31
src/util/has_duplicates.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* has_duplicates.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:14:06 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:14:13 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int has_duplicates(t_list *stack)
{
t_list *node;
while (stack)
{
node = stack->next;
while (node)
{
if (*(int *) stack->content == *(int *) node->content)
return (1);
node = node->next;
}
stack = stack->next;
}
return (0);
}

View File

@ -1,16 +1,15 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: o_ :::::: ::: */ /* ::: o_ :::::: ::: */
/* utils.c :+: / :+::+: :+: */ /* index_of.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */ /* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */ /* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/10 14:44:51 by whaffman #+#+# #+#+# #+# #+# | */ /* Created: 2024/11/24 15:14:00 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/17 17:41:19 by whaffman ### ### ### ### / \ */ /* Updated: 2024/11/24 15:14:01 by whaffman ### ### ### ### / \ */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h"
#include "push_swap.h" #include "push_swap.h"
int index_of(int number, t_list *stack) int index_of(int number, t_list *stack)
@ -27,31 +26,3 @@ int index_of(int number, t_list *stack)
} }
return (-1); return (-1);
} }
int ft_abs(int a)
{
if (a >= 0)
return (a);
return (-a);
}
int ft_max(int a, int b)
{
if (a > b)
return (a);
return (b);
}
int count_gt(int n, t_list *stack)
{
int count;
count = 0;
while (stack)
{
if (*(int *) stack->content < n)
count++;
stack = stack->next;
}
return (count);
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* initialise_state.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:13:53 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:13:55 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include <stdlib.h>
t_state *initialise_state(t_state **state)
{
*state = malloc(sizeof (t_state));
if (*state)
{
(*state)->a = NULL;
(*state)->b = NULL;
}
return (*state);
}

24
src/util/is_sorted.c Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* is_sorted.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:13:34 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:13:43 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int is_sorted(t_list *stack)
{
while (stack->next)
{
if (*(int *) stack->content > *(int *) stack->next->content)
return (0);
stack = stack->next;
}
return (1);
}

36
src/util/lis_indices.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* lis_indices.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:12:55 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:13:28 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include <stdlib.h>
int *lis_indices(int *lengths, int size, int max_len)
{
int *indices;
int i;
int j;
indices = (int *)malloc(sizeof(int) * max_len);
if (!indices)
return (NULL);
i = size - 1;
j = max_len - 1;
while (i >= 0)
{
if (lengths[i] == max_len)
{
indices[j--] = i;
max_len--;
}
i--;
}
return (indices);
}

43
src/util/lis_lengths.c Normal file
View File

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* lis_lengths.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:11:44 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:12:41 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include <stdlib.h>
int *lis_lengths(t_list *stack, int size)
{
int *lengths;
int i;
int j;
lengths = (int *)malloc(sizeof(int) * size);
if (!lengths)
return (NULL);
i = 0;
while (i < size)
lengths[i++] = 1;
i = 1;
while (i < size)
{
j = 0;
while (j < i)
{
if (*(int *)ft_lstat(stack, j)->content
< *(int *)ft_lstat(stack, i)->content
&& lengths[i] < lengths[j] + 1)
lengths[i] = lengths[j] + 1;
j++;
}
i++;
}
return (lengths);
}

View File

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* longest_incremental_subsequence.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:08:27 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:08:31 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
#include "push_swap.h"
int *longest_incremental_subsequence(t_list *stack, int *lis_size)
{
int size;
int *lengths;
int max_len;
int *indices;
int i;
size = ft_lstsize(stack);
if (size == 0)
return (NULL);
lengths = lis_lengths(stack, size);
if (!lengths)
return (NULL);
max_len = 0;
i = 0;
while (i < size)
{
if (lengths[i] > max_len)
max_len = lengths[i];
i++;
}
indices = lis_indices(lengths, size, max_len);
free(lengths);
*lis_size = max_len;
return (indices);
}

35
src/util/print_stack.c Normal file
View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* print_stack.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:06:56 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:08:07 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "push_swap.h"
void print_stack(const char *name, t_list *stack)
{
int lst_size;
t_list *node;
ft_printf("%s |", name);
if (!stack)
{
ft_printf("\n");
return ;
}
lst_size = ft_lstsize(stack);
while (lst_size > 0)
{
node = ft_lstat(stack, lst_size - 1);
ft_printf("%d ", *((int *) node->content));
lst_size--;
}
ft_printf("\n");
}

21
src/util/print_state.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* print_state.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/24 15:07:34 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/24 15:07:35 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void print_state(t_state *state)
{
ft_printf("\n");
print_stack("A", state->a);
print_stack("B", state->b);
ft_printf("===================\n");
}

46
tester.sh Normal file → Executable file
View File

@ -3,7 +3,7 @@
# This script is used to test the program # This script is used to test the program
run_test_case() { run_test_case() {
echo "Test case $1" # echo "Test case $1"
MAX=0 MAX=0
MIN=10000000 MIN=10000000
AVG=0 AVG=0
@ -37,22 +37,40 @@ run_test_case() {
fi fi
done done
AVG=$((AVG / ITER)) AVG=$((AVG / ITER))
echo "Iterations: $ITER" # echo -e "$1,\t$AVG,\t$MAX,\t$MIN"
echo "Max: $MAX" echo "List size: $1 Iterations: $ITER avg: $AVG max: $MAX min: $MIN"
echo "Min: $MIN" # echo "Max: $MAX"
echo "Average: $AVG" # echo "Min: $MIN"
echo "--------------------------------" # echo "Average: $AVG"
echo "Max Args: $MAX_ARGS" # echo "--------------------------------"
echo "--------------------------------" # echo "Max Args: $MAX_ARGS"
echo "" # echo "--------------------------------"
# echo ""
} }
run_test_case 1 10 run_test_case 1 10
run_test_case 2 10 run_test_case 2 10
run_test_case 3 200 run_test_case 3 10
run_test_case 5 200 run_test_case 4 10
run_test_case 50 100 run_test_case 5 10
run_test_case 100 100 run_test_case 6 10
run_test_case 500 50 run_test_case 7 10
run_test_case 8 10
run_test_case 9 10
run_test_case 10 10
run_test_case 20 10
run_test_case 30 10
run_test_case 40 10
run_test_case 50 10
run_test_case 60 10
run_test_case 70 10
run_test_case 80 10
run_test_case 90 10
run_test_case 100 10
run_test_case 200 10
run_test_case 300 10
run_test_case 400 10
run_test_case 500 10