From f98cc2c30e13e22e1ef8257d9a5f18be5efc7026 Mon Sep 17 00:00:00 2001 From: Maix0 Date: Wed, 7 Feb 2024 14:46:49 +0100 Subject: [PATCH] update --- include/app/best_move.h | 34 ++++------ include/app/best_move_inner.h | 19 +----- include/app/rotate.h | 15 +++-- src/app/best_move.c | 117 +++++++++++++++++++--------------- src/app/do_move.c | 42 +----------- src/app/find_place.c | 4 +- src/app/main.c | 6 +- src/app/moves/push.c | 26 ++++---- src/app/moves/rev_rotate.c | 27 ++++---- src/app/moves/rotate.c | 21 +++--- src/app/moves/swap.c | 16 +++-- src/app/run_with_items.c | 116 +++++++++++++++++++++------------ src/app/target.c | 8 +-- 13 files changed, 224 insertions(+), 227 deletions(-) diff --git a/include/app/best_move.h b/include/app/best_move.h index d0e075b..afb748f 100644 --- a/include/app/best_move.h +++ b/include/app/best_move.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 20:10:21 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 14:10:08 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 22:48:02 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,18 +14,7 @@ #define BEST_MOVE_H #include "app/state.h" - -enum e_stack_selector -{ - STACK_A, - STACK_B, -}; - -enum e_zero_position -{ - MIN_ZERO_POS, - MAX_ZERO_POS, -}; +#include "me/vec/vec_i64.h" struct s_functions { @@ -33,17 +22,20 @@ struct s_functions void (*reverse)(void *); }; +typedef t_usize (*t_iter_pos_func)(t_vec_i64 *); + typedef struct s_best_move_args { - enum e_stack_selector main_stack; - enum e_zero_position zero_pos; - void *args; - struct s_functions main; - struct s_functions other; - struct s_functions both; + t_iter_pos_func iter_func; + t_usize index; + t_vec_i64 *from; + t_vec_i64 *to; + void *args; + struct s_functions main; + struct s_functions other; + struct s_functions both; } t_best_move_args; -void run_func_with_best_rotate_for_item(t_state *state, t_usize index, - t_best_move_args data); +void run_func_with_best_rotate_for_item(t_state *state, t_best_move_args data); #endif /* BEST_MOVE_H */ diff --git a/include/app/best_move_inner.h b/include/app/best_move_inner.h index 3c419dc..c767e7a 100644 --- a/include/app/best_move_inner.h +++ b/include/app/best_move_inner.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 21:31:42 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 14:12:25 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 22:47:44 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,23 +20,6 @@ #include "me/vec/vec_i64.h" typedef void (*t_banana_func)(void *); -typedef t_usize (*t_iter_pos_func)(t_vec_i64 *); - -enum e_best_move_stack_selector -{ - MAIN, - OTHER, - BOTH, -}; - -typedef struct s_best_move_args_but_better -{ - t_best_move_args data; - t_vec_i64 *main_stack; - t_vec_i64 *other_stack; - t_iter_pos_func iter_func; - -} t_best_move_args_but_better; static inline t_banana_func choose_func(struct s_functions *funcs, t_rotation rotate) diff --git a/include/app/rotate.h b/include/app/rotate.h index fa930ed..1d44033 100644 --- a/include/app/rotate.h +++ b/include/app/rotate.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 19:00:18 by maiboyer #+# #+# */ -/* Updated: 2024/01/29 19:27:19 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 22:42:31 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,30 +22,33 @@ enum e_rotation_direction }; typedef struct s_rotation { - t_usize value; - t_usize ring_size; + t_usize value; + t_usize ring_size; enum e_rotation_direction direction; } t_rotation; static inline t_rotation forward(t_usize by, t_usize ring_size) { return ((t_rotation){ - .value = by % ring_size, .ring_size = ring_size, .direction = FORWARD}); + .value = by, .ring_size = ring_size, .direction = FORWARD}); } static inline t_rotation reverse(t_usize by, t_usize ring_size) { return ((t_rotation){ - .value = by % ring_size, .ring_size = ring_size, .direction = REVERSE}); + .value = by, .ring_size = ring_size, .direction = REVERSE}); } static inline t_rotation flip(t_rotation rot) { enum e_rotation_direction flipped; - flipped = FORWARD; if (rot.direction == FORWARD) flipped = REVERSE; + else if (rot.direction == REVERSE) + flipped = FORWARD; + else + return (rot); return ((t_rotation){.value = rot.ring_size - rot.value, .ring_size = rot.ring_size, diff --git a/src/app/best_move.c b/src/app/best_move.c index c23fabc..0343954 100644 --- a/src/app/best_move.c +++ b/src/app/best_move.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 14:25:00 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 23:01:46 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,14 +19,16 @@ #include "app/target.h" #include "me/types.h" #include "me/vec/vec_i64.h" +#include -static inline t_usize abs_diff(t_usize lhs, t_usize rhs) +/*static inline t_usize abs_diff(t_usize lhs, t_usize rhs) { if (lhs > rhs) return (lhs - rhs); else return (rhs - lhs); } +*/ static inline t_usize min(t_usize lhs, t_usize rhs) { @@ -35,8 +37,24 @@ static inline t_usize min(t_usize lhs, t_usize rhs) else return (lhs); } +static inline t_usize max(t_usize lhs, t_usize rhs) +{ + if (lhs < rhs) + return (rhs); + else + return (lhs); +} -static void find_least_move(t_rotation *main, t_rotation *other) +static inline t_usize move_count(t_rotation lhs, t_rotation rhs) +{ + if (lhs.direction == rhs.direction) + return (max(lhs.value, rhs.value)); + else + return (lhs.value + rhs.value); +} + +// static +void find_least_move(t_rotation *main, t_rotation *other) { t_usize main_fliped; t_usize other_fliped; @@ -44,85 +62,80 @@ static void find_least_move(t_rotation *main, t_rotation *other) t_rotation tmp; t_usize minimum; - none_fliped = main->value + other->value; - tmp = flip(*main); - main_fliped = abs_diff(tmp.value, other->value); - tmp = flip(*other); - other_fliped = abs_diff(tmp.value, main->value); + // return; + none_fliped = move_count(*main, *other); + main_fliped = move_count(flip(*main), *other); + other_fliped = move_count(*main, flip(*other)); minimum = min(none_fliped, min(main_fliped, other_fliped)); if (minimum == none_fliped) return; else if (minimum == main_fliped) - *main = flip(*main); + { + tmp = flip(*main); + *main = tmp; + } else if (minimum == other_fliped) - *other = flip(*other); + { + tmp = flip(*other); + *other = tmp; + } } +#include "me/printf/printf.h" -static void run_func_with_best_rotate_for_item_inner( - t_state *state, t_usize index, t_best_move_args_but_better data) +static void run_func_with_best_rotate_for_item_inner(t_state *state, + t_best_move_args data) { - t_usize other_size_len_min_1; t_usize target_index; t_rotation main; t_rotation other; t_usize i; - other_size_len_min_1 = data.other_stack->len; - if (other_size_len_min_1 == 0) - other_size_len_min_1++; - target_index = - (find_place(data.main_stack->buffer[index], state) + - (data.other_stack->len - data.iter_func(data.other_stack))) % - other_size_len_min_1; - main = target(0, index, data.main_stack->len); - other = target(target_index, 0, data.other_stack->len); - find_least_move(&main, &other); + target_index = (find_place(data.from->buffer[data.index], state) + + (data.to->len - data.iter_func(data.to))) % + max(data.to->len, 1); + main = target(0, data.index, data.from->len); + other = target(0, target_index, data.to->len); + if (main.direction != other.direction) + find_least_move(&main, &other); if (main.direction == other.direction) { i = 0; while (i++ < min(main.value, other.value)) - choose_func(&data.data.both, main)(data.data.args); + { + // me_eprintf("both\n"); + choose_func(&data.both, main)(data.args); + } i = 0; if (main.value > other.value) while (i++ < main.value - other.value) - choose_func(&data.data.main, main)(data.data.args); + { + // me_eprintf("main\n"); + choose_func(&data.main, main)(data.args); + } else while (i++ < other.value - main.value) - choose_func(&data.data.other, other)(data.data.args); + { + // me_eprintf("other\n"); + choose_func(&data.other, other)(data.args); + } } else { i = 0; while (i++ < main.value) - choose_func(&data.data.main, main)(data.data.args); + { + // me_eprintf("main\n"); + choose_func(&data.main, main)(data.args); + } i = 0; while (i++ < other.value) - choose_func(&data.data.other, other)(data.data.args); + { + // me_eprintf("other\n"); + choose_func(&data.other, other)(data.args); + } } } -void run_func_with_best_rotate_for_item(t_state *state, t_usize index, - t_best_move_args data) +void run_func_with_best_rotate_for_item(t_state *state, t_best_move_args data) { - t_best_move_args_but_better better_args; - - if (data.zero_pos == MIN_ZERO_POS) - better_args.iter_func = min_iter_zero_pos; - else if (data.zero_pos == MAX_ZERO_POS) - better_args.iter_func = max_iter_zero_pos; - else - return; - if (data.main_stack == STACK_A) - { - better_args.main_stack = &state->stack_a; - better_args.other_stack = &state->stack_b; - } - else if (data.main_stack == STACK_B) - { - better_args.main_stack = &state->stack_b; - better_args.other_stack = &state->stack_a; - } - else - return; - better_args.data = data; - run_func_with_best_rotate_for_item_inner(state, index, better_args); + run_func_with_best_rotate_for_item_inner(state, data); } diff --git a/src/app/do_move.c b/src/app/do_move.c index 756123a..23f0e49 100644 --- a/src/app/do_move.c +++ b/src/app/do_move.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/31 14:25:57 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 15:11:24 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 22:54:12 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,43 +28,3 @@ void rev_rotate_a(void *s); void rev_rotate_b(void *s); void rev_rotate_both(void *s); -void do_sort_insert(t_state *state, t_usize index, - enum e_stack_selector move_from, - enum e_zero_position zero_pos) -{ - void (*pop_move)(void *); - t_vec_i64 *pushed_stack; - struct s_functions main; - struct s_functions other; - - if (move_from == STACK_A) - { - pushed_stack = &state->stack_b; - pop_move = push_b; - main.forward = rev_rotate_a; - main.reverse = rotate_a; - other.forward = rev_rotate_b; - other.reverse = rotate_b; - } - else - { - pushed_stack = &state->stack_a; - pop_move = push_a; - main.forward = rev_rotate_b; - main.reverse = rotate_b; - other.forward = rev_rotate_a; - other.reverse = rotate_a; - } - run_func_with_best_rotate_for_item( - state, index, - (t_best_move_args){ - .zero_pos = zero_pos, - .main_stack = move_from, - .args = state, - .both = {.forward = rev_rotate_both, .reverse = rotate_both}, - .main = main, - .other = other, - }); - (pop_move)(state); - make_sorted_true_for_elem(state, pushed_stack->buffer[0]); -} diff --git a/src/app/find_place.c b/src/app/find_place.c index 7636ac4..11d93ab 100644 --- a/src/app/find_place.c +++ b/src/app/find_place.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 22:01:12 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 14:13:34 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 23:00:09 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ static void find_place_iter(t_usize index, t_i64_bool *elem, t_find_place_iter_state *state) { (void)(index); - if (!elem->active) + if (!(elem->active || elem->value == state->to_find_elem)) return; if (elem->value == state->to_find_elem) state->found_index = state->current_index; diff --git a/src/app/main.c b/src/app/main.c index a1f14c2..7bf85d4 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/02 01:05:32 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 22:06:51 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,7 +66,7 @@ int main(t_i32 argc, t_str argv[]) run_with_items(&state); for (t_usize i = 0; i < state.stack_a.len; i++) { - ft_printf("%i\n", state.stack_a.buffer[i]); + //me_eprintf("%i\n", state.stack_a.buffer[i]); } - (void)(state); + free_state(state); } diff --git a/src/app/moves/push.c b/src/app/moves/push.c index cd6c296..a9ecd7f 100644 --- a/src/app/moves/push.c +++ b/src/app/moves/push.c @@ -6,32 +6,34 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:22:54 by maiboyer #+# #+# */ -/* Updated: 2024/01/11 17:17:45 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 21:40:17 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ - #include "app/state.h" +#include "me/printf/printf.h" #include "me/vec/vec_i64.h" -static inline void push_inner(t_vec_i64 *to, t_vec_i64 *from, - t_const_str tag) +static inline void push_inner(t_vec_i64 *to, t_vec_i64 *from, t_const_str tag, + t_const_str print) { - t_i64 e; + t_i64 e; (void)(tag); if (from->len == 0) - return ; - vec_i64_pop(from, &e); - vec_i64_push(to, e); + return; + vec_i64_pop_front(from, &e); + vec_i64_push_front(to, e); + if (print) + ft_printf("%s\n", print); } -void push_a(t_state *s) +void push_a(t_state *s) { - push_inner(&s->stack_a, &s->stack_b, "Push A"); + push_inner(&s->stack_a, &s->stack_b, "Push A", "pa"); } -void push_b(t_state *s) +void push_b(t_state *s) { - push_inner(&s->stack_b, &s->stack_a, "Push B"); + push_inner(&s->stack_b, &s->stack_a, "Push B", "pb"); } diff --git a/src/app/moves/rev_rotate.c b/src/app/moves/rev_rotate.c index 29ae711..642c5d0 100644 --- a/src/app/moves/rev_rotate.c +++ b/src/app/moves/rev_rotate.c @@ -6,36 +6,39 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 14:57:21 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 14:12:23 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ - #include "app/state.h" +#include "me/printf/printf.h" -static inline void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag) +static inline void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag, + t_const_str print) { - t_i64 e; + t_i64 e; (void)(tag); if (stack->len <= 1) - return ; + return; vec_i64_pop(stack, &e); vec_i64_push_front(stack, e); + if (print) + ft_printf("%s\n", print); } -void rev_rotate_a(t_state *s) +void rev_rotate_a(t_state *s) { - rev_rotate_inner(&s->stack_a, "RevRotate A"); + rev_rotate_inner(&s->stack_a, "RevRotate A", "rra"); } -void rev_rotate_b(t_state *s) +void rev_rotate_b(t_state *s) { - rev_rotate_inner(&s->stack_b, "RevRotate B"); + rev_rotate_inner(&s->stack_b, "RevRotate B", "rrb"); } -void rev_rotate_both(t_state *s) +void rev_rotate_both(t_state *s) { - rev_rotate_inner(&s->stack_a, "RevRotate Both"); - rev_rotate_inner(&s->stack_b, "RevRotate Both"); + rev_rotate_inner(&s->stack_a, "RevRotate Both", NULL); + rev_rotate_inner(&s->stack_b, "RevRotate Both", "rrr"); } diff --git a/src/app/moves/rotate.c b/src/app/moves/rotate.c index 23cb016..51afccc 100644 --- a/src/app/moves/rotate.c +++ b/src/app/moves/rotate.c @@ -6,35 +6,40 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 14:58:10 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 22:34:51 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "app/state.h" +#include "me/printf/printf.h" -static inline void rotate_inner(t_vec_i64 *stack, t_const_str tag) +static inline void rotate_inner(t_vec_i64 *stack, t_const_str tag, + t_const_str print) { t_i64 e; (void)(tag); if (stack->len <= 1) return; - vec_i64_pop(stack, &e); - vec_i64_push_front(stack, e); + vec_i64_pop_front(stack, &e); + vec_i64_push(stack, e); + if (print) + ft_printf("%s\n", print); } void rotate_a(t_state *s) { - rotate_inner(&s->stack_a, "Rotate A"); + rotate_inner(&s->stack_a, "Rotate A", "ra"); } void rotate_b(t_state *s) { - rotate_inner(&s->stack_b, "Rotate B"); + rotate_inner(&s->stack_b, "Rotate B", "rb"); } void rotate_both(t_state *s) { - rotate_inner(&s->stack_a, "Rotate Both"); - rotate_inner(&s->stack_b, "Rotate Both"); + me_eprintf("BOTHHHHHH!!!!"); + rotate_inner(&s->stack_a, "Rotate Both", NULL); + rotate_inner(&s->stack_b, "Rotate Both", "rr"); } diff --git a/src/app/moves/swap.c b/src/app/moves/swap.c index 416abd7..4b90c5d 100644 --- a/src/app/moves/swap.c +++ b/src/app/moves/swap.c @@ -6,14 +6,16 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 16:26:04 by maiboyer #+# #+# */ -/* Updated: 2024/01/31 14:58:32 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 14:14:12 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "app/state.h" +#include "me/printf/printf.h" #include "me/vec/vec_i64.h" -static inline void swap_inner(t_vec_i64 *stack, t_const_str tag) +static inline void swap_inner(t_vec_i64 *stack, t_const_str tag, + t_const_str print) { t_i64 first; t_i64 second; @@ -25,20 +27,22 @@ static inline void swap_inner(t_vec_i64 *stack, t_const_str tag) vec_i64_pop(stack, &second); vec_i64_push(stack, first); vec_i64_push(stack, second); + if (print) + ft_printf("%s\n", print); } void swap_a(t_state *s) { - swap_inner(&s->stack_a, "Swap A"); + swap_inner(&s->stack_a, "Swap A", "sa"); } void swap_b(t_state *s) { - swap_inner(&s->stack_b, "Swap B"); + swap_inner(&s->stack_b, "Swap B", "sb"); } void swap_both(t_state *s) { - swap_inner(&s->stack_a, "Swap Both"); - swap_inner(&s->stack_b, "Swap Both"); + swap_inner(&s->stack_a, "Swap Both", NULL); + swap_inner(&s->stack_b, "Swap Both", "ss"); } diff --git a/src/app/run_with_items.c b/src/app/run_with_items.c index abbb661..eab39a6 100644 --- a/src/app/run_with_items.c +++ b/src/app/run_with_items.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/31 15:12:47 by maiboyer #+# #+# */ -/* Updated: 2024/02/01 20:59:24 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 22:55:08 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ #include "app/rotate.h" #include "app/state.h" #include "app/target.h" +#include "me/printf/printf.h" #include "me/types.h" #include "me/vec/vec_i64.h" #include "me/vec/vec_i64_bool.h" @@ -32,11 +33,7 @@ void rev_rotate_a(void *s); void rev_rotate_b(void *s); void rev_rotate_both(void *s); -void do_sort_insert(t_state *state, t_usize index, - enum e_stack_selector move_from, - enum e_zero_position zero_pos); - -void inc(void *s) +static void inc(void *s) { t_usize *p; @@ -44,49 +41,40 @@ void inc(void *s) *p += 1; } -t_usize best_index_to_move(t_state *state, enum e_stack_selector move_from, - enum e_zero_position zero_pos) +t_usize best_index_to_move(t_state *state, t_vec_i64 *from, t_vec_i64 *to, + t_iter_pos_func f) { - t_vec_i64 *stack; t_usize min_val; + t_usize min_pos; t_usize i; t_usize tmp; - if (move_from == STACK_A) - stack = &state->stack_a; - else - stack = &state->stack_b; - if (stack->len == 0) + if (from->len == 0) return (0); - i = 1; - run_func_with_best_rotate_for_item(state, 0, - (t_best_move_args){ - .args = &tmp, - .zero_pos = zero_pos, - .main_stack = move_from, - .other = {inc, inc}, - .main = {inc, inc}, - .both = {inc, inc}, - }); - min_val = tmp; - while (i < stack->len) + i = 0; + min_val = 0; + min_pos = 0; + while (i < from->len) { - run_func_with_best_rotate_for_item(state, i, - (t_best_move_args){ - .args = &tmp, - .zero_pos = zero_pos, - .main_stack = move_from, - .other = {inc, inc}, - .main = {inc, inc}, - .both = {inc, inc}, - }); + tmp = 1; + run_func_with_best_rotate_for_item(state, (t_best_move_args){ + .index = i, + .args = &tmp, + .iter_func = f, + .from = from, + .to = to, + .other = {inc, inc}, + .main = {inc, inc}, + .both = {inc, inc}, + }); if (tmp < min_val) { min_val = tmp; + min_pos = i; } i++; } - return (min_val); + return (min_pos); } bool is_sorted(t_vec_i64 *v) @@ -102,16 +90,43 @@ bool is_sorted(t_vec_i64 *v) return (true); } +void print_stack(t_const_str msg, t_vec_i64 *stack) +{ + return; + me_eprintf("[%s] = [\n", msg); + + for (t_usize i = 0; i < stack->len; i++) + me_eprintf("\t%i\n", stack->buffer[i]); + me_eprintf("]\n"); +} + void run_with_items(t_state *state) { t_rotation rot; + t_usize idx; if (is_sorted(&state->stack_a)) return; while (state->stack_a.len > state->stack_b.len) { - do_sort_insert(state, best_index_to_move(state, STACK_A, MAX_ZERO_POS), - STACK_A, MAX_ZERO_POS); + idx = best_index_to_move(state, &state->stack_a, &state->stack_b, + &max_iter_zero_pos); + run_func_with_best_rotate_for_item( + state, + (t_best_move_args){ + .index = idx, + .iter_func = &max_iter_zero_pos, + .from = &state->stack_a, + .to = &state->stack_b, + .args = state, + .both = {.forward = rotate_both, .reverse = rev_rotate_both}, + .main = {.forward = rotate_a, .reverse = rev_rotate_a}, + .other = {.forward = rotate_b, .reverse = rev_rotate_b}, + }); + push_b(state); + make_sorted_true_from_stack(state, &state->stack_b); + print_stack("f_stack_a", &state->stack_a); + print_stack("f_stack_b", &state->stack_b); } while (state->stack_a.len != 0) { @@ -121,10 +136,26 @@ void run_with_items(t_state *state) make_sorted_all_false(state); while (state->stack_b.len != 0) { - do_sort_insert(state, best_index_to_move(state, STACK_B, MIN_ZERO_POS), - STACK_B, MIN_ZERO_POS); + idx = best_index_to_move(state, &state->stack_b, &state->stack_a, + &min_iter_zero_pos); + run_func_with_best_rotate_for_item( + state, + (t_best_move_args){ + .index = idx, + .from = &state->stack_b, + .to = &state->stack_a, + .iter_func = &min_iter_zero_pos, + .args = state, + .both = {.forward = rotate_both, .reverse = rev_rotate_both}, + .main = {.forward = rotate_b, .reverse = rev_rotate_b}, + .other = {.forward = rotate_a, .reverse = rev_rotate_a}, + }); + push_a(state); + make_sorted_true_from_stack(state, &state->stack_a); + print_stack("s_stack_a", &state->stack_a); + print_stack("s_stack_b", &state->stack_b); } - rot = target(min_iter_zero_pos(&state->stack_a), 0, state->stack_a.len); + rot = target(0, min_iter_zero_pos(&state->stack_a), state->stack_a.len); if (rot.value > flip(rot).value) rot = flip(rot); while (rot.value > 0) @@ -135,5 +166,6 @@ void run_with_items(t_state *state) rev_rotate_a(state); rot.value--; } - printf("is_sorted: %s\n", is_sorted(&state->stack_a) ? "true" : "false"); + me_eprintf("is_sorted: %s\n", + is_sorted(&state->stack_a) ? "true" : "false"); } diff --git a/src/app/target.c b/src/app/target.c index 26c62e7..1fc1b55 100644 --- a/src/app/target.c +++ b/src/app/target.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 20:30:27 by maiboyer #+# #+# */ -/* Updated: 2024/01/29 22:29:50 by maiboyer ### ########.fr */ +/* Updated: 2024/02/02 22:40:55 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,8 @@ t_rotation target(t_usize from, t_usize to, t_usize ring_size) { if (ring_size == 0) ring_size++; - if (from < to) - return (forward(to - from, ring_size)); - else + if (from > to) return (reverse(from - to, ring_size)); + else + return (forward(to - from, ring_size)); }