yes
This commit is contained in:
parent
54a5b658d4
commit
33db892d3b
11 changed files with 233 additions and 38 deletions
6
gen.list
6
gen.list
|
|
@ -1,6 +1,12 @@
|
|||
vec/vec_cost
|
||||
vec/vec_cost_functions2
|
||||
vec/vec_cost_functions3
|
||||
vec/vec_element
|
||||
vec/vec_element_functions2
|
||||
vec/vec_element_functions3
|
||||
vec/vec_i64
|
||||
vec/vec_i64_functions2
|
||||
vec/vec_i64_functions3
|
||||
vec/vec_moves
|
||||
vec/vec_moves_functions2
|
||||
vec/vec_moves_functions3
|
||||
|
|
|
|||
32
include/app/cost.h
Normal file
32
include/app/cost.h
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cost.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/12 21:13:51 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/12 23:37:23 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef COST_H
|
||||
#define COST_H
|
||||
|
||||
#include "app/element.h"
|
||||
#include "app/state.h"
|
||||
#include "app/types/type_cost.h"
|
||||
#include "me/types.h"
|
||||
|
||||
t_cost alloc_cost(t_state *s);
|
||||
void free_cost(t_cost self);
|
||||
void calculate_cost(t_state *s);
|
||||
void cost_for_index(t_state *s, t_usize index);
|
||||
|
||||
typedef struct s_cost_iter_state
|
||||
{
|
||||
t_cost *current_minimum;
|
||||
t_usize max_index;
|
||||
} t_cost_iter_state;
|
||||
|
||||
#endif /* COST_H */
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 16:13:08 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/11 16:18:49 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/01/12 21:27:27 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -15,22 +15,9 @@
|
|||
|
||||
#include "app/element.h"
|
||||
#include "app/state.h"
|
||||
#include "me/types.h"
|
||||
#include "app/types/type_move.h"
|
||||
|
||||
typedef enum e_move
|
||||
{
|
||||
SWAP_A = 1 << 0,
|
||||
SWAP_B = 1 << 1,
|
||||
SWAP_BOTH = SWAP_A | SWAP_B,
|
||||
PUSH_A = 1 << 2,
|
||||
PUSH_B = 1 << 3,
|
||||
ROTATE_A = 1 << 4,
|
||||
ROTATE_B = 1 << 5,
|
||||
ROTATE_BOTH = ROTATE_A | ROTATE_B,
|
||||
REVERSE_ROTATE_A = 1 << 6,
|
||||
REVERSE_ROTATE_B = 1 << 7,
|
||||
REVERSE_ROTATE_BOTH = REVERSE_ROTATE_A | REVERSE_ROTATE_B,
|
||||
} t_move;
|
||||
#include "me/types.h"
|
||||
|
||||
void do_move(t_move m, t_state *s);
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 14:27:25 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/11 14:36:28 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/01/12 21:16:59 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -15,11 +15,13 @@
|
|||
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_element.h"
|
||||
#include "me/vec/vec_cost.h"
|
||||
|
||||
typedef struct s_state
|
||||
{
|
||||
t_vec_element stack_a;
|
||||
t_vec_element stack_b;
|
||||
t_vec_cost costs;
|
||||
} t_state;
|
||||
|
||||
t_state parses_arguments(t_usize count, t_str nums[]);
|
||||
|
|
|
|||
26
include/app/types/type_cost.h
Normal file
26
include/app/types/type_cost.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* type_cost.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/12 21:20:00 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/12 23:16:24 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef TYPE_COST_H
|
||||
#define TYPE_COST_H
|
||||
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_moves.h"
|
||||
|
||||
typedef struct s_cost
|
||||
{
|
||||
t_usize index;
|
||||
t_vec_moves moves;
|
||||
bool active;
|
||||
} t_cost;
|
||||
|
||||
#endif /* TYPE_COST_H */
|
||||
31
include/app/types/type_move.h
Normal file
31
include/app/types/type_move.h
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* type_move.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/12 21:26:56 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/12 21:27:08 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef TYPE_MOVE_H
|
||||
#define TYPE_MOVE_H
|
||||
|
||||
typedef enum e_move
|
||||
{
|
||||
SWAP_A = 1 << 0,
|
||||
SWAP_B = 1 << 1,
|
||||
SWAP_BOTH = SWAP_A | SWAP_B,
|
||||
PUSH_A = 1 << 2,
|
||||
PUSH_B = 1 << 3,
|
||||
ROTATE_A = 1 << 4,
|
||||
ROTATE_B = 1 << 5,
|
||||
ROTATE_BOTH = ROTATE_A | ROTATE_B,
|
||||
REVERSE_ROTATE_A = 1 << 6,
|
||||
REVERSE_ROTATE_B = 1 << 7,
|
||||
REVERSE_ROTATE_BOTH = REVERSE_ROTATE_A | REVERSE_ROTATE_B,
|
||||
} t_move;
|
||||
|
||||
#endif /* TYPE_MOVE_H */
|
||||
16
input.toml
16
input.toml
|
|
@ -38,3 +38,19 @@ replace.C__TYPENAME__ = "t_i64"
|
|||
replace.C__TYPEHEADER__ = ''
|
||||
replace.C__PREFIX__ = "i64"
|
||||
replace.C__PREFIXUP__ = "I64"
|
||||
|
||||
[[create.vec]]
|
||||
sources_output = "src/vec/"
|
||||
headers_output = "include/me/vec/"
|
||||
replace.C__TYPENAME__ = "t_cost"
|
||||
replace.C__TYPEHEADER__ = '#include "app/types/type_cost.h"'
|
||||
replace.C__PREFIX__ = "cost"
|
||||
replace.C__PREFIXUP__ = "COST"
|
||||
|
||||
[[create.vec]]
|
||||
sources_output = "src/vec/"
|
||||
headers_output = "include/me/vec/"
|
||||
replace.C__TYPENAME__ = "t_move"
|
||||
replace.C__TYPEHEADER__ = '#include "app/types/type_move.h"'
|
||||
replace.C__PREFIX__ = "moves"
|
||||
replace.C__PREFIXUP__ = "MOVES"
|
||||
|
|
|
|||
1
src.list
1
src.list
|
|
@ -1,3 +1,4 @@
|
|||
app/cost
|
||||
app/lis/lis
|
||||
app/lis/lower_bound
|
||||
app/main
|
||||
|
|
|
|||
76
src/app/cost.c
Normal file
76
src/app/cost.c
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cost.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/12 21:22:44 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/13 00:58:47 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "app/cost.h"
|
||||
#include "app/state.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_cost.h"
|
||||
#include "me/vec/vec_moves.h"
|
||||
|
||||
void reset_cost(t_usize _index, t_cost *c, void *_state)
|
||||
{
|
||||
(void)(_index + (t_usize)_state);
|
||||
c->active = false;
|
||||
c->moves.len = 0;
|
||||
}
|
||||
|
||||
void find_minimum(t_usize index, t_cost *c, t_cost_iter_state *s)
|
||||
{
|
||||
if (index >= s->max_index || !c->active)
|
||||
return;
|
||||
if (c->moves.len < s->current_minimum->moves.len)
|
||||
s->current_minimum = c;
|
||||
}
|
||||
|
||||
void cost_for_index(t_state *s, t_usize index)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void free_cost(t_cost self)
|
||||
{
|
||||
vec_moves_free(self.moves);
|
||||
}
|
||||
|
||||
t_cost alloc_cost(t_state *s)
|
||||
{
|
||||
t_cost out;
|
||||
|
||||
out = (t_cost){};
|
||||
out.active = false;
|
||||
out.index = s->costs.len;
|
||||
out.moves = vec_moves_new(s->stack_a.len, NULL);
|
||||
return (out);
|
||||
}
|
||||
|
||||
void calculate_cost(t_state *s)
|
||||
{
|
||||
t_vec_cost *costs;
|
||||
t_cost *cur;
|
||||
t_usize index;
|
||||
t_cost_iter_state iter_state;
|
||||
|
||||
costs = &s->costs;
|
||||
vec_cost_iter(costs, &reset_cost, NULL);
|
||||
index = 0;
|
||||
while (index > costs->len)
|
||||
{
|
||||
cur = &costs->buffer[index];
|
||||
cost_for_index(s, index);
|
||||
if (cur->moves.len == 1)
|
||||
break;
|
||||
index++;
|
||||
}
|
||||
iter_state = (t_cost_iter_state){.current_minimum = costs->buffer,
|
||||
.max_index = index};
|
||||
vec_cost_iter(costs, (void (*)())find_minimum, &iter_state);
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 18:50:35 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/12 19:41:23 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/01/12 20:42:48 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -185,16 +185,16 @@ void lis_inner(t_vec_element *elements, t_vec_i64 *result,
|
|||
i = 1;
|
||||
while (i < (t_i64)elements->len)
|
||||
{
|
||||
if (elements->buffer[result->buffer[result->len - 1]].value <
|
||||
elements->buffer[i].value)
|
||||
if (-elements->buffer[result->buffer[result->len - 1]].value <
|
||||
-elements->buffer[i].value)
|
||||
{
|
||||
previous_chain->buffer[i] = result->buffer[result->len - 1];
|
||||
vec_i64_push(result, i++);
|
||||
continue;
|
||||
}
|
||||
next_element_index = binary_search(elements, i);
|
||||
if (elements->buffer[i].value >
|
||||
elements->buffer[result->buffer[next_element_index]].value)
|
||||
if (-elements->buffer[i].value >
|
||||
-elements->buffer[result->buffer[next_element_index]].value)
|
||||
{
|
||||
if (next_element_index > 0)
|
||||
previous_chain->buffer[i] =
|
||||
|
|
@ -228,5 +228,6 @@ t_vec_i64 lis(t_vec_element *elements)
|
|||
v = previous_chain.buffer[v];
|
||||
}
|
||||
vec_i64_free(previous_chain);
|
||||
vec_i64_reverse(&result);
|
||||
return (result);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,10 +6,11 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 14:14:18 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/12 19:28:15 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/01/12 23:41:30 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "app/cost.h"
|
||||
#include "app/dup_state.h"
|
||||
#include "app/element.h"
|
||||
#include "app/lis.h"
|
||||
|
|
@ -19,12 +20,12 @@
|
|||
#include "me/printf/printf.h"
|
||||
#include "me/string/str_clone.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_cost.h"
|
||||
#include "me/vec/vec_element.h"
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
|
||||
void free_element(t_element elem)
|
||||
{
|
||||
|
|
@ -35,6 +36,7 @@ void free_state(t_state state)
|
|||
{
|
||||
vec_element_free(state.stack_a);
|
||||
vec_element_free(state.stack_b);
|
||||
vec_cost_free(state.costs);
|
||||
}
|
||||
|
||||
t_error parse_element(t_str s, t_element *e)
|
||||
|
|
@ -59,7 +61,7 @@ t_error parse_element(t_str s, t_element *e)
|
|||
*e = out;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
void check_eq(t_usize i, t_element *rhs, t_dup_state *dup_state)
|
||||
void check_eq(t_usize i, const t_element *rhs, t_dup_state *dup_state)
|
||||
{
|
||||
dup_state->found_dup =
|
||||
dup_state->found_dup ||
|
||||
|
|
@ -96,8 +98,8 @@ t_state parses_arguments(t_usize count, t_str nums[])
|
|||
t_element e;
|
||||
|
||||
i = 0;
|
||||
out.stack_a = vec_element_new(count * 2, free_element);
|
||||
out.stack_b = vec_element_new(count * 2, free_element);
|
||||
out.stack_a = vec_element_new(count + 1, free_element);
|
||||
out.stack_b = vec_element_new(count + 1, free_element);
|
||||
while (i < count)
|
||||
{
|
||||
if (parse_element(nums[i], &e))
|
||||
|
|
@ -106,11 +108,10 @@ t_state parses_arguments(t_usize count, t_str nums[])
|
|||
nums[i]),
|
||||
vec_element_free(out.stack_a), vec_element_free(out.stack_b),
|
||||
exit(1));
|
||||
vec_element_push(&out.stack_b, e);
|
||||
vec_element_push(&out.stack_a, e);
|
||||
i++;
|
||||
}
|
||||
while (!vec_element_pop(&out.stack_b, &e))
|
||||
vec_element_push(&out.stack_a, e);
|
||||
vec_element_reverse(&out.stack_a);
|
||||
check_no_duplicate(&out.stack_a, &out.stack_b);
|
||||
return out;
|
||||
}
|
||||
|
|
@ -130,6 +131,23 @@ t_state parses_arguments(t_usize count, t_str nums[])
|
|||
#define STR_CENTER(str, width) LEFT(str, width), str, RIGHT(str, width), ""
|
||||
|
||||
#define STACK_WIDTH 12
|
||||
|
||||
bool check_sorted(t_state *s)
|
||||
{
|
||||
t_usize not_in_order;
|
||||
t_usize i;
|
||||
|
||||
not_in_order = 0;
|
||||
i = 0;
|
||||
while (i + 1 < s->stack_a.len)
|
||||
{
|
||||
if (s->stack_a.buffer[i].value > s->stack_a.buffer[i].value)
|
||||
not_in_order++;
|
||||
i++;
|
||||
}
|
||||
return (not_in_order == 0);
|
||||
}
|
||||
|
||||
void print_state(t_state *s)
|
||||
{
|
||||
t_usize max_len;
|
||||
|
|
@ -163,17 +181,16 @@ void print_state(t_state *s)
|
|||
|
||||
int main(t_i32 argc, t_str argv[])
|
||||
{
|
||||
t_state state;
|
||||
t_vec_i64 o;
|
||||
t_state state;
|
||||
|
||||
state = parses_arguments(--argc, ++argv);
|
||||
state.costs = vec_cost_new(state.stack_a.len, &free_cost);
|
||||
while (state.costs.len < state.stack_a.len)
|
||||
vec_cost_push(&state.costs, alloc_cost(&state));
|
||||
print_state(&state);
|
||||
o = lis(&state.stack_a);
|
||||
printf("len = %zu\n", o.len);
|
||||
for (t_usize i = 0; i < o.len; i++)
|
||||
printf("%lli\n", state.stack_a.buffer[o.buffer[i]].value);
|
||||
printf("\n");
|
||||
while (!check_sorted(&state))
|
||||
{
|
||||
}
|
||||
free_state(state);
|
||||
vec_i64_free(o);
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue