stack_manipulation.c \
arg_parsing.c \
stacks_mem.c \
- is_sorted.c \
+ helpers.c \
CHECKSOURCES := $(addprefix $(CHECKSRCDIR)/, $(CHECKSOURCES))
stacks_mem.c \
actions.c \
prints.c \
- is_sorted.c \
+ helpers.c \
prepare.c \
+ find.c \
PUSHSOURCES := $(addprefix $(PUSHSRCDIR)/, $(PUSHSOURCES))
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/24 12:13:51 by ljiriste #+# #+# */
-/* Updated: 2024/02/23 09:48:21 by ljiriste ### ########.fr */
+/* Updated: 2024/02/24 14:06:52 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
void stack_pop(t_stack *s);
int stack_top(t_stack *s);
-int is_sorted(t_stack *s);
-int parse(int argc, char **argv, t_stack *s);
-
+int is_sorted(t_stack *s);
+int parse(int argc, char **argv, t_stack *s);
void prepare_stacks(t_stacks *s, size_t ind_a, size_t ind_b);
size_t cost(t_stacks *s, const size_t b_plus, const size_t a_plus);
+size_t find_best(t_stacks *s, t_vec *target_a_indeces);
+size_t find_target_ind(t_stack *stack, int val);
+size_t find_minind(t_stack *s);
#endif //CHECKER_H
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* find.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/02/24 11:37:22 by ljiriste #+# #+# */
+/* Updated: 2024/02/24 14:10:54 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "checker.h"
+#include "libft.h"
+#include <limits.h>
+#include <stddef.h>
+
+size_t find_minind(t_stack *s)
+{
+ size_t minind;
+ size_t i;
+ int min;
+
+ min = INT_MAX;
+ i = 0;
+ while (i < s->stack.size)
+ {
+ if (min >= stack_top(s))
+ {
+ minind = i;
+ min = stack_top(s);
+ }
+ stack_rotate(s, 1);
+ ++i;
+ }
+ return (minind);
+}
+
+static int max_or_min(t_stack *stack, int val)
+{
+ int max;
+ int min;
+ size_t i;
+
+ i = 0;
+ max = INT_MIN;
+ min = INT_MAX;
+ while (i < stack->stack.size)
+ {
+ if (stack_top(stack) > max)
+ {
+ max = stack_top(stack);
+ }
+ if (stack_top(stack) < min)
+ {
+ min = stack_top(stack);
+ }
+ stack_rotate(stack, 1);
+ ++i;
+ }
+ return (val < min || val > max);
+}
+
+size_t find_target_ind(t_stack *stack, int val)
+{
+ size_t i;
+ size_t res;
+ const int minind = find_minind(stack);
+
+ i = 0;
+ if (max_or_min(stack, val))
+ return (minind);
+ while (i < stack->stack.size && val < stack_top(stack))
+ {
+ stack_rotate(stack, 1);
+ ++i;
+ }
+ while (i < stack->stack.size && val > stack_top(stack))
+ {
+ stack_rotate(stack, 1);
+ ++i;
+ }
+ res = i;
+ while (i < stack->stack.size)
+ {
+ stack_rotate(stack, 1);
+ ++i;
+ }
+ return (res);
+}
+
+size_t find_best(t_stacks *s, t_vec *target_a_indeces)
+{
+ size_t res_ind;
+ size_t cur;
+ size_t best;
+ size_t i;
+
+ best = SIZE_MAX;
+ i = 0;
+ while (i < s->b.stack.size)
+ {
+ cur = cost(s, i, *(size_t *)ft_vec_access(target_a_indeces, i));
+ if (cur < best)
+ {
+ res_ind = i;
+ best = cur;
+ }
+ ++i;
+ }
+ return (res_ind);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* helpers.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/02/22 14:22:08 by ljiriste #+# #+# */
+/* Updated: 2024/02/24 14:09:17 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "checker.h"
+#include "libft.h"
+#include <stddef.h>
+
+int is_sorted(t_stack *s)
+{
+ const size_t start_ind = s->ind;
+ int prev;
+ int cur;
+
+ prev = stack_top(s);
+ stack_rotate(s, 1);
+ while (s->ind != start_ind)
+ {
+ cur = stack_top(s);
+ if (cur <= prev)
+ {
+ s->ind = start_ind;
+ return (0);
+ }
+ prev = cur;
+ stack_rotate(s, 1);
+ }
+ return (1);
+}
+
+// Let's split the stacks by the index of the current value
+//
+// a b
+// | |
+// | a+ |
+// | |
+// | | b+
+// --- |
+// | |
+// | |
+// | |
+// | a- ---
+// | |
+// | | b-
+// | |
+//
+// Either we have to stack_rotate both in the same direction
+// in which case the cost is the higher of those
+// - max(a+, b+) or max(a-, b-)
+// Or we have to stack_rotate them in the opposite direction
+// in which case we have to add the moves
+// - a+ + b- or a- + b+
+//
+// The minimum cost is the lowest of these four values
+size_t cost(t_stacks *s, const size_t b_plus, const size_t a_plus)
+{
+ size_t res;
+ const size_t a_minus = s->a.stack.size - a_plus;
+ const size_t b_minus = s->b.stack.size - b_plus;
+
+ res = ft_maxs(a_plus, b_plus);
+ res = ft_mins(res, ft_maxs(a_minus, b_minus));
+ res = ft_mins(res, a_plus + b_minus);
+ res = ft_mins(res, a_minus + b_plus);
+ return (res);
+}
+++ /dev/null
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* is_sorted.c :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2024/02/22 14:22:08 by ljiriste #+# #+# */
-/* Updated: 2024/02/23 10:58:13 by ljiriste ### ########.fr */
-/* */
-/* ************************************************************************** */
-
-#include "checker.h"
-#include <stddef.h>
-
-int is_sorted(t_stack *s)
-{
- const size_t start_ind = s->ind;
- int prev;
- int cur;
-
- prev = stack_top(s);
- stack_rotate(s, 1);
- while (s->ind != start_ind)
- {
- cur = stack_top(s);
- if (cur <= prev)
- {
- s->ind = start_ind;
- return (0);
- }
- prev = cur;
- stack_rotate(s, 1);
- }
- return (1);
-}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/23 09:35:45 by ljiriste #+# #+# */
-/* Updated: 2024/02/23 09:54:39 by ljiriste ### ########.fr */
+/* Updated: 2024/02/24 11:29:15 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
i = 0;
while (i < ft_mins(rot_a, rot_b))
{
- print_rotate(s, both);
+ print_rotate(s, both);
++i;
}
if (rot_a > rot_b)
i = 0;
while (i < ft_mins(rot_a, rot_b))
{
- print_reverse_rotate(s, both);
+ print_reverse_rotate(s, both);
++i;
}
if (rot_a > rot_b)
if (c == ft_maxs(ind_a, ind_b))
prepare_plus_together(s, ind_a, ind_b);
else if (c == ft_maxs(s->a.stack.size - ind_a, s->b.stack.size - ind_b))
- prepare_minus_together(s, s->a.stack.size - ind_a, s->b.stack.size - ind_b);
+ prepare_minus_together(s, s->a.stack.size - ind_a,
+ s->b.stack.size - ind_b);
else if (c == ind_a + (s->b.stack.size - ind_b))
prepare_a_plus_against(s, ind_a, s->b.stack.size - ind_b);
else
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/24 13:17:32 by ljiriste #+# #+# */
-/* Updated: 2024/02/24 11:15:29 by ljiriste ### ########.fr */
+/* Updated: 2024/02/24 14:10:54 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include "checker.h"
#include "libft.h"
#include <limits.h>
+#include <stddef.h>
void a_sort_three_or_less(t_stacks *s)
{
last = stack_top(&s->a);
max = INT_MIN;
steps_to_smaller = 0;
- while (i < s->a.stack.size)
+ while (i < s->a.stack.size)
{
stack_rotate(&s->a, 1);
steps_to_smaller += (stack_top(&s->a) < last);
return ;
}
-size_t find_minind(t_stack *s)
-{
- size_t minind;
- size_t i;
- int min;
-
- min = INT_MAX;
- i = 0;
- while (i < s->stack.size)
- {
- if (min >= stack_top(s))
- {
- minind = i;
- min = stack_top(s);
- }
- stack_rotate(s, 1);
- ++i;
- }
- return (minind);
-}
-
-int max_or_min(t_stack *stack, int val)
-{
- int max;
- int min;
- size_t i;
-
- i = 0;
- max = INT_MIN;
- min = INT_MAX;
- while (i < stack->stack.size)
- {
- if (stack_top(stack) > max)
- {
- max = stack_top(stack);
- }
- if (stack_top(stack) < min)
- {
- min = stack_top(stack);
- }
- stack_rotate(stack, 1);
- ++i;
- }
- return (val < min || val > max);
-}
-
-size_t find_target_ind(t_stack *stack, int val)
-{
- size_t i;
- size_t res;
- const int minind = find_minind(stack);
-
- i = 0;
- if (max_or_min(stack, val))
- return (minind);
- while (i < stack->stack.size && val < stack_top(stack))
- {
- stack_rotate(stack, 1);
- ++i;
- }
- while (i < stack->stack.size && val > stack_top(stack))
- {
- stack_rotate(stack, 1);
- ++i;
- }
- res = i;
- while (i < stack->stack.size)
- {
- stack_rotate(stack, 1);
- ++i;
- }
- return (res);
-}
-
-// Let's split the stacks by the index of the current value
-//
-// a b
-// | |
-// | a+ |
-// | |
-// | | b+
-// --- |
-// | |
-// | |
-// | |
-// | a- ---
-// | |
-// | | b-
-// | |
-//
-// Either we have to stack_rotate both in the same direction
-// in which case the cost is the higher of those
-// - max(a+, b+) or max(a-, b-)
-// Or we have to stack_rotate them in the opposite direction
-// in which case we have to add the moves
-// - a+ + b- or a- + b+
-//
-// The minimum cost is the lowest of these four values
-size_t cost(t_stacks *s, const size_t b_plus, const size_t a_plus)
-{
- size_t res;
- const size_t a_minus = s->a.stack.size - a_plus;
- const size_t b_minus = s->b.stack.size - b_plus;
-
- res = ft_maxs(a_plus, b_plus);
- res = ft_mins(res, ft_maxs(a_minus, b_minus));
- res = ft_mins(res, a_plus + b_minus);
- res = ft_mins(res, a_minus + b_plus);
- return (res);
-}
-
-size_t find_best(t_stacks *s, t_vec *target_a_indeces)
-{
- size_t res_ind;
- size_t cur;
- size_t best;
- size_t i;
-
- best = SIZE_MAX;
- i = 0;
- while (i < s->b.stack.size)
- {
- cur = cost(s, i, *(size_t *)ft_vec_access(target_a_indeces, i));
- if (cur < best)
- {
- res_ind = i;
- best = cur;
- }
- ++i;
- }
- return (res_ind);
-}
-
void get_push_indeces(t_stacks *s, int *ind_a, int *ind_b)
{
t_vec target_a_indeces;