108 lines
3.3 KiB
C
108 lines
3.3 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* best_move.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */
|
|
/* Updated: 2024/01/29 22:41:28 by maiboyer ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "app/best_move.h"
|
|
#include "app/best_move_inner.h"
|
|
#include "app/find_place.h"
|
|
#include "app/rotate.h"
|
|
#include "app/state.h"
|
|
#include "app/target.h"
|
|
#include "me/types.h"
|
|
#include "me/vec/vec_i64.h"
|
|
|
|
t_usize min_iter_zero_pos(t_vec_i64 *vec);
|
|
t_usize max_iter_zero_pos(t_vec_i64 *vec);
|
|
|
|
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)
|
|
{
|
|
if (lhs > rhs)
|
|
return (rhs);
|
|
else
|
|
return (lhs);
|
|
}
|
|
|
|
static void find_least_move(t_rotation *main, t_rotation *other)
|
|
{
|
|
t_usize main_fliped;
|
|
t_usize other_fliped;
|
|
t_usize none_fliped;
|
|
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);
|
|
minimum = min(none_fliped, min(main_fliped, other_fliped));
|
|
if (minimum == none_fliped)
|
|
return;
|
|
else if (minimum == main_fliped)
|
|
*main = flip(*main);
|
|
else if (minimum == other_fliped)
|
|
*other = flip(*other);
|
|
}
|
|
|
|
static void run_func_with_best_rotate_for_item_inner(
|
|
t_state *state, t_usize index, t_best_move_args_but_better data)
|
|
{
|
|
t_usize other_size_len_min_1;
|
|
t_usize target_index;
|
|
t_rotation rotate_main;
|
|
t_rotation rotate_other;
|
|
|
|
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;
|
|
rotate_main = target(0, index, data.main_stack->len);
|
|
rotate_other = target(target_index, 0, data.other_stack->len);
|
|
}
|
|
void run_func_with_best_rotate_for_item(t_state *state, t_usize index,
|
|
t_best_move_args data)
|
|
{
|
|
t_vec_i64 *main_stack;
|
|
t_vec_i64 *other_stack;
|
|
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);
|
|
}
|