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

54
flake.lock generated
View file

@ -59,11 +59,11 @@
"systems": "systems_2"
},
"locked": {
"lastModified": 1701680307,
"narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
"lastModified": 1705309234,
"narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
"rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
"type": "github"
},
"original": {
@ -77,11 +77,11 @@
"systems": "systems_3"
},
"locked": {
"lastModified": 1701680307,
"narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
"lastModified": 1705309234,
"narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
"rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
"type": "github"
},
"original": {
@ -95,11 +95,11 @@
"systems": "systems_4"
},
"locked": {
"lastModified": 1681202837,
"narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=",
"lastModified": 1705309234,
"narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "cfacdce06f30d2b68473a46042957675eebb3401",
"rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
"type": "github"
},
"original": {
@ -116,11 +116,11 @@
"rust-overlay": "rust-overlay"
},
"locked": {
"lastModified": 1702388541,
"narHash": "sha256-ExzIrROCDRXVnvbOs81aL9DKHm1OxlzVXQPpUTrB75c=",
"lastModified": 1706829609,
"narHash": "sha256-A0imQ9AIJafdL1/+j/1b3G7bm2j+N+VhzTsvKikKjz4=",
"owner": "maix0",
"repo": "generic_c",
"rev": "52b5de885c015d3b84ca26c02c3684ca0e9b426d",
"rev": "a470c2c5a8c8aadc852a7a50d72853f2a3873595",
"type": "github"
},
"original": {
@ -163,11 +163,11 @@
},
"nixpkgs_2": {
"locked": {
"lastModified": 1704842529,
"narHash": "sha256-OTeQA+F8d/Evad33JMfuXC89VMetQbsU4qcaePchGr4=",
"lastModified": 1706683685,
"narHash": "sha256-FtPPshEpxH/ewBOsdKBNhlsL2MLEFv1hEnQ19f/bFsQ=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "eabe8d3eface69f5bb16c18f8662a702f50c20d5",
"rev": "5ad9903c16126a7d949101687af0aa589b1d7d3d",
"type": "github"
},
"original": {
@ -177,11 +177,11 @@
},
"nixpkgs_3": {
"locked": {
"lastModified": 1702272962,
"narHash": "sha256-D+zHwkwPc6oYQ4G3A1HuadopqRwUY/JkMwHz1YF7j4Q=",
"lastModified": 1706683685,
"narHash": "sha256-FtPPshEpxH/ewBOsdKBNhlsL2MLEFv1hEnQ19f/bFsQ=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "e97b3e4186bcadf0ef1b6be22b8558eab1cdeb5d",
"rev": "5ad9903c16126a7d949101687af0aa589b1d7d3d",
"type": "github"
},
"original": {
@ -193,11 +193,11 @@
},
"nixpkgs_4": {
"locked": {
"lastModified": 1681358109,
"narHash": "sha256-eKyxW4OohHQx9Urxi7TQlFBTDWII+F+x2hklDOQPB50=",
"lastModified": 1706487304,
"narHash": "sha256-LE8lVX28MV2jWJsidW13D2qrHU/RUUONendL2Q/WlJg=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "96ba1c52e54e74c3197f4d43026b3f3d92e83ff9",
"rev": "90f456026d284c22b3e3497be980b2e47d0b28ac",
"type": "github"
},
"original": {
@ -209,11 +209,11 @@
},
"nixpkgs_5": {
"locked": {
"lastModified": 1704842529,
"narHash": "sha256-OTeQA+F8d/Evad33JMfuXC89VMetQbsU4qcaePchGr4=",
"lastModified": 1706683685,
"narHash": "sha256-FtPPshEpxH/ewBOsdKBNhlsL2MLEFv1hEnQ19f/bFsQ=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "eabe8d3eface69f5bb16c18f8662a702f50c20d5",
"rev": "5ad9903c16126a7d949101687af0aa589b1d7d3d",
"type": "github"
},
"original": {
@ -235,11 +235,11 @@
"nixpkgs": "nixpkgs_4"
},
"locked": {
"lastModified": 1702347444,
"narHash": "sha256-ueDw7aQf4Xyk69XnDD0YNWDlFdlOgJGPeWFa7uu/cfw=",
"lastModified": 1706753617,
"narHash": "sha256-ZKqTFzhFwSWFEpQTJ0uXnfJBs5Y/po9/8TK4bzssdbs=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "bc13176f27cf3be724d18924b4f6aa47686ca2e3",
"rev": "58be43ae223034217ea1bd58c73210644031b687",
"type": "github"
},
"original": {

View file

@ -1,6 +1,24 @@
convert/str_to_i16
convert/str_to_i16_utils
convert/str_to_i32
convert/str_to_i32_utils
convert/str_to_i64
convert/str_to_i64_utils
convert/str_to_i8
convert/str_to_i8_utils
convert/str_to_u16
convert/str_to_u16_utils
convert/str_to_u32
convert/str_to_u32_utils
convert/str_to_u64
convert/str_to_u64_utils
convert/str_to_u8
convert/str_to_u8_utils
vec/vec_i64
vec/vec_i64_bool
vec/vec_i64_bool_functions2
vec/vec_i64_bool_functions3
vec/vec_i64_bool_sort
vec/vec_i64_functions2
vec/vec_i64_functions3
vec/vec_i64_sort

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_to_numbers.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/01 21:08:13 by maiboyer #+# #+# */
/* Updated: 2024/02/02 00:22:50 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef STR_TO_NUMBERS_H
#define STR_TO_NUMBERS_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_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 */

View file

@ -4,6 +4,7 @@ sources = [
"mecstd/generic_sources/src/vec_C__PREFIX__.c__TEMPLATE__",
"mecstd/generic_sources/src/vec_C__PREFIX___functions2.c__TEMPLATE__",
"mecstd/generic_sources/src/vec_C__PREFIX___functions3.c__TEMPLATE__",
"mecstd/generic_sources/src/vec_C__PREFIX___sort.c__TEMPLATE__",
]
replace.C__TYPENAME__ = "type"
replace.C__TYPEHEADER__ = "header_include"
@ -23,6 +24,109 @@ replace.C__TYPEHEADER__ = "header_include"
replace.C__PREFIX__ = "prefix"
replace.C__PREFIXUP__ = "prefix"
[definition.str_to_num]
headers = []
sources = [
"mecstd/generic_sources/src/str_to_C__PREFIX__.c__TEMPLATE__",
"mecstd/generic_sources/src/str_to_C__PREFIX___utils.c__TEMPLATE__",
]
replace.C__TYPE__ = "type"
replace.C__UNSIGNED_TYPE__ = "type"
replace.C__PREFIX__ = "prefix"
replace.C__MAX__ = "value"
replace.C__MIN__ = "value"
replace.C__ZERO__ = "value"
replace.C__SIGNED_TYPE__ = "bool"
[[create.str_to_num]]
sources_output = "src/convert/"
headers_output = ""
replace.C__TYPE__ = "t_i64"
replace.C__UNSIGNED_TYPE__ = "t_u64"
replace.C__PREFIX__ = "i64"
replace.C__MAX__ = "9223372036854775807ll"
replace.C__MIN__ = "-9223372036854775808ll"
replace.C__ZERO__ = "0ll"
replace.C__SIGNED_TYPE__ = "true"
[[create.str_to_num]]
sources_output = "src/convert/"
headers_output = ""
replace.C__TYPE__ = "t_i32"
replace.C__UNSIGNED_TYPE__ = "t_u32"
replace.C__PREFIX__ = "i32"
replace.C__MAX__ = "2147483647"
replace.C__MIN__ = "-2147483648"
replace.C__ZERO__ = "0"
replace.C__SIGNED_TYPE__ = "true"
[[create.str_to_num]]
sources_output = "src/convert/"
headers_output = ""
replace.C__TYPE__ = "t_i16"
replace.C__UNSIGNED_TYPE__ = "t_u16"
replace.C__PREFIX__ = "i16"
replace.C__MAX__ = "32767"
replace.C__MIN__ = "-32768"
replace.C__ZERO__ = "0"
replace.C__SIGNED_TYPE__ = "true"
[[create.str_to_num]]
sources_output = "src/convert/"
headers_output = ""
replace.C__TYPE__ = "t_i8"
replace.C__UNSIGNED_TYPE__ = "t_u8"
replace.C__PREFIX__ = "i8"
replace.C__MAX__ = "127"
replace.C__MIN__ = "-128"
replace.C__ZERO__ = "0"
replace.C__SIGNED_TYPE__ = "true"
[[create.str_to_num]]
sources_output = "src/convert/"
headers_output = ""
replace.C__TYPE__ = "t_u64"
replace.C__UNSIGNED_TYPE__ = "t_u64"
replace.C__PREFIX__ = "u64"
replace.C__MAX__ = "18446744073709551615llu"
replace.C__MIN__ = "0llu"
replace.C__ZERO__ = "0llu"
replace.C__SIGNED_TYPE__ = "false"
[[create.str_to_num]]
sources_output = "src/convert/"
headers_output = ""
replace.C__TYPE__ = "t_u32"
replace.C__UNSIGNED_TYPE__ = "t_u32"
replace.C__PREFIX__ = "u32"
replace.C__MAX__ = "4294967295u"
replace.C__MIN__ = "0u"
replace.C__ZERO__ = "0u"
replace.C__SIGNED_TYPE__ = "false"
[[create.str_to_num]]
sources_output = "src/convert/"
headers_output = ""
replace.C__TYPE__ = "t_u16"
replace.C__UNSIGNED_TYPE__ = "t_u16"
replace.C__PREFIX__ = "u16"
replace.C__MAX__ = "65535u"
replace.C__MIN__ = "0u"
replace.C__ZERO__ = "0u"
replace.C__SIGNED_TYPE__ = "false"
[[create.str_to_num]]
sources_output = "src/convert/"
headers_output = ""
replace.C__TYPE__ = "t_u8"
replace.C__UNSIGNED_TYPE__ = "t_u8"
replace.C__PREFIX__ = "u8"
replace.C__MAX__ = "128u"
replace.C__MIN__ = "0u"
replace.C__ZERO__ = "0u"
replace.C__SIGNED_TYPE__ = "false"
[[create.vec]]
sources_output = "src/vec/"
headers_output = "include/me/vec/"

2
mecstd

@ -1 +1 @@
Subproject commit b032e9dcc8c869b4759958e50597cef690353ccc
Subproject commit 54174ae7cea63a007dbde26f470b842d78d43a19

View file

@ -2,10 +2,12 @@ app/best_move
app/do_move
app/find_place
app/iter_find
app/main
app/moves
app/moves/push
app/moves/rev_rotate
app/moves/rotate
app/moves/swap
app/rotate
app/run_with_items
app/target

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");
}