**.a
**.o
+pushswap
+checker
/* By: agilliar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/02 15:07:21 by agilliar #+# #+# */
-/* Updated: 2025/12/02 16:41:27 by agilliar ### ########.fr */
+/* Updated: 2025/12/02 23:19:25 by agilliar ### ########.fr */
/* */
/* ************************************************************************** */
exit(errcode);
}
+// I present to you: "null garbage collector" allocator
+// When your program is on a device intended to explode in 5 seconds,
+// you do not need to worry about frees, the world is your garbage collector
+//
+// Since our program has a bounded amount of allocations per run, and no
+// allocations are used as scrath working space, we would not need to free
+// except on program termination.
+// The OS is an excellent garbage collector, however some consider memory leaks
+// errors.
+//
+// We just keep a in-allocation linked list that gets automatically freed on
+// exit. The only requirement is that `cheatexit` be called in all termination
+// paths.
void *cheatalloc(size_t len)
{
void **new;
/* By: agilliar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/02 11:02:13 by agilliar #+# #+# */
-/* Updated: 2025/12/02 17:37:32 by agilliar ### ########.fr */
+/* Updated: 2025/12/02 23:05:13 by agilliar ### ########.fr */
/* */
/* ************************************************************************** */
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* pushswap.c :+: :+: :+: */
+/* main_pushswap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: agilliar <agilliar@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/02 22:15:12 by agilliar #+# #+# */
-/* Updated: 2025/12/02 22:16:49 by agilliar ### ########.fr */
+/* Updated: 2025/12/02 23:15:12 by agilliar ### ########.fr */
/* */
/* ************************************************************************** */
int main(int argc, char **argv)
{
- (void) argc;
- (void) argv;
+ t_stacks stacks;
+
+ stacks = stacks_new();
+ for (int i = 1; i < argc; i++)
+ stacks_insert_init(&stacks, psval_parse(argv[i]));
+ for (int i = 1; i < argc; i++)
+ psval_output(psval_parse(argv[i]));
+ output_flush();
+ cheatexit(!stacks_is_solved(&stacks));
}
/* By: agilliar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/02 16:47:00 by agilliar #+# #+# */
-/* Updated: 2025/12/02 22:11:55 by agilliar ### ########.fr */
+/* Updated: 2025/12/02 22:42:38 by agilliar ### ########.fr */
/* */
/* ************************************************************************** */
store->written = 0;
}
-static void output_char(char c)
+void output_char(char c)
{
t_output_store *store;
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* psval.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: agilliar <agilliar@student.42mulhouse.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2025/12/02 22:35:19 by agilliar #+# #+# */
+/* Updated: 2025/12/02 22:54:24 by agilliar ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "pushswap.h"
+
+static void psval_output_no_sign(t_psval val)
+{
+ if (val > 9 || val < -9)
+ psval_output_no_sign(val / 10);
+ val %= 10;
+ if (val < 0)
+ val = -val;
+ output_char("0123456789"[val]);
+}
+
+void psval_output(t_psval val)
+{
+ if (val < 0)
+ output_char('-');
+ psval_output_no_sign(val);
+}
+
+t_psval psval_parse(const char *s)
+{
+ t_psval sign;
+ t_psval res;
+
+ sign = 1;
+ res = 0;
+ if (*s == '-')
+ {
+ s++;
+ sign = -1;
+ }
+ while (*s)
+ {
+ if (*s < '0' || *s > '9')
+ cheatexit(1);
+ if (__builtin_mul_overflow(res, 10, &res))
+ cheatexit(1);
+ if (__builtin_add_overflow(res, sign * (*s - '0'), &res))
+ cheatexit(1);
+ s++;
+ }
+ return (res);
+}
/* By: agilliar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/02 11:02:44 by agilliar #+# #+# */
-/* Updated: 2025/12/02 18:40:21 by agilliar ### ########.fr */
+/* Updated: 2025/12/02 23:05:01 by agilliar ### ########.fr */
/* */
/* ************************************************************************** */
t_closure ops[11];
} t_ops;
-void cheatexit(int errcode);
-void *cheatalloc(size_t len);
+void cheatexit(int errcode);
+void *cheatalloc(size_t len);
-t_clist *clist_new(t_psval val);
+t_clist *clist_new(t_psval val);
// Positive numbers are forward, which wraps back to end of stack
-t_psval clist_get_at(const t_clist *list, int i);
-t_clist *clist_pop(t_clist **src);
-// *dst: : Nullable
-void clist_push(t_clist **dst, t_clist *lst);
-// *dst: : Nullable
-void clist_push_back(t_clist **dst, t_clist *lst);
-
-void output_str(char *s);
-void output_str_ln(char *s);
+t_psval clist_get_at(const t_clist *list, int i);
+t_clist *clist_pop(t_clist **src);
+// *dst : Nullable
+void clist_push(t_clist **dst, t_clist *lst);
+// *dst : Nullable
+void clist_push_back(t_clist **dst, t_clist *lst);
+
+void output_char(char c);
+void output_str(char *s);
+void output_str_ln(char *s);
+void output_flush(void);
+
+t_stacks stacks_new(void);
+void stacks_insert_init(t_stacks *stacks, t_psval val);
+bool stacks_is_solved(const t_stacks *stacks);
+void stacks_apply(t_stacks *stacks, const t_ops *ops, t_algorithm algo);
+
+t_psval psval_parse(const char *s);
+void psval_output(t_psval val);
#endif
/* By: agilliar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/02 15:57:41 by agilliar #+# #+# */
-/* Updated: 2025/12/02 22:17:14 by agilliar ### ########.fr */
+/* Updated: 2025/12/02 22:25:54 by agilliar ### ########.fr */
/* */
/* ************************************************************************** */
stacks->a_len++;
}
-bool stack_is_solved(const t_stacks *stacks)
+bool stacks_is_solved(const t_stacks *stacks)
{
size_t i;