made printing move optional and created the checker, and put gnl in libft
This commit is contained in:
parent
d0f8e125c3
commit
e266e6dcc3
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,3 +8,4 @@ Session.vim
|
||||
test.txt
|
||||
checker_linux
|
||||
.vscode/
|
||||
checker
|
||||
17
Makefile
17
Makefile
@ -22,7 +22,7 @@ VPATH = src:src/util:src/moves:src/sort
|
||||
|
||||
SOURCES = push_all_but_3_b.c push_all_but_lis.c push.c put_at_index_asc.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 \
|
||||
rotate_b.c rotate.c shortest_rotate.c 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 \
|
||||
@ -30,7 +30,12 @@ SOURCES = push_all_but_3_b.c push_all_but_lis.c push.c put_at_index_asc.c \
|
||||
longest_incremental_subsequence.c new_element.c print_stack.c \
|
||||
print_state.c stack_min.c
|
||||
|
||||
SOURCES_PUSH_SWAP = push_swap.c
|
||||
SOURCES_CHECKER = checker.c
|
||||
|
||||
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))
|
||||
OBJECTS_PUSH_SWAP = $(addprefix $(OBJ_PATH)/, $(SOURCES_PUSH_SWAP:.c=.o))
|
||||
OBJECTS_CHECKER = $(addprefix $(OBJ_PATH)/, $(SOURCES_CHECKER:.c=.o))
|
||||
|
||||
CC = cc
|
||||
RM = rm -rf
|
||||
@ -40,15 +45,19 @@ CFLAGS = -Wall -Wextra -Werror -g
|
||||
LDFLAGS = -L./libft
|
||||
LDLIBS = -lft
|
||||
|
||||
all: $(NAME)
|
||||
all: $(NAME) checker
|
||||
|
||||
$(LIBFT):
|
||||
@echo "Make Libft and add the archive to lib and the header to inc"
|
||||
@$(MAKE) -sC libft
|
||||
|
||||
$(NAME): $(OBJECTS)
|
||||
$(NAME): $(OBJECTS) $(OBJECTS_PUSH_SWAP)
|
||||
@echo "Linking the object files in the executable."
|
||||
@$(CC) $(CFLAGS) $(OBJECTS) $(LDFLAGS) $(LDLIBS) -o $(NAME)
|
||||
@$(CC) $(CFLAGS) $(OBJECTS) $(OBJECTS_PUSH_SWAP) $(LDFLAGS) $(LDLIBS) -o $(NAME)
|
||||
|
||||
checker: $(OBJECTS) $(OBJECTS_CHECKER)
|
||||
@echo "Linking the object files in the executable."
|
||||
@$(CC) $(CFLAGS) $(OBJECTS) $(OBJECTS_CHECKER) $(LDFLAGS) $(LDLIBS) -o checker
|
||||
|
||||
$(OBJ_PATH):
|
||||
@mkdir -p $@
|
||||
|
||||
@ -66,15 +66,15 @@ void reverse_rotate(t_list **stack);
|
||||
|
||||
void swap(t_list **stack);
|
||||
|
||||
void pa(t_state *state);
|
||||
void pb(t_state *state);
|
||||
void sa(t_state *state);
|
||||
void sb(t_state *state);
|
||||
void ss(t_state *state);
|
||||
void ra(t_state *state);
|
||||
void rb(t_state *state);
|
||||
void rr(t_state *state);
|
||||
void rra(t_state *state);
|
||||
void rrb(t_state *state);
|
||||
void rrr(t_state *state);
|
||||
void pa(t_state *state, int silent);
|
||||
void pb(t_state *state, int silent);
|
||||
void sa(t_state *state, int silent);
|
||||
void sb(t_state *state, int silent);
|
||||
void ss(t_state *state, int silent);
|
||||
void ra(t_state *state, int silent);
|
||||
void rb(t_state *state, int silent);
|
||||
void rr(t_state *state, int silent);
|
||||
void rra(t_state *state, int silent);
|
||||
void rrb(t_state *state, int silent);
|
||||
void rrr(t_state *state, int silent);
|
||||
#endif
|
||||
|
||||
@ -23,7 +23,7 @@ WARNINGS = -Wall -Wextra -Werror
|
||||
CFLAGS = $(WARNINGS)
|
||||
|
||||
VPATH = src:src/conversion:src/ft_printf:src/list:src/memory:src/output:\
|
||||
src/string
|
||||
src/string:src/get_next_line
|
||||
|
||||
SRC_CONVERSION = ft_atoi.c ft_itoa.c ft_tolower.c ft_toupper.c \
|
||||
|
||||
@ -33,7 +33,7 @@ SRC_MEMORY = ft_memchr.c ft_memcmp.c ft_memcpy.c ft_memmove.c ft_memset.c \
|
||||
SRC_STRING = ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c ft_isprint.c \
|
||||
ft_split.c ft_strchr.c ft_strdup.c ft_striteri.c ft_strjoin.c \
|
||||
ft_strlcat.c ft_strlcpy.c ft_strlen.c ft_strmapi.c ft_strncmp.c \
|
||||
ft_strnstr.c ft_strrchr.c ft_strtrim.c ft_substr.c
|
||||
ft_strnstr.c ft_strrchr.c ft_strtrim.c ft_substr.c ft_strcmp.c
|
||||
|
||||
SRC_LIST = ft_lstadd_back.c ft_lstadd_front.c ft_lstclear.c ft_lstdelone.c \
|
||||
ft_lstiter.c ft_lstlast.c ft_lstmap.c ft_lstnew.c ft_lstsize.c
|
||||
@ -45,8 +45,10 @@ SRC_FT_PRINTF = ft_isbase.c ft_putnbr_base.c ft_write_str.c parse_placeholder.c
|
||||
ft_putnbr_signed.c parse_conversion.c print_char.c \
|
||||
print_number.c print_string.c
|
||||
|
||||
SRC_GET_NEXT_LINE = get_next_line.c
|
||||
|
||||
SOURCES = $(SRC_CONVERSION) $(SRC_MEMORY) $(SRC_STRING) \
|
||||
$(SRC_LIST) $(SRC_OUTPUT) $(SRC_FT_PRINTF)
|
||||
$(SRC_LIST) $(SRC_OUTPUT) $(SRC_FT_PRINTF) $(SRC_GET_NEXT_LINE)
|
||||
|
||||
OBJECTS = $(addprefix $(OBJ_DIR)/, $(SOURCES:.c=.o))
|
||||
|
||||
|
||||
@ -37,6 +37,7 @@ int ft_tolower(int c);
|
||||
char *ft_strchr(const char *s, int c);
|
||||
char *ft_strrchr(const char *s, int c);
|
||||
int ft_strncmp(const char *s1, const char *s2, size_t n);
|
||||
int ft_strcmp(const char *s1, const char *s2);
|
||||
void *ft_memchr(const void *s, int c, size_t n);
|
||||
int ft_memcmp(const void *s1, const void *s2, size_t n);
|
||||
char *ft_strnstr(const char *big, const char *little, size_t len);
|
||||
@ -74,4 +75,12 @@ t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
|
||||
*/
|
||||
|
||||
int ft_printf(const char *format, ...);
|
||||
|
||||
/*
|
||||
* get_next_line
|
||||
*/
|
||||
# define OPEN_MAX 1024
|
||||
# define BUFFER_SIZE 256
|
||||
char *get_next_line(int fd);
|
||||
|
||||
#endif
|
||||
|
||||
@ -11,12 +11,36 @@
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void execute_move(const char *move, t_state *state)
|
||||
{
|
||||
void (*const moves[])(t_state *, int) = {&sa, &sb, &ss, &pa, &pb, \
|
||||
&ra, &rb, &rr, &rra, &rrb, &rrr};
|
||||
const char *moves_names[] = {"sa\n", "sb\n", "ss\n", "pa\n", "pb\n", \
|
||||
"ra\n", "rb\n", "rr\n", "rra\n", "rrb\n", "rrr\n"};
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < 11)
|
||||
{
|
||||
if (ft_strcmp(move, moves_names[i]) == 0)
|
||||
{
|
||||
moves[i](state, 1);
|
||||
return;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
ft_putstr_fd("Error: Invalid move\n", 2);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int size;
|
||||
t_state *state;
|
||||
char *move;
|
||||
|
||||
state = NULL;
|
||||
if (!initialise_state(&state))
|
||||
return (1);
|
||||
while (argc-- > 1)
|
||||
@ -26,9 +50,16 @@ int main(int argc, char *argv[])
|
||||
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);
|
||||
size = ft_lstsize(state->a);
|
||||
move = get_next_line(STDIN_FILENO);
|
||||
while (move)
|
||||
{
|
||||
execute_move(move, state);
|
||||
free(move);
|
||||
move = get_next_line(STDIN_FILENO);
|
||||
}
|
||||
free(move);
|
||||
if (ft_lstsize(state->a) == size && (size == 0 || is_sorted(state->a)))
|
||||
return (free_state(state), ft_printf("OK\n"));
|
||||
return (free_state(state), ft_printf("KO\n"));
|
||||
}
|
||||
|
||||
@ -12,28 +12,27 @@
|
||||
|
||||
#include "libft.h"
|
||||
#include "push_swap.h"
|
||||
#include <unistd.h>
|
||||
|
||||
void pb(t_state *state)
|
||||
void pb(t_state *state, int silent)
|
||||
{
|
||||
push(&(state->a), &(state->b));
|
||||
#ifndef SILENT
|
||||
ft_printf("pb\n");
|
||||
#endif
|
||||
if (!silent)
|
||||
ft_putstr_fd("pb\n", STDOUT_FILENO);
|
||||
}
|
||||
|
||||
void pa(t_state *state)
|
||||
void pa(t_state *state, int silent)
|
||||
{
|
||||
push(&(state->b), &(state->a));
|
||||
#ifndef SILENT
|
||||
ft_printf("pa\n");
|
||||
#endif
|
||||
if (!silent)
|
||||
ft_putstr_fd("pa\n", STDOUT_FILENO);
|
||||
}
|
||||
|
||||
void push(t_list **stack_1, t_list **stack_2)
|
||||
{
|
||||
t_list *temp;
|
||||
|
||||
if (!stack_1)
|
||||
if (!*stack_1)
|
||||
return ;
|
||||
temp = (*stack_1)->next;
|
||||
ft_lstadd_front(stack_2, *stack_1);
|
||||
|
||||
@ -20,5 +20,5 @@ void push_all_but_3_b(t_state *state)
|
||||
|
||||
i = 0;
|
||||
while (i++ < size - 3)
|
||||
pb(state);
|
||||
pb(state, 0);
|
||||
}
|
||||
|
||||
@ -29,10 +29,10 @@ void push_all_but_lis(t_state *state)
|
||||
if (j < lis_size && i == lis_indices[j])
|
||||
{
|
||||
j++;
|
||||
ra(state);
|
||||
ra(state, 0);
|
||||
}
|
||||
else
|
||||
pb(state);
|
||||
pb(state, 0);
|
||||
i++;
|
||||
}
|
||||
free(lis_indices);
|
||||
|
||||
@ -12,30 +12,28 @@
|
||||
|
||||
#include "libft.h"
|
||||
#include "push_swap.h"
|
||||
#include <unistd.h>
|
||||
|
||||
void rra(t_state *state)
|
||||
void rra(t_state *state, int silent)
|
||||
{
|
||||
reverse_rotate(&(state->a));
|
||||
#ifndef SILENT
|
||||
ft_printf("rra\n");
|
||||
#endif
|
||||
if (!silent)
|
||||
ft_putstr_fd("rra\n", STDOUT_FILENO);
|
||||
}
|
||||
|
||||
void rrb(t_state *state)
|
||||
void rrb(t_state *state, int silent)
|
||||
{
|
||||
reverse_rotate(&(state->b));
|
||||
#ifndef SILENT
|
||||
ft_printf("rrb\n");
|
||||
#endif
|
||||
if (!silent)
|
||||
ft_putstr_fd("rrb\n", STDOUT_FILENO);
|
||||
}
|
||||
|
||||
void rrr(t_state *state)
|
||||
void rrr(t_state *state, int silent)
|
||||
{
|
||||
reverse_rotate(&(state->a));
|
||||
reverse_rotate(&(state->b));
|
||||
#ifndef SILENT
|
||||
ft_printf("rrr\n");
|
||||
#endif
|
||||
if (!silent)
|
||||
ft_putstr_fd("rrr\n", STDOUT_FILENO);
|
||||
}
|
||||
|
||||
void reverse_rotate(t_list **stack)
|
||||
@ -43,7 +41,7 @@ void reverse_rotate(t_list **stack)
|
||||
t_list *temp;
|
||||
t_list *last;
|
||||
|
||||
if (!stack)
|
||||
if (!*stack)
|
||||
return ;
|
||||
temp = *stack;
|
||||
last = ft_lstlast(*stack);
|
||||
|
||||
@ -12,37 +12,35 @@
|
||||
|
||||
#include "libft.h"
|
||||
#include "push_swap.h"
|
||||
#include <unistd.h>
|
||||
|
||||
void ra(t_state *state)
|
||||
void ra(t_state *state, int silent)
|
||||
{
|
||||
rotate(&(state->a));
|
||||
#ifndef SILENT
|
||||
ft_printf("ra\n");
|
||||
#endif
|
||||
if (!silent)
|
||||
ft_putstr_fd("ra\n", STDOUT_FILENO);
|
||||
}
|
||||
|
||||
void rb(t_state *state)
|
||||
void rb(t_state *state, int silent)
|
||||
{
|
||||
rotate(&(state->b));
|
||||
#ifndef SILENT
|
||||
ft_printf("rb\n");
|
||||
#endif
|
||||
if (!silent)
|
||||
ft_putstr_fd("rb\n", STDOUT_FILENO);
|
||||
}
|
||||
|
||||
void rr(t_state *state)
|
||||
void rr(t_state *state, int silent)
|
||||
{
|
||||
rotate(&(state->a));
|
||||
rotate(&(state->b));
|
||||
#ifndef SILENT
|
||||
ft_printf("rr\n");
|
||||
#endif
|
||||
if (!silent)
|
||||
ft_putstr_fd("rr\n", STDOUT_FILENO);
|
||||
}
|
||||
|
||||
void rotate(t_list **stack)
|
||||
{
|
||||
t_list *temp;
|
||||
|
||||
if (!stack)
|
||||
if (!*stack)
|
||||
return ;
|
||||
temp = (*stack)->next;
|
||||
(*stack)->next = NULL;
|
||||
|
||||
@ -16,12 +16,12 @@ void rotate_a(int n, t_state *state)
|
||||
{
|
||||
while (n > 0)
|
||||
{
|
||||
ra(state);
|
||||
ra(state, 0);
|
||||
n--;
|
||||
}
|
||||
while (n < 0)
|
||||
{
|
||||
rra(state);
|
||||
rra(state, 0);
|
||||
n++;
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,12 +16,12 @@ void rotate_b(int n, t_state *state)
|
||||
{
|
||||
while (n > 0)
|
||||
{
|
||||
rb(state);
|
||||
rb(state, 0);
|
||||
n--;
|
||||
}
|
||||
while (n < 0)
|
||||
{
|
||||
rrb(state);
|
||||
rrb(state, 0);
|
||||
n++;
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,30 +12,27 @@
|
||||
|
||||
#include "libft.h"
|
||||
#include "push_swap.h"
|
||||
#include <unistd.h>
|
||||
|
||||
void sa(t_state *state)
|
||||
void sa(t_state *state, int silent)
|
||||
{
|
||||
swap(&(state->a));
|
||||
#ifndef SILENT
|
||||
ft_printf("sa\n");
|
||||
#endif
|
||||
if (!silent)
|
||||
ft_putstr_fd("sa\n", STDOUT_FILENO);
|
||||
}
|
||||
|
||||
void sb(t_state *state)
|
||||
void sb(t_state *state, int silent)
|
||||
{
|
||||
swap(&(state->b));
|
||||
#ifndef SILENT
|
||||
ft_printf("sb\n");
|
||||
#endif
|
||||
if (!silent)
|
||||
ft_putstr_fd("sb\n", STDOUT_FILENO);
|
||||
}
|
||||
|
||||
void ss(t_state *state)
|
||||
void ss(t_state *state, int silent)
|
||||
{
|
||||
swap(&(state->a));
|
||||
swap(&(state->b));
|
||||
#ifndef SILENT
|
||||
ft_printf("ss\n");
|
||||
#endif
|
||||
if (!silent)
|
||||
ft_putstr_fd("ss\n", STDOUT_FILENO);
|
||||
}
|
||||
|
||||
void swap(t_list **stack)
|
||||
|
||||
@ -24,5 +24,5 @@ void b_merge_a(int idx_b, t_state *state)
|
||||
common_rotations(&shortest_a, &shortest_b, state);
|
||||
rotate_a(shortest_a, state);
|
||||
rotate_b(shortest_b, state);
|
||||
pa(state);
|
||||
pa(state, 0);
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ void common_rotations(int *shortest_a, int *shortest_b, t_state *state)
|
||||
{
|
||||
while (*shortest_a > 0 && *shortest_b > 0)
|
||||
{
|
||||
rr(state);
|
||||
rr(state, 0);
|
||||
(*shortest_a)--;
|
||||
(*shortest_b)--;
|
||||
}
|
||||
@ -29,7 +29,7 @@ void common_rotations(int *shortest_a, int *shortest_b, t_state *state)
|
||||
{
|
||||
while (*shortest_a < 0 && *shortest_b < 0)
|
||||
{
|
||||
rrr(state);
|
||||
rrr(state, 0);
|
||||
(*shortest_a)++;
|
||||
(*shortest_b)++;
|
||||
}
|
||||
|
||||
@ -32,10 +32,10 @@ int sort3(t_state *state)
|
||||
return (1);
|
||||
else if (a < b && b > c && a < c)
|
||||
{
|
||||
rra(state);
|
||||
sa(state);
|
||||
rra(state, 0);
|
||||
sa(state, 0);
|
||||
}
|
||||
else if ((a > b && a < c) || (a > b && b > c))
|
||||
sa(state);
|
||||
sa(state, 0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
15
tester.sh
15
tester.sh
@ -14,7 +14,7 @@ run_test_case() {
|
||||
do
|
||||
ARG=$(seq -1000 1000 | shuf -n $N)
|
||||
OPS=$(./push_swap $ARG | wc -l)
|
||||
OK=$(./push_swap $ARG | ./checker_linux $ARG)
|
||||
OK=$(./push_swap $ARG | ./checker $ARG)
|
||||
if [ $OPS -gt $MAX ]
|
||||
then
|
||||
MAX_ARGS="Max: $OPS | $(echo $ARG | tr -d '\n')"
|
||||
@ -49,6 +49,17 @@ run_test_case() {
|
||||
|
||||
}
|
||||
|
||||
run_test_case 500 1000
|
||||
run_test_case 1 10
|
||||
run_test_case 2 10
|
||||
run_test_case 3 10
|
||||
run_test_case 4 10
|
||||
run_test_case 5 10
|
||||
run_test_case 10 10
|
||||
run_test_case 20 10
|
||||
run_test_case 50 10
|
||||
run_test_case 100 10
|
||||
run_test_case 200 10
|
||||
run_test_case 500 10
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user