]> Untitled Git - axy/ft/pushswap.git/commitdiff
Comments and shenanigans
authorAxy <gilliardmarthey.axel@gmail.com>
Tue, 2 Dec 2025 22:19:31 +0000 (23:19 +0100)
committerAxy <gilliardmarthey.axel@gmail.com>
Tue, 2 Dec 2025 22:19:31 +0000 (23:19 +0100)
.gitignore
a.out [new file with mode: 0755]
cheatalloc.c
clist.c
main_pushswap.c [moved from pushswap.c with 68% similarity]
output.c
psval.c [new file with mode: 0644]
pushswap.h
stacks.c

index 47cb25a2f340ace9b309c5a3da2df682fcaf54b9..612d16d5bf6c294d230d252f4703a16c1d4dd6c3 100644 (file)
@@ -1,2 +1,4 @@
 **.a
 **.o
+pushswap
+checker
diff --git a/a.out b/a.out
new file mode 100755 (executable)
index 0000000..e052e40
Binary files /dev/null and b/a.out differ
index 3696e8962a2a1106c9c53828887466e14639ef8e..46492e60bc0ee437f3497c35a2f7aa969702678a 100644 (file)
@@ -6,7 +6,7 @@
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -43,6 +43,19 @@ void cheatexit(int errcode)
        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;
diff --git a/clist.c b/clist.c
index cbfb0de5d807925e8bf60e341a528cb53885daba..4a78d4b52f1a86a5a26e97878b9304690d712024 100644 (file)
--- a/clist.c
+++ b/clist.c
@@ -6,7 +6,7 @@
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
similarity index 68%
rename from pushswap.c
rename to main_pushswap.c
index 29a20e50b415601fb25abe3809cd3c8b87001e10..9c4970efcf1d65dedf26a3275709ebc55be98705 100644 (file)
@@ -1,12 +1,12 @@
 /* ************************************************************************** */
 /*                                                                            */
 /*                                                        :::      ::::::::   */
-/*   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));
 }
index 4c2235e1276ef7a2e71ef4616ba2657d69434024..c1fed0b8528179259bf84f643e1246495b604982 100644 (file)
--- a/output.c
+++ b/output.c
@@ -6,7 +6,7 @@
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -39,7 +39,7 @@ void  output_flush(void)
        store->written = 0;
 }
 
-static void    output_char(char c)
+void   output_char(char c)
 {
        t_output_store  *store;
 
diff --git a/psval.c b/psval.c
new file mode 100644 (file)
index 0000000..a565da9
--- /dev/null
+++ b/psval.c
@@ -0,0 +1,55 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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);
+}
index 57219e09b647931ca0e589629818577c84b602c1..701536873304e726ca723435a8b20b485847444f 100644 (file)
@@ -6,7 +6,7 @@
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -70,19 +70,29 @@ typedef struct s_ops
        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
index 85844c61c6aae6d8268151e26d93f5b116c463e1..b8a0a8201f46a01abf7f21cec49d647dd066671d 100644 (file)
--- a/stacks.c
+++ b/stacks.c
@@ -6,7 +6,7 @@
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -30,7 +30,7 @@ void  stacks_insert_init(t_stacks *stacks, t_psval val)
        stacks->a_len++;
 }
 
-bool   stack_is_solved(const t_stacks *stacks)
+bool   stacks_is_solved(const t_stacks *stacks)
 {
        size_t  i;