From ed0c78d8a299701274a28bf3f3bbcd4b6428b5b0 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Thu, 8 Feb 2024 14:27:45 +0100 Subject: [PATCH] everything in src|include should pass the norminette now --- Makefile | 6 +- flake.lock | 43 +++++++- flake.nix | 13 ++- include/app/best_index_to_move.h | 30 ++++++ include/app/best_move.h | 38 +++---- include/app/best_move_inner.h | 44 ++++++-- include/app/find_iter.h | 10 +- include/app/find_place.h | 14 +-- include/app/iter_state.h | 8 +- include/app/moves.h | 1 - include/app/rotate.h | 38 ++++--- include/app/state.h | 34 +++---- include/app/target.h | 8 +- include/app/types/type_i64_bool.h | 10 +- include/me/convert/str_to_numbers.h | 24 ++--- mecstd | 2 +- src.list | 1 + src/app/best_index_to_move.c | 77 ++++++++++++++ src/app/best_move.c | 118 +++++++-------------- src/app/do_move.c | 23 ++--- src/app/find_place.c | 14 +-- src/app/iter_find.c | 12 +-- src/app/main.c | 34 +++---- src/app/moves.c | 21 ++-- src/app/moves/push.c | 12 +-- src/app/moves/rev_rotate.c | 14 +-- src/app/moves/rotate.c | 14 +-- src/app/moves/swap.c | 16 +-- src/app/rotate.c | 1 - src/app/run_with_items.c | 153 +++++++++------------------- src/app/target.c | 4 +- 31 files changed, 453 insertions(+), 384 deletions(-) create mode 100644 include/app/best_index_to_move.h create mode 100644 src/app/best_index_to_move.c diff --git a/Makefile b/Makefile index 780fe88..c8ff40e 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/03 13:20:01 by maiboyer #+# #+# # -# Updated: 2024/01/11 14:14:03 by maiboyer ### ########.fr # +# Updated: 2024/02/08 14:25:56 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -27,8 +27,8 @@ endif NAME = push_swap LIB_NAME ?= TARGET = $(NAME) -CC = clang -CFLAGS += -Wall -Wextra -Werror -g2 -lme -L$(BUILD_DIR) -Wno-unused-command-line-argument -MMD +CC ?= clang +CFLAGS += -Wall -Werror -Wextra -g2 -lme -L$(BUILD_DIR) -Wno-unused-command-line-argument -MMD BONUS_FILES = LIBS_NAME = mecstd SUBJECT_URL = 'https://cdn.intra.42.fr/pdf/pdf/118610/en.subject.pdf' diff --git a/flake.lock b/flake.lock index 1589801..d2e09c7 100644 --- a/flake.lock +++ b/flake.lock @@ -108,6 +108,21 @@ "type": "github" } }, + "flake-utils_5": { + "locked": { + "lastModified": 1659877975, + "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, "generic_c": { "inputs": { "flake-utils": "flake-utils_3", @@ -147,6 +162,27 @@ "type": "github" } }, + "nixGL": { + "inputs": { + "flake-utils": "flake-utils_5", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1685908677, + "narHash": "sha256-E4zUPEUFyVWjVm45zICaHRpfGepfkE9Z2OECV9HXfA4=", + "owner": "guibou", + "repo": "nixGL", + "rev": "489d6b095ab9d289fe11af0219a9ff00fe87c7c5", + "type": "github" + }, + "original": { + "owner": "guibou", + "repo": "nixGL", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1700108881, @@ -209,11 +245,11 @@ }, "nixpkgs_5": { "locked": { - "lastModified": 1706683685, - "narHash": "sha256-FtPPshEpxH/ewBOsdKBNhlsL2MLEFv1hEnQ19f/bFsQ=", + "lastModified": 1706925685, + "narHash": "sha256-hVInjWMmgH4yZgA4ZtbgJM1qEAel72SYhP5nOWX4UIM=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "5ad9903c16126a7d949101687af0aa589b1d7d3d", + "rev": "79a13f1437e149dc7be2d1290c74d378dad60814", "type": "github" }, "original": { @@ -226,6 +262,7 @@ "c_formatter_42": "c_formatter_42", "flake-utils": "flake-utils_2", "generic_c": "generic_c", + "nixGL": "nixGL", "nixpkgs": "nixpkgs_5" } }, diff --git a/flake.nix b/flake.nix index 24419ec..3a31d1f 100644 --- a/flake.nix +++ b/flake.nix @@ -5,6 +5,8 @@ flake-utils.url = "github:numtide/flake-utils"; generic_c.url = "github:maix0/generic_c"; c_formatter_42.url = "github:maix0/c_formatter_42-flake"; + nixGL.url = "github:guibou/nixGL"; + nixGL.inputs.nixpkgs.follows = "nixpkgs"; }; outputs = { self, @@ -12,10 +14,14 @@ flake-utils, generic_c, c_formatter_42, + nixGL, }: flake-utils.lib.eachDefaultSystem ( system: let - pkgs = nixpkgs.legacyPackages.${system}; + pkgs = import nixpkgs { + inherit system; + overlays = [nixGL.overlay]; + }; in { devShell = pkgs.mkShell { packages = [ @@ -24,14 +30,15 @@ pkgs.clang pkgs.clang-tools pkgs.norminette - generic_c.packages.${system}.default - c_formatter_42.packages.${system}.default pkgs.poppler_utils pkgs.minilibx pkgs.valgrind pkgs.libbsd pkgs.tree pkgs.fastmod + generic_c.packages.${system}.default + c_formatter_42.packages.${system}.default + pkgs.nixgl.nixGLIntel ]; }; } diff --git a/include/app/best_index_to_move.h b/include/app/best_index_to_move.h new file mode 100644 index 0000000..53e5ed7 --- /dev/null +++ b/include/app/best_index_to_move.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_index_to_move.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/08 14:18:43 by maiboyer #+# #+# */ +/* Updated: 2024/02/08 14:20:01 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BEST_INDEX_TO_MOVE_H +# define BEST_INDEX_TO_MOVE_H + +# include "app/best_move.h" +# include "app/state.h" +# include "me/types.h" +# include "me/vec/vec_i64.h" + +struct s_best_index_to_move +{ + t_state *state; + t_vec_i64 *from; + t_vec_i64 *to; + t_iter_pos_func f; + t_usize i; +}; + +#endif /* BEST_INDEX_TO_MOVE_H */ diff --git a/include/app/best_move.h b/include/app/best_move.h index afb748f..f78fb2b 100644 --- a/include/app/best_move.h +++ b/include/app/best_move.h @@ -6,36 +6,38 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 20:10:21 by maiboyer #+# #+# */ -/* Updated: 2024/02/02 22:48:02 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 13:30:30 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef BEST_MOVE_H -#define BEST_MOVE_H +# define BEST_MOVE_H -#include "app/state.h" -#include "me/vec/vec_i64.h" +# include "app/state.h" +# include "me/vec/vec_i64.h" -struct s_functions +struct s_functions { - void (*forward)(void *); - void (*reverse)(void *); + void (*forward)(void *); + void (*reverse)(void *); }; -typedef t_usize (*t_iter_pos_func)(t_vec_i64 *); +typedef t_usize (*t_iter_pos_func)(t_vec_i64 *); typedef struct s_best_move_args { - t_iter_pos_func iter_func; - t_usize index; - t_vec_i64 *from; - t_vec_i64 *to; - void *args; - struct s_functions main; - struct s_functions other; - struct s_functions both; -} t_best_move_args; + t_iter_pos_func iter_func; + t_usize index; + t_vec_i64 *from; + t_vec_i64 *to; + void *args; + struct s_functions main; + struct s_functions other; + struct s_functions both; + bool print_stuff; +} t_best_move_args; -void run_func_with_best_rotate_for_item(t_state *state, t_best_move_args data); +void run_func_with_best_rotate_for_item(t_state *state, + t_best_move_args data); #endif /* BEST_MOVE_H */ diff --git a/include/app/best_move_inner.h b/include/app/best_move_inner.h index c767e7a..902c125 100644 --- a/include/app/best_move_inner.h +++ b/include/app/best_move_inner.h @@ -6,23 +6,23 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 21:31:42 by maiboyer #+# #+# */ -/* Updated: 2024/02/02 22:47:44 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 14:22:49 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef BEST_MOVE_INNER_H -#define BEST_MOVE_INNER_H +# define BEST_MOVE_INNER_H -#include "app/best_move.h" -#include "app/rotate.h" -#include "app/state.h" -#include "me/types.h" -#include "me/vec/vec_i64.h" +# include "app/best_move.h" +# include "app/rotate.h" +# include "app/state.h" +# include "me/types.h" +# include "me/vec/vec_i64.h" -typedef void (*t_banana_func)(void *); +typedef void (*t_banana_func)(void *); -static inline t_banana_func choose_func(struct s_functions *funcs, - t_rotation rotate) +static inline t_banana_func choose_func(struct s_functions *funcs, + t_rotation rotate) { if (rotate.direction == FORWARD) return (funcs->forward); @@ -31,4 +31,28 @@ static inline t_banana_func choose_func(struct s_functions *funcs, return (NULL); } +static inline t_isize abs_diff(t_isize lhs, t_isize rhs) +{ + if (lhs > rhs) + return (lhs - rhs); + else + return (rhs - lhs); +} + +static inline t_isize min(t_isize lhs, t_isize rhs) +{ + if (lhs > rhs) + return (rhs); + else + return (lhs); +} + +static inline t_isize max(t_isize lhs, t_isize rhs) +{ + if (lhs < rhs) + return (rhs); + else + return (lhs); +} + #endif /* BEST_MOVE_INNER_H */ diff --git a/include/app/find_iter.h b/include/app/find_iter.h index 0f445c6..ff9b460 100644 --- a/include/app/find_iter.h +++ b/include/app/find_iter.h @@ -11,12 +11,12 @@ /* ************************************************************************** */ #ifndef FIND_ITER_H -#define FIND_ITER_H +# define FIND_ITER_H -#include "me/types.h" -#include "me/vec/vec_i64.h" +# include "me/types.h" +# include "me/vec/vec_i64.h" -t_usize min_iter_zero_pos(t_vec_i64 *vec); -t_usize max_iter_zero_pos(t_vec_i64 *vec); +t_usize min_iter_zero_pos(t_vec_i64 *vec); +t_usize max_iter_zero_pos(t_vec_i64 *vec); #endif /* FIND_ITER_H */ diff --git a/include/app/find_place.h b/include/app/find_place.h index 00484f6..234e7b1 100644 --- a/include/app/find_place.h +++ b/include/app/find_place.h @@ -11,19 +11,19 @@ /* ************************************************************************** */ #ifndef FIND_PLACE_H -#define FIND_PLACE_H +# define FIND_PLACE_H -#include "app/state.h" -#include "me/types.h" +# include "app/state.h" +# include "me/types.h" typedef struct s_find_place_iter_state { - t_usize current_index; - t_usize found_index; + t_usize current_index; + t_usize found_index; t_i64 to_find_elem; -} t_find_place_iter_state; +} t_find_place_iter_state; -t_usize find_place(t_i64 elem, t_state *state); +t_usize find_place(t_i64 elem, t_state *state); #endif /* FIND_PLACE_H */ diff --git a/include/app/iter_state.h b/include/app/iter_state.h index 8eb554d..acc14be 100644 --- a/include/app/iter_state.h +++ b/include/app/iter_state.h @@ -11,14 +11,14 @@ /* ************************************************************************** */ #ifndef ITER_STATE_H -#define ITER_STATE_H +# define ITER_STATE_H -#include "me/types.h" +# include "me/types.h" typedef struct s_iter_state { - t_usize pos; + t_usize pos; t_i64 elem; -} t_iter_state; +} t_iter_state; #endif /* ITER_STATE_H */ diff --git a/include/app/moves.h b/include/app/moves.h index b2f6efc..db9b4f7 100644 --- a/include/app/moves.h +++ b/include/app/moves.h @@ -13,7 +13,6 @@ #ifndef MOVES_H # define MOVES_H - # include "app/state.h" # include "app/types/type_move.h" # include "me/types.h" diff --git a/include/app/rotate.h b/include/app/rotate.h index 1d44033..f393485 100644 --- a/include/app/rotate.h +++ b/include/app/rotate.h @@ -6,53 +6,51 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 19:00:18 by maiboyer #+# #+# */ -/* Updated: 2024/02/02 22:42:31 by maiboyer ### ########.fr */ +/* Updated: 2024/02/08 13:33:09 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef ROTATE_H -#define ROTATE_H +# define ROTATE_H -#include "me/types.h" +# include "me/types.h" -enum e_rotation_direction +enum e_rotation_direction { FORWARD, REVERSE, }; typedef struct s_rotation { - t_usize value; - t_usize ring_size; - enum e_rotation_direction direction; -} t_rotation; + t_isize value; + t_isize ring_size; + enum e_rotation_direction direction; +} t_rotation; -static inline t_rotation forward(t_usize by, t_usize ring_size) +static inline t_rotation forward(t_isize by, t_isize ring_size) { - return ((t_rotation){ - .value = by, .ring_size = ring_size, .direction = FORWARD}); + return ((t_rotation){.value = by, .ring_size = ring_size, + .direction = FORWARD}); } -static inline t_rotation reverse(t_usize by, t_usize ring_size) +static inline t_rotation reverse(t_isize by, t_isize ring_size) { - return ((t_rotation){ - .value = by, .ring_size = ring_size, .direction = REVERSE}); + return ((t_rotation){.value = by, .ring_size = ring_size, + .direction = REVERSE}); } -static inline t_rotation flip(t_rotation rot) +static inline t_rotation flip(t_rotation rot) { - enum e_rotation_direction flipped; + enum e_rotation_direction flipped; if (rot.direction == FORWARD) flipped = REVERSE; else if (rot.direction == REVERSE) flipped = FORWARD; - else + else return (rot); - return ((t_rotation){.value = rot.ring_size - rot.value, - .ring_size = rot.ring_size, - .direction = flipped}); + .ring_size = rot.ring_size, .direction = flipped}); } #endif /* ROTATE_H */ diff --git a/include/app/state.h b/include/app/state.h index 63c630a..f05612a 100644 --- a/include/app/state.h +++ b/include/app/state.h @@ -11,25 +11,25 @@ /* ************************************************************************** */ #ifndef STATE_H -#define STATE_H +# define STATE_H -#include "me/types.h" -#include "me/vec/vec_i64.h" -#include "me/vec/vec_i64_bool.h" +# include "me/types.h" +# include "me/vec/vec_i64.h" +# include "me/vec/vec_i64_bool.h" typedef struct s_state { - t_vec_i64_bool sorted; - t_vec_i64 stack_a; - t_vec_i64 stack_b; -} t_state; + t_vec_i64_bool sorted; + t_vec_i64 stack_a; + t_vec_i64 stack_b; +} t_state; -t_state parses_arguments(t_usize count, t_str nums[]); -void free_state(t_state state); +t_state parses_arguments(t_usize count, t_str nums[]); +void free_state(t_state state); -static inline void make_sorted_true_for_elem(t_state *s, t_i64 elem) +static inline void make_sorted_true_for_elem(t_state *s, t_i64 elem) { - t_usize i; + t_usize i; i = 0; while (i < s->sorted.len) @@ -37,15 +37,15 @@ static inline void make_sorted_true_for_elem(t_state *s, t_i64 elem) if (s->sorted.buffer[i].value == elem) { s->sorted.buffer[i].active = true; - return; + return ; } i++; } } -static inline void make_sorted_true_from_stack(t_state *s, t_vec_i64 *stack) +static inline void make_sorted_true_from_stack(t_state *s, t_vec_i64 *stack) { - t_usize i; + t_usize i; i = 0; while (i < stack->len) @@ -54,9 +54,9 @@ static inline void make_sorted_true_from_stack(t_state *s, t_vec_i64 *stack) } } -static inline void make_sorted_all_false(t_state *s) +static inline void make_sorted_all_false(t_state *s) { - t_usize i; + t_usize i; i = 0; while (i < s->sorted.len) diff --git a/include/app/target.h b/include/app/target.h index 6cee43b..204044e 100644 --- a/include/app/target.h +++ b/include/app/target.h @@ -11,11 +11,11 @@ /* ************************************************************************** */ #ifndef TARGET_H -#define TARGET_H +# define TARGET_H -#include "app/rotate.h" -#include "me/types.h" +# include "app/rotate.h" +# include "me/types.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); #endif /* TARGET_H */ diff --git a/include/app/types/type_i64_bool.h b/include/app/types/type_i64_bool.h index 58ffce1..48af825 100644 --- a/include/app/types/type_i64_bool.h +++ b/include/app/types/type_i64_bool.h @@ -11,14 +11,14 @@ /* ************************************************************************** */ #ifndef TYPE_I64_BOOL_H -#define TYPE_I64_BOOL_H +# define TYPE_I64_BOOL_H -#include "me/types.h" +# include "me/types.h" typedef struct s_i64_bool { - t_i64 value; - bool active; -} t_i64_bool; + t_i64 value; + bool active; +} t_i64_bool; #endif /* TYPE_I64_BOOL_H */ diff --git a/include/me/convert/str_to_numbers.h b/include/me/convert/str_to_numbers.h index 180ad5c..9f90ddf 100644 --- a/include/me/convert/str_to_numbers.h +++ b/include/me/convert/str_to_numbers.h @@ -11,20 +11,20 @@ /* ************************************************************************** */ #ifndef STR_TO_NUMBERS_H -#define STR_TO_NUMBERS_H +# define STR_TO_NUMBERS_H -#include "me/types.h" +# include "me/types.h" -t_error str_to_isize(t_const_str str, t_u32 radix, t_isize *out); -t_error str_to_i64(t_const_str str, t_u32 radix, t_i64 *out); -t_error str_to_i32(t_const_str str, t_u32 radix, t_i32 *out); -t_error str_to_i16(t_const_str str, t_u32 radix, t_i16 *out); -t_error str_to_i8(t_const_str str, t_u32 radix, t_i8 *out); +t_error str_to_isize(t_const_str str, t_u32 radix, t_isize *out); +t_error str_to_i64(t_const_str str, t_u32 radix, t_i64 *out); +t_error str_to_i32(t_const_str str, t_u32 radix, t_i32 *out); +t_error str_to_i16(t_const_str str, t_u32 radix, t_i16 *out); +t_error str_to_i8(t_const_str str, t_u32 radix, t_i8 *out); -t_error str_to_usize(t_const_str str, t_u32 radix, t_usize *out); -t_error str_to_u64(t_const_str str, t_u32 radix, t_u64 *out); -t_error str_to_u32(t_const_str str, t_u32 radix, t_u32 *out); -t_error str_to_u16(t_const_str str, t_u32 radix, t_u16 *out); -t_error str_to_u8(t_const_str str, t_u32 radix, t_u8 *out); +t_error str_to_usize(t_const_str str, t_u32 radix, t_usize *out); +t_error str_to_u64(t_const_str str, t_u32 radix, t_u64 *out); +t_error str_to_u32(t_const_str str, t_u32 radix, t_u32 *out); +t_error str_to_u16(t_const_str str, t_u32 radix, t_u16 *out); +t_error str_to_u8(t_const_str str, t_u32 radix, t_u8 *out); #endif /* STR_TO_NUMBERS_H */ diff --git a/mecstd b/mecstd index f4c3ed9..4cb7e19 160000 --- a/mecstd +++ b/mecstd @@ -1 +1 @@ -Subproject commit f4c3ed9f01ed41b9579b3881fde43fe2a8b5993e +Subproject commit 4cb7e19ed8aaa0c910f38fb6947f91b363556b4c diff --git a/src.list b/src.list index 8a1324a..3b26f15 100644 --- a/src.list +++ b/src.list @@ -1,3 +1,4 @@ +app/best_index_to_move app/best_move app/do_move app/find_place diff --git a/src/app/best_index_to_move.c b/src/app/best_index_to_move.c new file mode 100644 index 0000000..1b37701 --- /dev/null +++ b/src/app/best_index_to_move.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* best_index_to_move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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); +} diff --git a/src/app/best_move.c b/src/app/best_move.c index 0343954..2bbbfdf 100644 --- a/src/app/best_move.c +++ b/src/app/best_move.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 -/*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); -} diff --git a/src/app/do_move.c b/src/app/do_move.c index 23f0e49..96b06b1 100644 --- a/src/app/do_move.c +++ b/src/app/do_move.c @@ -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); diff --git a/src/app/find_place.c b/src/app/find_place.c index 11d93ab..1a3efd6 100644 --- a/src/app/find_place.c +++ b/src/app/find_place.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/src/app/iter_find.c b/src/app/iter_find.c index 474e9cc..bd6d1cf 100644 --- a/src/app/iter_find.c +++ b/src/app/iter_find.c @@ -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); diff --git a/src/app/main.c b/src/app/main.c index 7bf85d4..deaf59f 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 -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); } diff --git a/src/app/moves.c b/src/app/moves.c index d1bc31c..1eef164 100644 --- a/src/app/moves.c +++ b/src/app/moves.c @@ -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"); diff --git a/src/app/moves/push.c b/src/app/moves/push.c index a9ecd7f..6646044 100644 --- a/src/app/moves/push.c +++ b/src/app/moves/push.c @@ -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"); } diff --git a/src/app/moves/rev_rotate.c b/src/app/moves/rev_rotate.c index 642c5d0..74de1c1 100644 --- a/src/app/moves/rev_rotate.c +++ b/src/app/moves/rev_rotate.c @@ -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"); diff --git a/src/app/moves/rotate.c b/src/app/moves/rotate.c index 51afccc..7249a63 100644 --- a/src/app/moves/rotate.c +++ b/src/app/moves/rotate.c @@ -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); diff --git a/src/app/moves/swap.c b/src/app/moves/swap.c index 4b90c5d..730e4a6 100644 --- a/src/app/moves/swap.c +++ b/src/app/moves/swap.c @@ -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"); diff --git a/src/app/rotate.c b/src/app/rotate.c index e2f2ed3..33c6652 100644 --- a/src/app/rotate.c +++ b/src/app/rotate.c @@ -12,4 +12,3 @@ #include "app/rotate.h" #include "me/types.h" - diff --git a/src/app/run_with_items.c b/src/app/run_with_items.c index eab39a6..45ed8df 100644 --- a/src/app/run_with_items.c +++ b/src/app/run_with_items.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 -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"); } diff --git a/src/app/target.c b/src/app/target.c index 1fc1b55..1444454 100644 --- a/src/app/target.c +++ b/src/app/target.c @@ -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++;