diff --git a/.gitignore b/.gitignore index 14b2bca..6ccc634 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,4 @@ test push_swap push_swap_visualizer Push-Swap-Tester +checker diff --git a/Makefile b/Makefile index c8ff40e..bf3d702 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # -# Updated: 2024/02/08 14:25:56 by maiboyer ### ########.fr # +# Updated: 2024/02/09 15:27:44 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -19,12 +19,14 @@ LIBS_DIR = . GENERIC_DIR = output/src GENERIC_INCLUDE = output/include +NAME = push_swap + ifeq ($(MAKECMDGOALS), bonus) - CFLAGS += -DBONUS - BUILD_DIR := $(BUILD_DIR)/bonus/ + CFLAGS += -DBONUS=1 + BUILD_DIR := $(BUILD_DIR)/bonus/ + NAME = checker endif -NAME = push_swap LIB_NAME ?= TARGET = $(NAME) CC ?= clang @@ -111,6 +113,8 @@ fclean: clean @printf $(COL_WHITE)Clearing\ Output\ $(COL_GRAY)%-28s$(COL_RESET)\ \ \($(LIB_NAME)$(NAME)\) @rm -f $(BUILD_DIR)$(NAME) + @rm -f push_swap + @rm -f checker @printf $(COL_GREEN)done$(COL_RESET)\\n re: fclean all diff --git a/mecstd b/mecstd index 60b9db4..caed78a 160000 --- a/mecstd +++ b/mecstd @@ -1 +1 @@ -Subproject commit 60b9db4c20aa957cff32ae58dfc8a2124f9b35d5 +Subproject commit caed78a6223582e876f0fc748ca18350f572f34e diff --git a/src.list b/src.list index 4cb65a2..2015edd 100644 --- a/src.list +++ b/src.list @@ -1,9 +1,11 @@ +app/actual_main app/best_index_to_move app/best_move app/do_move app/find_place app/iter_find app/main +app/main_bonus app/moves app/moves/push app/moves/rev_rotate @@ -15,3 +17,4 @@ app/sort2 app/sort3 app/sort5 app/target +bonus/move1 diff --git a/src/app/actual_main.c b/src/app/actual_main.c new file mode 100644 index 0000000..c0f79ce --- /dev/null +++ b/src/app/actual_main.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* actual_main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/09 15:10:39 by maiboyer #+# #+# */ +/* Updated: 2024/02/09 15:26:33 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/printf/printf.h" +#include "me/string/str_n_compare.h" +#include "me/types.h" + +int main_normal(t_i32 argc, t_str argv[]); +int main_checker(t_i32 argc, t_str argv[]); + +int main(t_i32 argc, t_str argv[]) +{ + if (BONUS) + return (main_checker(argc, argv)); + return (main_normal(argc, argv)); +} diff --git a/src/app/main.c b/src/app/main.c index 0b7965f..53d09c1 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */ -/* Updated: 2024/02/08 19:05:25 by maiboyer ### ########.fr */ +/* Updated: 2024/02/09 15:50:10 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,41 +14,71 @@ #include "app/types/type_i64_bool.h" #include "me/convert/str_to_numbers.h" #include "me/printf/printf.h" +#include "me/string/str_n_compare.h" #include "me/types.h" #include "me/vec/vec_i64.h" #include "me/vec/vec_i64_bool.h" +#include #include - #if BONUS == 1 -# define ERR_INVALID_NUM "KO\n" -# define ERR_DUPLICATE "KO\n" +// # define ERR_INVALID_NUM "KO1\n" +// # define ERR_DUPLICATE "KO2\n" +# define ERR_INVALID_NUM "Error\n" //:\nInvalid Number\n" +# define ERR_DUPLICATE "Error\n" // :\nDuplicate Number\n" + +t_usize print_error(t_const_str fmt, ...) +{ + va_list args; + t_usize ret; + + va_start(args, fmt); + ret = me_veprintf(fmt, &args); + va_end(args); + return (ret); +} + #else -# define ERR_INVALID_NUM "Error:\nInvalid Number\n" -# define ERR_DUPLICATE "Error:\nDuplicate Number\n" +// # define ERR_INVALID_NUM "Error:\nInvalid Number\n" +// # define ERR_DUPLICATE "Error:\nDuplicate Number\n" + +# define ERR_INVALID_NUM "Error\n" //:\nInvalid Number\n" +# define ERR_DUPLICATE "Error\n" // :\nDuplicate Number\n" + +t_usize print_error(t_const_str fmt, ...) +{ + va_list args; + t_usize ret; + + va_start(args, fmt); + ret = me_veprintf(fmt, &args); + va_end(args); + return (ret); +} + #endif -void sort_3(t_state *state); -void sort_2(t_state *state); -void sort_5(t_state *state); +void sort_3(t_state *state); +void sort_2(t_state *state); +void sort_5(t_state *state); +bool is_sorted(t_vec_i64 *v); +void run_with_items(t_state *state); -void run_with_items(t_state *state); - -bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) +bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) { return (lhs->value <= rhs->value); } -void free_state(t_state state) +void free_state(t_state state) { vec_i64_free(state.stack_a); vec_i64_free(state.stack_b); vec_i64_bool_free(state.sorted); } -bool duplicate_check(t_state *state) +bool duplicate_check(t_state *state) { t_i64 last; - t_usize index; + t_usize index; if (state->sorted.len == 0) return (false); @@ -63,11 +93,11 @@ bool duplicate_check(t_state *state) return (false); } -t_state parses_arguments(t_usize count, t_str nums[]) +t_state parses_arguments(t_usize count, t_str nums[]) { - t_state state; + t_state state; t_i32 atoi; - t_usize i; + t_usize i; state.stack_a = vec_i64_new(count, NULL); state.stack_b = vec_i64_new(count, NULL); @@ -78,8 +108,8 @@ t_state parses_arguments(t_usize count, t_str nums[]) if (str_to_i32(nums[i], 10, &atoi)) (free_state(state), me_eprintf(ERR_INVALID_NUM), exit(1)); vec_i64_push(&state.stack_a, atoi); - vec_i64_bool_push(&state.sorted, (t_i64_bool){.value = atoi, - .active = false}); + vec_i64_bool_push(&state.sorted, + (t_i64_bool){.value = atoi, .active = false}); i++; } vec_i64_bool_sort(&state.sorted, sort_i64_bool); @@ -88,11 +118,9 @@ t_state parses_arguments(t_usize count, t_str nums[]) return (state); } -bool is_sorted(t_vec_i64 *v); - -int main(t_i32 argc, t_str argv[]) +int main_normal(t_i32 argc, t_str argv[]) { - t_state state; + t_state state; (void)(argc--, argv++); state = parses_arguments(argc, argv); @@ -109,4 +137,5 @@ int main(t_i32 argc, t_str argv[]) else run_with_items(&state); free_state(state); + return (0); } diff --git a/src/app/main_bonus.c b/src/app/main_bonus.c new file mode 100644 index 0000000..51132f6 --- /dev/null +++ b/src/app/main_bonus.c @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 18:59:33 by maiboyer #+# #+# */ +/* Updated: 2024/02/09 15:51:09 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#include "me/buffered_str/buf_str.h" +#include "me/gnl/gnl.h" +#include "me/printf/printf.h" +#include "me/string/str_n_compare.h" +#include "me/vec/vec_i64.h" + +#undef BONUS +#define BONUS 1 +#if BONUS + +void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, + t_const_str print); +void swap_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +void rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +void rev_rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); +bool is_sorted(t_vec_i64 *v); + +t_error handle_operation(t_buffer_str s, t_state *state) +{ + t_usize op; + + op = 0; + if (s.buf == NULL) + return (ERROR); + if (str_n_compare(s.buf, "pa", 2) == 0) + push_inner(&state->stack_a, &state->stack_b, NULL, (op++, NULL)); + if (str_n_compare(s.buf, "pb", 2) == 0) + push_inner(&state->stack_b, &state->stack_a, NULL, (op++, NULL)); + + if (str_n_compare(s.buf, "sa", 2) == 0 || + str_n_compare(s.buf, "ss", 2) == 0) + swap_inner(&state->stack_a, NULL, (op++, NULL)); + if (str_n_compare(s.buf, "sb", 2) == 0 || + str_n_compare(s.buf, "ss", 2) == 0) + swap_inner(&state->stack_b, NULL, (op++, NULL)); + + if (str_n_compare(s.buf, "ra", 2) == 0 || + str_n_compare(s.buf, "rr", 2) == 0) + rotate_inner(&state->stack_a, NULL, (op++, NULL)); + if (str_n_compare(s.buf, "rb", 2) == 0 || + str_n_compare(s.buf, "rr", 2) == 0) + rotate_inner(&state->stack_b, NULL, (op++, NULL)); + + if (str_n_compare(s.buf, "rra", 3) == 0 || + str_n_compare(s.buf, "rrr", 3) == 0) + rev_rotate_inner(&state->stack_a, NULL, (op++, NULL)); + if (str_n_compare(s.buf, "rrb", 3) == 0 || + str_n_compare(s.buf, "rrr", 3) == 0) + rev_rotate_inner(&state->stack_b, NULL, (op++, NULL)); + if (op == 0) + return (ERROR); + return (NO_ERROR); +} + +int main_checker(t_i32 argc, t_str argv[]) +{ + t_state state; + t_buffer_str s; + bool err; + (void)(argc--, argv++); + state = parses_arguments(argc, argv); + s = get_next_line(0, &err); + while (!err) + { + if (handle_operation(s, &state)) + { + (str_free(s), free_state(state)); + me_printf("KO\n"); + return (1); + } + str_free(s); + s = get_next_line(0, &err); + } + str_free(s); + if (is_sorted(&state.stack_a) && state.stack_b.len == 0) + me_printf("OK\n"); + else + me_printf("KO\n"); + + free_state(state); + return (0); +} + +#endif diff --git a/src/app/moves/push.c b/src/app/moves/push.c index 66529bd..dc53491 100644 --- a/src/app/moves/push.c +++ b/src/app/moves/push.c @@ -25,7 +25,7 @@ void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, vec_i64_pop_front(from, &e); vec_i64_push_front(to, e); if (print) - ft_printf("%s\n", print); + me_printf("%s\n", print); } void push_a(t_state *s) diff --git a/src/app/moves/rev_rotate.c b/src/app/moves/rev_rotate.c index 868341a..e10e112 100644 --- a/src/app/moves/rev_rotate.c +++ b/src/app/moves/rev_rotate.c @@ -24,7 +24,7 @@ void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag, vec_i64_pop(stack, &e); vec_i64_push_front(stack, e); if (print) - ft_printf("%s\n", print); + me_printf("%s\n", print); } void rev_rotate_a(t_state *s) diff --git a/src/app/moves/rotate.c b/src/app/moves/rotate.c index be95e5a..5fb0bbd 100644 --- a/src/app/moves/rotate.c +++ b/src/app/moves/rotate.c @@ -23,7 +23,7 @@ void rotate_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) vec_i64_pop_front(stack, &e); vec_i64_push(stack, e); if (print) - ft_printf("%s\n", print); + me_printf("%s\n", print); } void rotate_a(t_state *s) diff --git a/src/app/moves/swap.c b/src/app/moves/swap.c index 96e6d96..cb58326 100644 --- a/src/app/moves/swap.c +++ b/src/app/moves/swap.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/02/08 19:17:59 by maiboyer ### ########.fr */ +/* Updated: 2024/02/09 15:40:37 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,14 +20,14 @@ void swap_inner(t_vec_i64 *stack, t_const_str tag, t_const_str print) t_i64 second; (void)(tag); - if (stack->len <= 1) + if (stack->len < 2) return; - vec_i64_pop(stack, &first); - vec_i64_pop(stack, &second); - vec_i64_push(stack, first); - vec_i64_push(stack, second); + vec_i64_pop_front(stack, &first); + vec_i64_pop_front(stack, &second); + vec_i64_push_front(stack, first); + vec_i64_push_front(stack, second); if (print) - ft_printf("%s\n", print); + me_printf("%s\n", print); } void swap_a(t_state *s) diff --git a/src/app/sort5.c b/src/app/sort5.c index 894482c..8c23853 100644 --- a/src/app/sort5.c +++ b/src/app/sort5.c @@ -100,18 +100,18 @@ bool sort_5_specialized(t_state *state, t_i64 cur[5]) vec_i64_sort(&why_do_i_do_this, sort_i64); if (cur[0] == target[3 - 1] && cur[1] == target[4 - 1] && cur[2] == \ target[1 - 1] && cur[3] == target[5 - 1] && cur[4] == target[2 - 1]) - return (ft_printf("pb\npb\nra\nsa\npa\nsa\npa\nsa\nrra\n"), true); + return (me_printf("pb\npb\nra\nsa\npa\nsa\npa\nsa\nrra\n"), true); if (cur[0] == target[3 - 1] && cur[1] == target[4 - 1] && cur[2] == \ target[5 - 1] && cur[3] == target[2 - 1] && cur[4] == target[1 - 1]) - return (ft_printf("pb\npb\nsb\nsa\npa\nsa\nrra\nrra\npa\nra\nra\n"), + return (me_printf("pb\npb\nsb\nsa\npa\nsa\nrra\nrra\npa\nra\nra\n"), true); if (cur[0] == target[4 - 1] && cur[1] == target[3 - 1] && cur[2] == \ target[1 - 1] && cur[3] == target[5 - 1] && cur[4] == target[2 - 1]) - return (ft_printf("pb\npb\nra\nsa\npa\nsa\nrra\nrra\npa\nra\nra\n"), + return (me_printf("pb\npb\nra\nsa\npa\nsa\nrra\nrra\npa\nra\nra\n"), true); if (cur[0] == target[4 - 1] && cur[1] == target[3 - 1] && cur[2] == \ target[5 - 1] && cur[3] == target[2 - 1] && cur[4] == target[1 - 1]) - return (ft_printf("pb\npb\nsb\nsa\npa\nsa\npa\nsa\nrra\n"), true); + return (me_printf("pb\npb\nsb\nsa\npa\nsa\npa\nsa\nrra\n"), true); return (false); } diff --git a/src/bonus/main.c b/src/bonus/main.c deleted file mode 100644 index a03f049..0000000 --- a/src/bonus/main.c +++ /dev/null @@ -1,56 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* main.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maiboyer +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/02/08 18:59:33 by maiboyer #+# #+# */ -/* Updated: 2024/02/08 22:05:12 by maiboyer ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "app/state.h" -#include "me/buffered_str/buf_str.h" -#include "me/gnl/gnl.h" -#include "me/string/str_n_compare.h" - -#undef BONUS -#define BONUS 1 -#if BONUS - -void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, - t_const_str print); -void swap_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); -void rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); -void rev_rotate_inner(t_vec_i64 *vec, t_const_str tag, t_const_str print); - -void handle_operation(t_buffer_str s, t_state *state) -{ - if (str_n_compare(s.buf, "pa", 2)) - push_inner(&state->stack_a, &state->stack_b, NULL, NULL); - if (str_n_compare(s.buf, "pb", 2)) - push_inner(&state->stack_b, &state->stack_a, NULL, NULL); - if (str_n_compare(s.buf, "sa", 2) ) - push_inner(&state->stack_a, &state->stack_b, NULL, NULL); - if (str_n_compare(s.buf, "sb", 2)) - push_inner(&state->stack_b, &state->stack_a, NULL, NULL); -} - -int main(t_i32 argc, t_str argv[]) -{ - t_state state; - t_buffer_str s; - bool err; - (void)(argc--, argv++); - state = parses_arguments(argc, argv); - s = get_next_line(0, &err); - while (err) - { - handle_operation(s, &state); - str_free(s); - s = get_next_line(0, &err); - } -} - -#endif