diff --git a/check.sh b/check.sh index b0639d9..ded0e01 100755 --- a/check.sh +++ b/check.sh @@ -3,6 +3,6 @@ ARGS="133 193 -16 -234 239 85 -223 -100 178 212 -202 -102 -199 -233 227 86 -166 224 160 -21 -56 102 -62 62 -127 89 127 -246 1 140 38 157 179 220 154 -1 125 240 -238 78 -175 222 -145 -60 115 67 -220 80 -136 -134 -159 35 136 192 47 117 -51 -140 37 6 -26 -188 213 -204 -210 14 -70 112 201 -191 -22 219 15 -143 -131 -41 -152 -187 24 215 235 -147 170 -168 12 107 105 130 -91 230 -174 -176 -224 -35 5 -110 -180 60 -59 -24 94 -196 -39 21 131 -18 165 -2 -219 -198 -19 -11 244 -151 -80 43 -17 -244 -65 124 9 -153 118 -6 -235 81 225 121 217 -43 30 218 -111 163 -9 23 214 166 207 238 -217 -161 -165 116 -170 26 -44 -106 245 -155 48 -177 75 -66 -89 109 22 56 58 -53 203 -79 39 184 -94 -231 45 246 93 13 -128 -179 -236 -139 114 63 -32 196 -205 57 249 -243 233 247 -46 -90 -47 -201 -144 221 -172 66 -38 -101 -232 175 164 -215 -195 -126 209 -108 -64 126 134 -77 -69 113 146 -211 2 88 -28 -132 -250 77 -181 191 198 183 -239 -52 -118 187 -109 4 -208 73 -96 -81 87 188 122 -120 -29 83 18 92 -83 100 -68 -221 -186 189 41 68 -150 129 -49 -137 -99 -123 -42 150 -192 -82 -158 104 -61 145 16 96 -115 -27 -119 97 -203 -240 -173 -185 -227 -98 72 -237 -7 -105 -76 149 -218 -182 54 76 61 211 173 7 185 -10 19 -15 -84 -48 36 53 -67 -63 155 -169 -138 -141 -78 33 -85 234 138 -146 223 158 135 91 108 55 32 90 226 -230 -40 177 -34 -112 -190 144 -104 -241 71 237 -226 95 208 -31 180 59 52 151 153 -135 46 -50 -154 204 132 119 70 110 199 -225 169 194 -148 148 241 -37 27 -23 176 232 -156 206 200 -167 -14 3 -209 159 -75 -248 111 -73 34 -113 -184 25 -86 -122 106 11 162 -133 -88 -245 147 -178 -197 190 -117 84 210 -54 -247 98 -157 64 -95 -129 10 -207 -72 182 -71 181 51 79 -87 242 20 123 82 99 -130 49 101 216 -216 -93 -97 172 -125 -163 -200 -8 -206 197 -242 -194 -212 168 161 -45 103 -160 195 -249 -25 69 137 167 152 -20 -142 243 -55 -171 -36 156 -12 -149 29 28 -183 50 120 -222 17 174 -4 -74 143 236 -189 44 -228 -162 40 229 228 -124 248 231 -5 -193 65 -13 139 128 -92 31 -103 142 -3 -33 205 -164 186 -121 -57 0 -229 42 -114 -213 202 -58 141 -107 -30 -116 171 -214 74 8" echo -n "Mine: " -./push_swap $ARGS |./checker $ARGS +./push_swap $ARGS | valgrind --track-origins=yes ./checker $ARGS echo -n "Their: " ./push_swap $ARGS | ./Push-Swap-Tester/checker_linux $ARGS diff --git a/src.list b/src.list index 2015edd..e78502d 100644 --- a/src.list +++ b/src.list @@ -16,5 +16,6 @@ app/run_with_items app/sort2 app/sort3 app/sort5 +app/state app/target bonus/move1 diff --git a/src/app/main.c b/src/app/main.c index f6c0d0f..e234e3e 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/09 15:50:10 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 17:42:03 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,40 +20,18 @@ #include "me/vec/vec_i64_bool.h" #include #include + #if BONUS == 1 -// # 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); -} +# define ERR_INVALID_NUM "Error\n" +# define ERR_DUPLICATE "Error\n" #else -// # 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); -} +//# define ERR_INVALID_NUM "Error:\nInvalid Number\n" +//# define ERR_DUPLICATE "Error:\nDuplicate Number\n" +# define ERR_INVALID_NUM "Error\n" +# define ERR_DUPLICATE "Error\n" #endif @@ -63,61 +41,6 @@ void sort_5(t_state *state); bool is_sorted(t_vec_i64 *v); void run_with_items(t_state *state); -bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) -{ - return (lhs->value <= rhs->value); -} - -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) -{ - t_i64 last; - t_usize index; - - if (state->sorted.len == 0) - return (false); - index = 1; - last = state->sorted.buffer[0].value; - while (index < state->sorted.len) - { - if (last == state->sorted.buffer[index].value) - return (true); - last = state->sorted.buffer[index++].value; - } - return (false); -} - -t_state parses_arguments(t_usize count, t_str nums[]) -{ - t_state state; - t_i32 atoi; - t_usize i; - - state.stack_a = vec_i64_new(count, NULL); - state.stack_b = vec_i64_new(count, NULL); - state.sorted = vec_i64_bool_new(count, NULL); - i = 0; - while (i < count) - { - 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}); - i++; - } - vec_i64_bool_sort(&state.sorted, sort_i64_bool); - if (duplicate_check(&state)) - (free_state(state), me_eprintf(ERR_DUPLICATE), exit(1)); - return (state); -} - int main_normal(t_i32 argc, t_str argv[]) { t_state state; diff --git a/src/app/main_bonus.c b/src/app/main_bonus.c index 3968e34..4de3a02 100644 --- a/src/app/main_bonus.c +++ b/src/app/main_bonus.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/08 18:59:33 by maiboyer #+# #+# */ -/* Updated: 2024/02/10 18:55:20 by maiboyer ### ########.fr */ +/* Updated: 2024/02/15 17:47:33 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,6 @@ #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, @@ -28,6 +26,16 @@ 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); +bool does_match_strings(t_const_str in, t_const_str s1, t_const_str s2) +{ + bool res; + + res = (str_n_compare(in, s1, 4) == 0); + if (s2 != NULL) + res = (res || (str_n_compare(in, s2, 4) == 0)); + return (res); +} + t_error handle_operation(t_buffer_str s, t_state *state) { t_usize op; @@ -35,67 +43,62 @@ t_error handle_operation(t_buffer_str s, t_state *state) op = 0; if (s.buf == NULL) return (ERROR); - if (str_n_compare(s.buf, "pa", 2) == 0) + if (does_match_strings(s.buf, "pa", NULL)) push_inner(&state->stack_a, &state->stack_b, NULL, (op++, NULL)); - if (str_n_compare(s.buf, "pb", 2) == 0) + if (does_match_strings(s.buf, "pb", NULL)) 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) + if (does_match_strings(s.buf, "sa", "ss")) 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) + if (does_match_strings(s.buf, "sb", "ss")) 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) + if (does_match_strings(s.buf, "ra", "rr")) 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) + if (does_match_strings(s.buf, "rb", "rr")) 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) + if (does_match_strings(s.buf, "rra", "rrr")) 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) + if (does_match_strings(s.buf, "rrb", "rrr")) rev_rotate_inner(&state->stack_b, NULL, (op++, NULL)); if (op == 0) return (ERROR); return (NO_ERROR); } +void handle_end(t_state state, t_buffer_str s) +{ + 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); +} + int main_checker(t_i32 argc, t_str argv[]) { t_state state; t_buffer_str s; bool err; - t_usize count; (void)(argc--, argv++); + s.buf = ""; err = false; state = parses_arguments(argc, argv); - s = get_next_line(0, &err); - count = 1; - while (!err && s.buf != NULL) + while (!err) { + s = get_next_line(0, &err); + if (err) + break ; if (s.len != 0 && s.buf[s.len - 1] == '\n') str_pop(&s); - // me_printf("line[%u]: '%s'\n", count, s.buf); + if (s.buf[0] == '\0') + continue ; if (handle_operation(s, &state)) - { - me_printf("KO: error\n"); - me_printf("ERROR[%u] = '%s'\n", count, s.buf); - (str_free(s), free_state(state)); - return (1); - } - str_free(s); - s = get_next_line(0, &err); - count++; + return (me_printf("KO\n"), str_free(s), free_state(state), 1); + if (s.buf != NULL) + str_free(s); } - str_free(s); - if (is_sorted(&state.stack_a) && state.stack_b.len == 0) - me_printf("OK\n"); - else - me_printf("KO: Not sorted\n"); - free_state(state); + handle_end(state, s); return (0); } diff --git a/src/app/state.c b/src/app/state.c new file mode 100644 index 0000000..7d5e2dc --- /dev/null +++ b/src/app/state.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* state.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */ +/* Updated: 2024/02/15 17:42:13 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/state.h" +#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 "Error\n" +# define ERR_DUPLICATE "Error\n" + +#else + +//# define ERR_INVALID_NUM "Error:\nInvalid Number\n" +//# define ERR_DUPLICATE "Error:\nDuplicate Number\n" +# define ERR_INVALID_NUM "Error\n" +# define ERR_DUPLICATE "Error\n" + +#endif + +bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs) +{ + return (lhs->value <= rhs->value); +} + +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) +{ + t_i64 last; + t_usize index; + + if (state->sorted.len == 0) + return (false); + index = 1; + last = state->sorted.buffer[0].value; + while (index < state->sorted.len) + { + if (last == state->sorted.buffer[index].value) + return (true); + last = state->sorted.buffer[index++].value; + } + return (false); +} + +t_state parses_arguments(t_usize count, t_str nums[]) +{ + t_state state; + t_i32 atoi; + t_usize i; + + state.stack_a = vec_i64_new(count, NULL); + state.stack_b = vec_i64_new(count, NULL); + state.sorted = vec_i64_bool_new(count, NULL); + i = 0; + while (i < count) + { + 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}); + i++; + } + vec_i64_bool_sort(&state.sorted, sort_i64_bool); + if (duplicate_check(&state)) + (free_state(state), me_eprintf(ERR_DUPLICATE), exit(1)); + return (state); +}