stack architecture is stable, wrong solution for 11...

This commit is contained in:
Jack Halford 2016-09-12 01:52:13 +02:00
parent 0a8f6242ab
commit 07effd02c9
10 changed files with 408 additions and 200 deletions

1
fillit/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
fillit

View file

@ -9,6 +9,11 @@ extern int g_target;
extern int g_ttmn;
extern char **g_sol;
typedef struct s_stack
{
char id;
int num;
} t_stack;
typedef struct s_ttmn
{
@ -19,6 +24,18 @@ typedef struct s_ttmn
t_list *ft_get_ttmn(char *filename);
void ft_map_clean(t_list *list);
void ft_map_switch(t_list *list);
t_list *ft_stack_new_range(int a, int b);
void ft_map_stack_stars(t_list **amap, t_list **astack, char c);
void ft_map_stack_ttmn(t_list **amap, t_list **astack, int anchor, int pos[4][2], char id, int size);
void ft_put_stack(t_stack *content);
int ft_stack_cmp_num(t_stack *a, int *b);
int ft_stack_cmp_num2(t_stack *a, t_stack *b);
void ft_map_unstack_char(t_list **amap, t_list **astack, char c);
void ft_map_unstack_ttmn(t_list **amap, t_list **astack, char c);
char **ft_empty_board(size_t size);
char **ft_copy_board(char **board);
void ft_free_board(char ***board);
@ -31,17 +48,16 @@ void ft_ttmn_reset(t_list *ttmn);
int ft_board_add(char **board, t_ttmn block, int i);
void ft_board_remove(char **board, char c);
int ft_solver(char **board, t_list **amap, t_list *lttmn, int space);
int ft_solver(char **board, t_list **amap, t_list **astack, t_list *lttmn, int space);
int ft_solved(char **board);
int ft_check_blobs(char **board, t_list **amap, t_list *lblob, t_list *lttmn, int space);
int ft_get_blobs(char **board, t_list **amap, t_list *lttmn, int space);
int ft_fit_blob(char **board, t_list **amap, t_list *blob, t_list *lttmn, int space);
int ft_fit_blob(char **board, t_list **amap, t_list **astack, t_list *lttmn, int i, int blob_size, int space);
int ft_solve_blobs(char **board, t_list **amap, t_list **astack, t_list *lttmn, int space);
t_list *ft_empty_around(char **board, int size, int i);
t_list *ft_empty_here(char **board, int size, int i);
int ft_empty_around2(char **board, int size, int i);
int ft_empty_here2(char **board, int size, int i);
int ft_empty_around2(t_list *map, int size, int i);
int ft_empty_here2(t_list *map, int size, int i);
void ft_map_delttmn(t_list **amap, int anchor, int pos[4][2], int size);
void ft_map_addttmn(t_list **amap, int anchor, int pos[4][2], int size);

@ -1 +1 @@
Subproject commit 4ec8bf9ec711dd24c399d75c5763ee6706d09104
Subproject commit b1f0ae385b169c0ca6bad6dd19b9610619e78413

View file

@ -1,48 +1,67 @@
#include "fillit.h"
int ft_fit_blob(char **board, t_list **amap, t_list *blob, t_list *lttmn, int space)
int ft_fit_blob(char **board, t_list **amap, t_list **astack, t_list *lttmn, int i, int blob_size, int space)
{
t_list *list;
t_list *new_map;
t_ttmn *ttmn;
t_list *list;
t_list *blob;
t_stack *stack;
int size;
int i;
int n;
n = blob_size;
size = ft_strlen(*board);
blob = *astack;
/* ft_putendl("fitting blob:"); */
/* ft_show_board(board); */
/* ft_lst_print(blob, &ft_put_stack); */
while (blob)
{
i = *(int*)blob->content;
stack = blob->content;
/* ft_put_stack(stack); */
if (stack->id != '*')
{
/* ft_putendl("iwtbf"); */
break ;
}
stack->id = '.';
/* ft_lst_print(*astack, &ft_put_stack); */
/* ft_put_stack(stack); */
blob = blob->next;
i = stack->num;
list = lttmn;
while (list)
{
ttmn = (t_ttmn *)list->content;
if (ttmn->placed)
{
list = list->next;
continue ;
}
if (ft_board_add(board, *ttmn, i))
if (ttmn->placed || ft_board_add(board, *ttmn, i))
{
list = list->next;
continue ;
}
/* printf("fitted %c in blob\n", ttmn->id); */
/* fflush(stdout); */
ttmn->placed = 1;
new_map = ft_lstmap(*amap, &ft_id);
if (ft_solver(board, &new_map, lttmn, space))
/* ft_map_stack_ttmn(amap, astack, i, ttmn->pos, ttmn->id, size); */
if (ft_solver(board, amap, astack, lttmn, space))
return (1);
/* ft_map_unstack(amap, astack, 4); */
ttmn->placed = 0;
ft_board_remove(board, ttmn->id);
list = list->next;
}
blob = blob->next;
}
/* ft_lst_print(*astack, &ft_put_stack); */
return (0);
}
int ft_get_blobs(char **board, t_list **amap, t_list *lttmn, int space)
int ft_solve_blobs(char **board, t_list **amap, t_list **astack, t_list *lttmn, int space)
{
t_list *blob;
t_list *new_map;
t_list *map;
int sup_space = 0;
int size;
int blob_size;
@ -51,52 +70,78 @@ int ft_get_blobs(char **board, t_list **amap, t_list *lttmn, int space)
if (!lttmn)
return (ft_solved(board));
size = ft_strlen(*board);
/* new_map = ft_lstmap(*amap, &ft_id); */
new_map = *amap;
while (new_map)
{
i = *(int *)(new_map)->content;
/* ft_show_board(board); */
/* ft_lst_print(new_map, &ft_putnbr); */
map = *amap;
/* ft_board_remove(board, '*'); */
/* ft_board_remove(board, '^'); */
ft_lstiter(*amap, &ft_map_clean);
blob = ft_empty_here(board, size, i);
if (!blob)
/* ft_show_board(board); */
while (map)
{
new_map = new_map->next;
i = ((t_stack *)map->content)->num;
ft_board_replace(board, '*', '^');
ft_lstiter(*amap, &ft_map_switch);
blob_size = ft_empty_here2(map, size, i);
map = map->next;
if (blob_size == 0)
continue ;
}
new_map = new_map->next;
blob_size = ft_lstsize(blob);
/* printf("found blob_size=%i\n", blob_size); */
/* fflush(stdout); */
/* ft_lst_print(*amap, &ft_put_stack); */
/* ft_lst_print(*astack, &ft_put_stack); */
fflush(stdout);
if (blob_size / 4 == 0)
{
space -= blob_size % 4;
/* ft_putendl("delsub"); */
/* ft_lst_print(*amap, &ft_putnbr); */
ft_lst_delsub(amap, blob, &ft_diff, &ft_lst_cfree);
/* ft_lst_print(blob, &ft_putnbr); */
/* ft_lst_print(*amap, &ft_putnbr); */
new_map = *amap;
/* ft_debug(); */
if (space + sup_space < 0)
{
ft_board_remove(board, '*');
return (0);
}
/* ft_putendl("stacking stars:"); */
/* ft_lst_print(*amap, &ft_put_stack); */
/* ft_lst_print(*astack, &ft_put_stack); */
ft_map_stack_stars(amap, astack, '.');
map = *amap;
/* ft_lst_print(*amap, &ft_put_stack); */
/* ft_lst_print(*astack, &ft_put_stack); */
}
else if (blob_size / 4 == 1)
{
space -= blob_size % 4;
ft_lst_delsub(amap, blob, &ft_diff, &ft_lst_cfree);
new_map = *amap;
if (ft_fit_blob(board, amap, blob, lttmn, space))
if (space + sup_space < 0)
{
ft_board_remove(board, '*');
return (0);
}
/* ft_putendl("stacking stars:"); */
/* ft_lst_print(*amap, &ft_put_stack); */
/* ft_lst_print(*astack, &ft_put_stack); */
ft_map_stack_stars(amap, astack, '*');
map = *amap;
/* ft_lst_print(*amap, &ft_put_stack); */
/* ft_lst_print(*astack, &ft_put_stack); */
if (ft_fit_blob(board, amap, astack, lttmn, i, blob_size, space))
{
ft_board_remove(board, '*');
return (1);
}
else
{
space -= 4;
if (space + sup_space < 0)
{
/* ft_board_remove(board, '*'); */
return (0);
}
}
}
else
{
sup_space -= blob_size % 4;
if (space + sup_space < 0)
{
@ -104,6 +149,8 @@ int ft_get_blobs(char **board, t_list **amap, t_list *lttmn, int space)
return (0);
}
}
ft_board_remove(board, '*');
return (ft_solver(board, amap, lttmn, space));
}
/* ft_board_remove(board, '*'); */
/* ft_board_remove(board, '^'); */
return (ft_solver(board, amap, astack, lttmn, space));
}

View file

@ -4,13 +4,15 @@ int ft_solved(char **board)
{
ft_board_remove(board, '*');
g_sol = ft_copy_board(board);
ft_putendl("found solution:");
ft_show_board(board);
return (1);
}
int ft_solver(char **board, t_list **amap, t_list *lttmn, int space)
int ft_solver(char **board, t_list **amap, t_list **astack, t_list *lttmn, int space)
{
int size;
int i;
int size;
t_ttmn *ttmn;
t_list *map;
@ -18,35 +20,40 @@ int ft_solver(char **board, t_list **amap, t_list *lttmn, int space)
return (ft_solved(board));
ttmn = (t_ttmn *)lttmn->content;
if (ttmn->placed)
return (ft_solver(board, amap, lttmn->next, space));
return (ft_solver(board, amap, astack, lttmn->next, space));
size = ft_strlen(*board);
map = *amap;
/* ft_show_board(board); */
/* ft_lst_print(*amap, &ft_putnbr); */
/* ft_lst_print(*amap, &ft_put_stack); */
/* ft_lst_print(*astack, &ft_put_stack); */
while (map)
{
i = *(int *)map->content;
i = ((t_stack *)map->content)->num;
if (ft_board_add(board, *ttmn, i))
{
map = map->next;
continue ;
}
ft_map_stack_ttmn(amap, astack, i, ttmn->pos, ttmn->id, size);
/* ft_show_board(board); */
/* ft_lst_print(*amap, &ft_put_stack); */
/* ft_lst_print(*astack, &ft_put_stack); */
t_list *n_map = ft_lstmap(*amap, &ft_id);
ft_map_delttmn(&n_map, i, ttmn->pos, size);
if (ft_get_blobs(board, &n_map, lttmn->next, space))
if (ft_solve_blobs(board, amap, astack, lttmn->next, space))
return (1);
/* ft_putendl("p2"); */
/* ft_show_board(board); */
/* ft_lst_print(*amap, &ft_put_stack); */
/* ft_lst_print(*astack, &ft_put_stack); */
/* ft_map_delttmn(amap, i, ttmn->pos, size); */
/* if (ft_validate_waste(board, &n_map, lttmn->next, space)) */
/* return (1); */
if (ft_solver(board, amap, lttmn->next, space))
return (1);
/* ft_map_addttmn(amap, i, ttmn->pos, size); */
/* map = *amap; */
ft_map_unstack_ttmn(amap, astack, ttmn->id);
ft_lstiter(*amap, &ft_map_clean);
ft_board_remove(board, ttmn->id);
map = map->next;
/* ft_lst_print(*amap, &ft_put_stack); */
/* ft_lst_print(*astack, &ft_put_stack); */
}
return (0);
}

View file

@ -1,108 +1,108 @@
#include "fillit.h"
int ft_fit_blob2(char **board, t_list **amap, t_list *lttmn, int i, int blob_size, int space)
{
t_ttmn *ttmn;
t_list *list;
int y;
int l;
int size;
int n;
/* int ft_fit_blob2(char **board, t_list **amap, t_list *lttmn, int i, int blob_size, int space) */
/* { */
/* t_ttmn *ttmn; */
/* t_list *list; */
/* int y; */
/* int l; */
/* int size; */
/* int n; */
/* printf("going to fit any at %i,%i\n", i, j); */
/* fflush(stdout); */
/* ft_show_board(board); */
y = i;
n = blob_size;
size = ft_strlen(*board);
while (y < size * size)
{
if (board[y / size][y % size] == '*')
{
n--;
l = -1;
/* printf("0 trying all at %i\n", y); */
/* fflush(stdout); */
list = lttmn;
while (list)
{
ttmn = (t_ttmn *)list->content;
if (ttmn->placed)
{
list = list->next;
continue ;
}
if (ft_board_add(board, *ttmn, i))
{
list = list->next;
continue ;
}
ttmn->placed = 1;
if (ft_solver(board, amap, lttmn, space))
return (1);
ttmn->placed = 0;
ft_board_remove(board, ttmn->id);
list = list->next;
}
/* printf("failed at %i\n", y); */
/* fflush(stdout); */
}
y++;
}
return (0);
}
/* /1* printf("going to fit any at %i,%i\n", i, j); *1/ */
/* /1* fflush(stdout); *1/ */
/* /1* ft_show_board(board); *1/ */
/* y = i; */
/* n = blob_size; */
/* size = ft_strlen(*board); */
/* while (y < size * size) */
/* { */
/* if (board[y / size][y % size] == '*') */
/* { */
/* n--; */
/* l = -1; */
/* /1* printf("0 trying all at %i\n", y); *1/ */
/* /1* fflush(stdout); *1/ */
/* list = lttmn; */
/* while (list) */
/* { */
/* ttmn = (t_ttmn *)list->content; */
/* if (ttmn->placed) */
/* { */
/* list = list->next; */
/* continue ; */
/* } */
/* if (ft_board_add(board, *ttmn, i)) */
/* { */
/* list = list->next; */
/* continue ; */
/* } */
/* ttmn->placed = 1; */
/* if (ft_solver(board, amap, lttmn, space)) */
/* return (1); */
/* ttmn->placed = 0; */
/* ft_board_remove(board, ttmn->id); */
/* list = list->next; */
/* } */
/* /1* printf("failed at %i\n", y); *1/ */
/* /1* fflush(stdout); *1/ */
/* } */
/* y++; */
/* } */
/* return (0); */
/* } */
int ft_validate_waste(char **board, t_list **amap, t_list *lttmn, int space)
{
t_ttmn *ttmn;
int waste;
int y;
t_list *blob;
int blob_size;
int size;
/* int ft_validate_waste(char **board, t_list **amap, t_list *lttmn, int space) */
/* { */
/* t_ttmn *ttmn; */
/* int waste; */
/* int y; */
/* t_list *blob; */
/* int blob_size; */
/* int size; */
if (!lttmn)
return (ft_solved(board));
ttmn = (t_ttmn *)lttmn->content;
if (ttmn->placed)
return (ft_solver(board, amap, lttmn->next, space));
waste = 0;
size = ft_strlen(*board);
y = -1;
/* ft_show_board(board); */
while (++y < size * size)
{
if (board[y / size][y % size] != '.')
continue ;
/* blob = ft_empty_here(board, size, y); */
/* blob_size = ft_lstsize(blob); */
blob_size = ft_empty_here2(board, size, y);
/* ft_board_replace(board, '*', '^') */
/* printf("found blob=%i at %i\n", blob_size, y); */
/* fflush(stdout); */
/* ft_show_board(board); */
waste += blob_size % 4;
if (waste > size * size - 4 * g_ttmn)
{
ft_board_remove(board, '*');
return (0);
}
if (blob_size / 4 == 1)
{
ft_board_remove(board, '*');
blob = ft_empty_here(board, size, y);
if (ft_fit_blob2(board, amap, lttmn, y, blob_size, space))
return (1);
}
waste += (blob_size / 4 == 1) ? 4 : 0;
if (waste > size * size - 4 * g_ttmn)
{
ft_board_remove(board, '*');
return (0);
}
}
ft_board_remove(board, '*');
return (ft_solver(board, amap, lttmn, space));
}
/* if (!lttmn) */
/* return (ft_solved(board)); */
/* ttmn = (t_ttmn *)lttmn->content; */
/* if (ttmn->placed) */
/* return (ft_solver(board, amap, lttmn->next, space)); */
/* waste = 0; */
/* size = ft_strlen(*board); */
/* y = -1; */
/* /1* ft_show_board(board); *1/ */
/* while (++y < size * size) */
/* { */
/* if (board[y / size][y % size] != '.') */
/* continue ; */
/* /1* blob = ft_empty_here(board, size, y); *1/ */
/* /1* blob_size = ft_lstsize(blob); *1/ */
/* blob_size = ft_empty_here2(board, size, y); */
/* /1* ft_board_replace(board, '*', '^') *1/ */
/* /1* printf("found blob=%i at %i\n", blob_size, y); *1/ */
/* /1* fflush(stdout); *1/ */
/* /1* ft_show_board(board); *1/ */
/* waste += blob_size % 4; */
/* if (waste > size * size - 4 * g_ttmn) */
/* { */
/* ft_board_remove(board, '*'); */
/* return (0); */
/* } */
/* if (blob_size / 4 == 1) */
/* { */
/* ft_board_remove(board, '*'); */
/* blob = ft_empty_here(board, size, y); */
/* if (ft_fit_blob2(board, amap, lttmn, y, blob_size, space)) */
/* return (1); */
/* } */
/* waste += (blob_size / 4 == 1) ? 4 : 0; */
/* if (waste > size * size - 4 * g_ttmn) */
/* { */
/* ft_board_remove(board, '*'); */
/* return (0); */
/* } */
/* } */
/* ft_board_remove(board, '*'); */
/* return (ft_solver(board, amap, lttmn, space)); */
/* } */

View file

@ -30,16 +30,24 @@ t_list *ft_empty_around(char **board, int size, int i)
return (add);
}
int ft_empty_here2(char **board, int size, int i)
int ft_empty_here2(t_list *map, int size, int i)
{
int n;
char c;
t_list *list;
n = 0;
if (board[i / size][i % size] == '.')
list = ft_lst_find(map, &i, &ft_stack_cmp_num);
if (list)
{
board[i / size][i % size] = '*';
c = ((t_stack *)list->content)->id;
if (c == '.')
{
/* board[i / size][i % size] = '*'; */
((t_stack *)list->content)->id = '*';
n++;
n += ft_empty_around2(board, size, i);
n += ft_empty_around2(map, size, i);
}
}
/* printf("list at %i: ", i); */
/* fflush(stdout); */
@ -47,15 +55,19 @@ int ft_empty_here2(char **board, int size, int i)
return (n);
}
int ft_empty_around2(char **board, int size, int i)
int ft_empty_around2(t_list *map, int size, int i)
{
int n;
n = 0;
n += i % size < size - 1 ? ft_empty_here2(board, size, i + 1) : 0;
n += i / size > 0 ? ft_empty_here2(board, size, i - size) : 0;
n += i % size > 0 ? ft_empty_here2(board, size, i - 1) : 0;
n += i / size < size - 1 ? ft_empty_here2(board, size, i + size) : 0;
/* n += i % size < size - 1 ? ft_empty_here2(board, size, i + 1) : 0; */
/* n += i / size > 0 ? ft_empty_here2(board, size, i - size) : 0; */
/* n += i % size > 0 ? ft_empty_here2(board, size, i - 1) : 0; */
/* n += i / size < size - 1 ? ft_empty_here2(board, size, i + size) : 0; */
n += i % size < size - 1 ? ft_empty_here2(map, size, i + 1) : 0;
n += i / size > 0 ? ft_empty_here2(map, size, i - size) : 0;
n += i % size > 0 ? ft_empty_here2(map, size, i - 1) : 0;
n += i / size < size - 1 ? ft_empty_here2(map, size, i + size) : 0;
/* ft_lst_print(add, &ft_putnbr); */
return (n);
}

View file

@ -11,6 +11,7 @@ int main(int ac, char **av)
{
t_list *lttmn;
t_list *map;
t_list *stack;
char **board;
int size;
@ -20,16 +21,13 @@ int main(int ac, char **av)
return (1);
}
size = g_target + 2;
/* printf("n_ttmn=%zu, target=%zu\n", g_ttmn, g_target); */
/* fflush(stdout); */
/* ft_show_ttmn(*(t_ttmn *)lttmn->content); */
/* ft_show_ttmn(*(t_ttmn *)ft_lstlast(lttmn)->content); */
while (size >= g_target)
{
map = ft_lstnew_range(0, size * size);
map = ft_stack_new_range(0, size * size);
stack = NULL;
board = ft_empty_board(size);
ft_lstiter(lttmn, &ft_ttmn_reset);
ft_solver(board, &map, lttmn, size * size - 4 * g_ttmn);
ft_solver(board, &map, &stack, lttmn, size * size - 4 * g_ttmn);
ft_free_board(&board);
size--;
}

98
fillit/src/stack_lib.c Normal file
View file

@ -0,0 +1,98 @@
#include "fillit.h"
void ft_put_stack(t_stack *content)
{
ft_putnbr(content->num);
ft_putchar(',');
ft_putchar(content->id);
}
t_list *ft_stack_new_range(int a, int b)
{
t_list *lst;
t_stack stack;
if (a >= b)
return (NULL);
lst = NULL;
while (a < b)
{
b--;
stack.num = b;
stack.id = '.';
ft_lstadd(&lst, ft_lstnew(&stack, sizeof(t_stack)));
}
return (lst);
}
int ft_stack_cmp_num(t_stack *a, int *b)
{
return (a->num - *b);
}
int ft_stack_cmp_num2(t_stack *a, t_stack *b)
{
return (a->num - b->num);
}
int ft_stack_cmp_id(t_stack *a, char *b)
{
char id;
id = a->id;
/* printf("id:%c, ref:%c, cmp:%i\n", id, *b, strncmp(&id, b, 1)); */
/* fflush(stdout); */
return (strncmp(&id, b, 1));
}
void ft_map_clean(t_list *list)
{
t_stack *stack;
stack = (t_stack *)list->content;
stack->id = '.';
}
void ft_map_switch(t_list *list)
{
t_stack *stack;
stack = (t_stack *)list->content;
if (stack->id == '*')
stack->id = '^';
}
void ft_map_stack_stars(t_list **amap, t_list **astack, char c)
{
t_list *link;
t_stack *content;
char star;
star = '*';
while ((link = ft_lst_removeif(amap, &star, &ft_stack_cmp_id)))
{
content = (t_stack *)link->content;
content->id = c;
ft_lstadd(astack, link);
}
}
void ft_map_stack_ttmn(t_list **amap, t_list **astack, int anchor, int pos[4][2], char id, int size)
{
int i;
int j;
t_list *link;
t_stack *stack;
i = -1;
while (++i < 4)
{
j = anchor + size * pos[i][0] + pos[i][1];
/* printf("moving: %i\n", j); */
/* fflush(stdout); */
link = ft_lst_removeif(amap, &j, &ft_stack_cmp_num);
stack = (t_stack *)link->content;
stack->id = id;
ft_lstadd(astack, link);
}
}

29
fillit/src/unstack_lib.c Normal file
View file

@ -0,0 +1,29 @@
#include "fillit.h"
void ft_map_unstack_char(t_list **amap, t_list **astack, char c)
{
t_list *link;
t_stack *stack;
link = *astack;
stack = link->content;
while (stack->id == c)
{
/* printf("stack->num = %i\n", stack->num); */
/* fflush(stdout); */
*astack = (*astack)->next;
ft_lst_sorted_insert(amap, link, &ft_stack_cmp_num2);
/* stack->id = '.'; */
link = *astack;
if (!link)
break ;
stack = link->content;
}
}
void ft_map_unstack_ttmn(t_list **amap, t_list **astack, char c)
{
ft_map_unstack_char(amap, astack, '.');
ft_map_unstack_char(amap, astack, c);
}