added stuff

This commit is contained in:
Maix0 2024-02-02 01:06:44 +01:00
parent e0e790c230
commit a08b85d7cc
8 changed files with 281 additions and 32 deletions

72
src/app/main.c Normal file
View file

@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:00:12 by maiboyer #+# #+# */
/* Updated: 2024/02/02 01:05:32 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/types.h"
#include "me/vec/vec_i64.h"
#include "me/vec/vec_i64_bool.h"
#include <stdlib.h>
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);
}
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("Error:\nInvalid Number\n"),
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);
return (state);
}
void run_with_items(t_state *state);
int main(t_i32 argc, t_str argv[])
{
t_state state;
(argc--, argv++);
state = parses_arguments(argc, 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]);
}
(void)(state);
}

View file

@ -6,14 +6,19 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/31 15:12:47 by maiboyer #+# #+# */
/* Updated: 2024/01/31 15:41:41 by maiboyer ### ########.fr */
/* Updated: 2024/02/01 20:59:24 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "app/best_move.h"
#include "app/find_iter.h"
#include "app/rotate.h"
#include "app/state.h"
#include "app/target.h"
#include "me/types.h"
#include "me/vec/vec_i64.h"
#include "me/vec/vec_i64_bool.h"
#include <stdio.h>
void push_a(void *s);
void push_b(void *s);
@ -43,7 +48,6 @@ t_usize best_index_to_move(t_state *state, enum e_stack_selector move_from,
enum e_zero_position zero_pos)
{
t_vec_i64 *stack;
t_usize min_pos;
t_usize min_val;
t_usize i;
t_usize tmp;
@ -55,7 +59,6 @@ t_usize best_index_to_move(t_state *state, enum e_stack_selector move_from,
if (stack->len == 0)
return (0);
i = 1;
min_pos = 0;
run_func_with_best_rotate_for_item(state, 0,
(t_best_move_args){
.args = &tmp,
@ -80,7 +83,6 @@ t_usize best_index_to_move(t_state *state, enum e_stack_selector move_from,
if (tmp < min_val)
{
min_val = tmp;
min_pos = i;
}
i++;
}
@ -102,6 +104,8 @@ bool is_sorted(t_vec_i64 *v)
void run_with_items(t_state *state)
{
t_rotation rot;
if (is_sorted(&state->stack_a))
return;
while (state->stack_a.len > state->stack_b.len)
@ -113,4 +117,23 @@ void run_with_items(t_state *state)
{
push_b(state);
}
vec_i64_bool_reverse(&state->sorted);
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);
}
rot = target(min_iter_zero_pos(&state->stack_a), 0, state->stack_a.len);
if (rot.value > flip(rot).value)
rot = flip(rot);
while (rot.value > 0)
{
if (rot.direction == FORWARD)
rotate_a(state);
else
rev_rotate_a(state);
rot.value--;
}
printf("is_sorted: %s\n", is_sorted(&state->stack_a) ? "true" : "false");
}