everything in src|include should pass the norminette now

This commit is contained in:
Maieul BOYER 2024-02-08 14:27:45 +01:00
parent 3ab3a384c3
commit ed0c78d8a2
No known key found for this signature in database
31 changed files with 453 additions and 384 deletions

View file

@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* best_index_to_move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/08 14:14:54 by maiboyer #+# #+# */
/* Updated: 2024/02/08 14:21:51 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "app/best_index_to_move.h"
#include "app/best_move.h"
#include "app/find_iter.h"
#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"
#include <stdio.h>
static void inc(void *s)
{
t_usize *p;
p = (t_usize *)s;
*p += 1;
}
static inline t_usize count_move_for_index(struct s_best_index_to_move d)
{
t_usize tmp;
tmp = 0;
run_func_with_best_rotate_for_item(d.state,
(t_best_move_args){
.index = d.i,
.args = &tmp,
.iter_func = d.f,
.from = d.from,
.to = d.to,
.other = {inc, inc},
.main = {inc, inc},
.both = {inc, inc},
});
return (tmp);
}
t_usize best_index_to_move(t_state *state, t_vec_i64 *from, t_vec_i64 *to,
t_iter_pos_func f)
{
t_usize min_val;
t_usize min_pos;
t_usize i;
t_usize tmp;
if (from->len == 0)
return (0);
i = 0;
min_val = ~0;
min_pos = 0;
while (i < from->len)
{
tmp = count_move_for_index((struct s_best_index_to_move){state, from,
to, f, i});
if (tmp < min_val)
{
min_val = tmp;
min_pos = i;
}
i++;
}
return (min_pos);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/29 20:04:33 by maiboyer #+# #+# */
/* Updated: 2024/02/02 23:01:46 by maiboyer ### ########.fr */
/* Updated: 2024/02/08 14:22:55 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,54 +21,28 @@
#include "me/vec/vec_i64.h"
#include <stdio.h>
/*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 inline t_usize max(t_usize lhs, t_usize rhs)
{
if (lhs < rhs)
return (rhs);
else
return (lhs);
}
static inline t_usize move_count(t_rotation lhs, t_rotation rhs)
static inline t_isize 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);
return (abs_diff(lhs.value, rhs.value));
}
// static
void find_least_move(t_rotation *main, t_rotation *other)
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;
t_isize main_fliped;
t_isize other_fliped;
t_isize none_fliped;
t_rotation tmp;
t_isize minimum;
// 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;
return ;
else if (minimum == main_fliped)
{
tmp = flip(*main);
@ -80,62 +54,46 @@ void find_least_move(t_rotation *main, t_rotation *other)
*other = tmp;
}
}
#include "me/printf/printf.h"
static void run_func_with_best_rotate_for_item_inner(t_state *state,
t_best_move_args data)
static inline void func(t_best_move_args *data, t_rotation *main,
t_rotation *other)
{
t_usize target_index;
t_rotation main;
t_rotation other;
t_usize i;
t_isize i;
target_index = (find_place(data.from->buffer[data.index], state) +
(data.to->len - data.iter_func(data.to))) %
max(data.to->len, 1);
i = 0;
while (i++ < min(main->value, other->value))
choose_func(&data->both, *main)(data->args);
i = 0;
if (main->value > other->value)
while (i++ < main->value - other->value)
choose_func(&data->main, *main)(data->args);
else
while (i++ < other->value - main->value)
choose_func(&data->other, *other)(data->args);
}
void run_func_with_best_rotate_for_item(t_state *state,
t_best_move_args data)
{
t_isize target_index;
t_rotation main;
t_rotation other;
t_isize i;
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);
other = target(target_index, 0, data.to->len);
find_least_move(&main, &other);
if (main.direction == other.direction)
{
i = 0;
while (i++ < min(main.value, other.value))
{
// me_eprintf("both\n");
choose_func(&data.both, main)(data.args);
}
i = 0;
if (main.value > other.value)
while (i++ < main.value - other.value)
{
// me_eprintf("main\n");
choose_func(&data.main, main)(data.args);
}
else
while (i++ < other.value - main.value)
{
// me_eprintf("other\n");
choose_func(&data.other, other)(data.args);
}
}
func(&data, &main, &other);
else
{
i = 0;
while (i++ < main.value)
{
// me_eprintf("main\n");
choose_func(&data.main, main)(data.args);
}
i = 0;
while (i++ < other.value)
{
// me_eprintf("other\n");
choose_func(&data.other, other)(data.args);
}
}
}
void run_func_with_best_rotate_for_item(t_state *state, t_best_move_args data)
{
run_func_with_best_rotate_for_item_inner(state, data);
}

View file

@ -16,15 +16,14 @@
#include "me/types.h"
#include "me/vec/vec_i64.h"
void push_a(void *s);
void push_b(void *s);
void swap_a(void *s);
void swap_b(void *s);
void swap_both(void *s);
void rotate_a(void *s);
void rotate_b(void *s);
void rotate_both(void *s);
void rev_rotate_a(void *s);
void rev_rotate_b(void *s);
void rev_rotate_both(void *s);
void push_a(void *s);
void push_b(void *s);
void swap_a(void *s);
void swap_b(void *s);
void swap_both(void *s);
void rotate_a(void *s);
void rotate_b(void *s);
void rotate_both(void *s);
void rev_rotate_a(void *s);
void rev_rotate_b(void *s);
void rev_rotate_both(void *s);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/29 22:01:12 by maiboyer #+# #+# */
/* Updated: 2024/02/02 23:00:09 by maiboyer ### ########.fr */
/* Updated: 2024/02/08 14:06:05 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,25 +14,25 @@
#include "app/types/type_i64_bool.h"
#include "me/vec/vec_i64_bool.h"
static void find_place_iter(t_usize index, t_i64_bool *elem,
t_find_place_iter_state *state)
static void find_place_iter(t_usize index, t_i64_bool *elem,
t_find_place_iter_state *state)
{
(void)(index);
if (!(elem->active || elem->value == state->to_find_elem))
return;
return ;
if (elem->value == state->to_find_elem)
state->found_index = state->current_index;
state->current_index++;
}
t_usize find_place(t_i64 elem, t_state *state)
t_usize find_place(t_i64 elem, t_state *state)
{
t_find_place_iter_state iter_state;
t_find_place_iter_state iter_state;
iter_state.current_index = 0;
iter_state.found_index = 0;
iter_state.to_find_elem = elem;
vec_i64_bool_iter(&state->sorted, (void(*))find_place_iter, &iter_state);
vec_i64_bool_iter(&state->sorted, (void (*)())find_place_iter, &iter_state);
return (iter_state.found_index);
}

View file

@ -14,7 +14,7 @@
#include "me/types.h"
#include "me/vec/vec_i64.h"
static void iter_min(t_usize index, t_i64 *elem, t_iter_state *state)
static void iter_min(t_usize index, t_i64 *elem, t_iter_state *state)
{
if (*elem < state->elem)
{
@ -23,7 +23,7 @@ static void iter_min(t_usize index, t_i64 *elem, t_iter_state *state)
}
}
static void iter_max(t_usize index, t_i64 *elem, t_iter_state *state)
static void iter_max(t_usize index, t_i64 *elem, t_iter_state *state)
{
if (*elem > state->elem)
{
@ -32,9 +32,9 @@ static void iter_max(t_usize index, t_i64 *elem, t_iter_state *state)
}
}
t_usize min_iter_zero_pos(t_vec_i64 *vec)
t_usize min_iter_zero_pos(t_vec_i64 *vec)
{
t_iter_state state;
t_iter_state state;
if (vec->len == 0)
return (0);
@ -44,9 +44,9 @@ t_usize min_iter_zero_pos(t_vec_i64 *vec)
return (state.pos);
}
t_usize max_iter_zero_pos(t_vec_i64 *vec)
t_usize max_iter_zero_pos(t_vec_i64 *vec)
{
t_iter_state state;
t_iter_state state;
if (vec->len == 0)
return (0);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */
/* Updated: 2024/02/02 22:06:51 by maiboyer ### ########.fr */
/* Updated: 2024/02/08 13:50:09 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,23 +19,25 @@
#include "me/vec/vec_i64_bool.h"
#include <stdlib.h>
bool sort_i64_bool(t_i64_bool *lhs, t_i64_bool *rhs)
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)
void free_state(t_state state)
{
vec_i64_free(state.stack_a);
vec_i64_free(state.stack_b);
vec_i64_bool_free(state.sorted);
}
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);
@ -45,28 +47,22 @@ t_state parses_arguments(t_usize count, t_str nums[])
{
if (str_to_i32(nums[i], 10, &atoi))
(free_state(state), me_eprintf("Error:\nInvalid Number\n"),
exit(1));
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);
return (state);
}
void run_with_items(t_state *state);
int main(t_i32 argc, t_str argv[])
int main(t_i32 argc, t_str argv[])
{
t_state state;
t_state state;
(argc--, argv++);
(void)(argc--, argv++);
state = parses_arguments(argc, argv);
run_with_items(&state);
for (t_usize i = 0; i < state.stack_a.len; i++)
{
//me_eprintf("%i\n", state.stack_a.buffer[i]);
}
free_state(state);
}

View file

@ -11,20 +11,19 @@
/* ************************************************************************** */
#include "app/moves.h"
#include "app/state.h"
#include "app/types/type_move.h"
void push_a(t_state *s);
void push_b(t_state *s);
void swap_a(t_state *s);
void swap_b(t_state *s);
void rotate_a(t_state *s);
void rotate_b(t_state *s);
void rev_rotate_a(t_state *s);
void rev_rotate_b(t_state *s);
void push_a(t_state *s);
void push_b(t_state *s);
void swap_a(t_state *s);
void swap_b(t_state *s);
void rotate_a(t_state *s);
void rotate_b(t_state *s);
void rev_rotate_a(t_state *s);
void rev_rotate_b(t_state *s);
void do_move(t_move m, t_state *s)
void do_move(t_move m, t_state *s)
{
if (m & PUSH_A)
push_a(s);
@ -44,7 +43,7 @@ void do_move(t_move m, t_state *s)
rev_rotate_b(s);
}
t_const_str get_str_for_move(t_move m)
t_const_str get_str_for_move(t_move m)
{
if (m & PUSH_A)
return ("pa");

View file

@ -14,26 +14,26 @@
#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,
t_const_str print)
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;
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", "pa");
}
void push_b(t_state *s)
void push_b(t_state *s)
{
push_inner(&s->stack_b, &s->stack_a, "Push B", "pb");
}

View file

@ -13,31 +13,31 @@
#include "app/state.h"
#include "me/printf/printf.h"
static inline void rev_rotate_inner(t_vec_i64 *stack, t_const_str tag,
t_const_str print)
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", "rra");
}
void rev_rotate_b(t_state *s)
void rev_rotate_b(t_state *s)
{
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", NULL);
rev_rotate_inner(&s->stack_b, "RevRotate Both", "rrr");

View file

@ -13,31 +13,31 @@
#include "app/state.h"
#include "me/printf/printf.h"
static inline void rotate_inner(t_vec_i64 *stack, t_const_str tag,
t_const_str print)
static inline void 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_front(stack, &e);
vec_i64_push(stack, e);
if (print)
ft_printf("%s\n", print);
}
void rotate_a(t_state *s)
void rotate_a(t_state *s)
{
rotate_inner(&s->stack_a, "Rotate A", "ra");
}
void rotate_b(t_state *s)
void rotate_b(t_state *s)
{
rotate_inner(&s->stack_b, "Rotate B", "rb");
}
void rotate_both(t_state *s)
void rotate_both(t_state *s)
{
me_eprintf("BOTHHHHHH!!!!");
rotate_inner(&s->stack_a, "Rotate Both", NULL);

View file

@ -14,15 +14,15 @@
#include "me/printf/printf.h"
#include "me/vec/vec_i64.h"
static inline void swap_inner(t_vec_i64 *stack, t_const_str tag,
t_const_str print)
static inline void swap_inner(t_vec_i64 *stack, t_const_str tag,
t_const_str print)
{
t_i64 first;
t_i64 second;
t_i64 first;
t_i64 second;
(void)(tag);
if (stack->len <= 1)
return;
return ;
vec_i64_pop(stack, &first);
vec_i64_pop(stack, &second);
vec_i64_push(stack, first);
@ -31,17 +31,17 @@ static inline void swap_inner(t_vec_i64 *stack, t_const_str tag,
ft_printf("%s\n", print);
}
void swap_a(t_state *s)
void swap_a(t_state *s)
{
swap_inner(&s->stack_a, "Swap A", "sa");
}
void swap_b(t_state *s)
void swap_b(t_state *s)
{
swap_inner(&s->stack_b, "Swap B", "sb");
}
void swap_both(t_state *s)
void swap_both(t_state *s)
{
swap_inner(&s->stack_a, "Swap Both", NULL);
swap_inner(&s->stack_b, "Swap Both", "ss");

View file

@ -12,4 +12,3 @@
#include "app/rotate.h"
#include "me/types.h"

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/31 15:12:47 by maiboyer #+# #+# */
/* Updated: 2024/02/02 22:55:08 by maiboyer ### ########.fr */
/* Updated: 2024/02/08 14:22:15 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,65 +21,46 @@
#include "me/vec/vec_i64_bool.h"
#include <stdio.h>
void push_a(void *s);
void push_b(void *s);
void swap_a(void *s);
void swap_b(void *s);
void swap_both(void *s);
void rotate_a(void *s);
void rotate_b(void *s);
void rotate_both(void *s);
void rev_rotate_a(void *s);
void rev_rotate_b(void *s);
void rev_rotate_both(void *s);
void push_a(void *s);
void push_b(void *s);
void swap_a(void *s);
void swap_b(void *s);
void swap_both(void *s);
void rotate_a(void *s);
void rotate_b(void *s);
void rotate_both(void *s);
void rev_rotate_a(void *s);
void rev_rotate_b(void *s);
void rev_rotate_both(void *s);
static void inc(void *s)
t_usize best_index_to_move(t_state *state, t_vec_i64 *from, t_vec_i64 *to,
t_iter_pos_func f);
static inline struct s_functions funcs( void (*f)(void *), void (*r)(void *))
{
t_usize *p;
p = (t_usize *)s;
*p += 1;
return ((struct s_functions){.forward = f, .reverse = r});
}
t_usize best_index_to_move(t_state *state, t_vec_i64 *from, t_vec_i64 *to,
t_iter_pos_func f)
static inline void banana_first(t_state *state)
{
t_usize min_val;
t_usize min_pos;
t_usize i;
t_usize tmp;
t_usize idx;
if (from->len == 0)
return (0);
i = 0;
min_val = 0;
min_pos = 0;
while (i < from->len)
{
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_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 = \
funcs(rotate_both, rev_rotate_both), .main = funcs(rotate_a, rev_rotate_a), \
.other = funcs(rotate_b, rev_rotate_b), });
push_b(state);
make_sorted_true_from_stack(state, &state->stack_b);
}
bool is_sorted(t_vec_i64 *v)
bool is_sorted(t_vec_i64 *v)
{
t_usize i;
t_usize i;
i = 1;
while (i < v->len)
{
@ -90,71 +71,35 @@ bool is_sorted(t_vec_i64 *v)
return (true);
}
void print_stack(t_const_str msg, t_vec_i64 *stack)
static inline void banana_second(t_state *state)
{
return;
me_eprintf("[%s] = [\n", msg);
t_usize idx;
for (t_usize i = 0; i < stack->len; i++)
me_eprintf("\t%i\n", stack->buffer[i]);
me_eprintf("]\n");
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 = funcs(rotate_both, rev_rotate_both), .main = funcs(rotate_b, \
rev_rotate_b), .other = funcs(rotate_a, rev_rotate_a), });
push_a(state);
make_sorted_true_from_stack(state, &state->stack_a);
}
void run_with_items(t_state *state)
void run_with_items(t_state *state)
{
t_rotation rot;
t_usize idx;
t_rotation rot;
if (is_sorted(&state->stack_a))
return;
return ;
while (state->stack_a.len > state->stack_b.len)
{
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);
}
banana_first(state);
while (state->stack_a.len != 0)
{
push_b(state);
}
vec_i64_bool_reverse(&state->sorted);
make_sorted_all_false(state);
while (state->stack_b.len != 0)
{
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);
}
banana_second(state);
rot = target(0, min_iter_zero_pos(&state->stack_a), state->stack_a.len);
if (rot.value > flip(rot).value)
rot = flip(rot);
@ -166,6 +111,4 @@ void run_with_items(t_state *state)
rev_rotate_a(state);
rot.value--;
}
me_eprintf("is_sorted: %s\n",
is_sorted(&state->stack_a) ? "true" : "false");
}

View file

@ -10,10 +10,10 @@
/* */
/* ************************************************************************** */
#include "app/target.h"
#include "app/rotate.h"
#include "app/target.h"
t_rotation target(t_usize from, t_usize to, t_usize ring_size)
t_rotation target(t_usize from, t_usize to, t_usize ring_size)
{
if (ring_size == 0)
ring_size++;