stable, still too slow

This commit is contained in:
Jack Halford 2016-09-09 02:10:11 +02:00
parent 560d411a97
commit b3fee7af01
8 changed files with 66 additions and 51 deletions

View file

@ -17,25 +17,25 @@ F_LIB = ft
F_LIB := $(addprefix -l, $(F_LIB))
W_FLAGS = -Wall -Wextra -Werror
DEBUG = -w
D_FLAGS = -g
MKDIR = mkdir -p
RM = /bin/rm -rf
.PHONY: all clean fclean re lib/libft
all: $(NAME)
$(D_OBJ)/%.o: $(D_SRC)/%.c $(D_INC)
@$(MKDIR) $(D_OBJ)
@$(CC) -I$(D_INC) $(W_FLAGS) -c $< -o $@ $(DEBUG)
@$(CC) -I$(D_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
@echo "Compiling "$<"..."
$(D_LIB):
@$(MAKE) -C $@ 2>/dev/null
$(NAME): $(F_OBJ) $(D_LIB)
$(CC) -I$(D_INC) $(O_LIB) $(F_LIB) $(W_FLAGS) $(F_OBJ) -o $@ $(DEBUG)
$(CC) -I$(D_INC) $(O_LIB) $(F_LIB) $(W_FLAGS) $(F_OBJ) -o $@ $(D_FLAGS)
clean:
$(RM) $(D_OBJ)

View file

@ -1,9 +1,9 @@
#ifndef FILLIT_H
# define FILLIT_H
# define BUF_SIZE 32
# include <fcntl.h>
# include <stdio.h>
# include "libft.h"
# define FILLIT_H
# define BUF_SIZE 32
extern int g_target;
extern int g_ttmn;
@ -31,10 +31,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 waste);
int ft_solver(char **board, t_list **amap, 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 waste); */
t_list *ft_waste_around(char **board, t_list **amap, int size, int i);
t_list *ft_waste_here(char **board, t_list **amap, int size, int i);
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);
t_list *ft_empty_around(char **board, t_list **amap, int size, int i);
t_list *ft_empty_here(char **board, t_list **amap, 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);
#endif

View file

@ -1,8 +1,7 @@
#include "fillit.h"
int ft_fit_blob(char **board, t_list **amap, t_list *lblob, t_list *lttmn, int waste)
int ft_fit_blob(char **board, t_list **amap, t_list *lblob, t_list *lttmn, int space)
{
t_list *tmp;
t_list *list;
t_list *new_map;
t_list *blob;
@ -39,7 +38,7 @@ int ft_fit_blob(char **board, t_list **amap, t_list *lblob, t_list *lttmn, int
new_map = ft_lstmap(*amap, &ft_id);
/* printf("just fitted ttmn %c\n", ttmn->id); */
/* ft_show_ttmn(*ttmn); */
if (ft_check_blobs(board, &new_map, lblob->next, lttmn, waste))
if (ft_check_blobs(board, &new_map, lblob->next, lttmn, space))
return (1);
ttmn->placed = 0;
ft_board_remove(board, ttmn->id);
@ -51,19 +50,17 @@ int ft_fit_blob(char **board, t_list **amap, t_list *lblob, t_list *lttmn, int
return (0);
}
int ft_get_blobs(char **board, t_list **amap, t_list *lttmn)
int ft_get_blobs(char **board, t_list **amap, t_list *lttmn, int space)
{
t_list *lblob;
t_list *blob;
t_list *map;
int waste;
int size;
int i;
if (!lttmn)
return (ft_solved(board));
lblob = NULL;
waste = 0;
size = ft_strlen(*board);
map = *amap;
/* ft_putendl("getting blobs for"); */
@ -72,16 +69,23 @@ int ft_get_blobs(char **board, t_list **amap, t_list *lttmn)
while (map)
{
i = *(int *)map->content;
blob = ft_waste_here(board, amap, size, i);
blob = ft_empty_here(board, amap, size, i);
map = *amap;
if (ft_lstsize(blob) / 4 > 0)
ft_lsteadd(&lblob, ft_lstnew(&blob, sizeof(t_list *)));
/* ft_lst_print(blob, &ft_putnbr); */
if (ft_lstsize(blob) / 4 <= 1)
space -= ft_lstsize(blob) % 4;
/* ft_lst_print(*(t_list **)ft_lstlast(lblob), &ft_putnbr); */
waste += ft_lstsize(blob) % 4;
if (waste > size * size - 4 * g_ttmn)
/* printf("took off %i, waste=%i\n", ft_lstsize(blob) % 4, size*size - 4*g_ttmn - space); */
/* ft_show_board(board); */
/* ft_lst_print(blob, &ft_putnbr); */
/* fflush(stdout); */
if (space < 0)
{
ft_board_remove(board, '*');
/* ft_show_board(board); */
/* printf("lack of space=%i\n", space); */
/* fflush(stdout); */
return (0);
}
}
@ -90,10 +94,10 @@ int ft_get_blobs(char **board, t_list **amap, t_list *lttmn)
/* ft_putendl("found blobs in map:"); */
/* ft_show_board(board); */
/* ft_lst_print2(lblob, &ft_putnbr); */
return (ft_check_blobs(board, amap, lblob, lttmn, waste));
return (ft_check_blobs(board, amap, lblob, lttmn, space));
}
int ft_check_blobs(char **board, t_list **amap, t_list *lblob, t_list *lttmn, int waste)
int ft_check_blobs(char **board, t_list **amap, t_list *lblob, t_list *lttmn, int space)
{
t_list *blob;
t_list *new_map;
@ -103,14 +107,14 @@ int ft_check_blobs(char **board, t_list **amap, t_list *lblob, t_list *lttmn, i
if (!lttmn)
return (ft_solved(board));
if (((t_ttmn *)lttmn->content)->placed)
return (ft_check_blobs(board, amap, lblob, lttmn->next, waste));
return (ft_check_blobs(board, amap, lblob, lttmn->next, space));
fflush(stdout);
if (!lblob)
{
/* printf("calling solver, no more blobs\n"); */
/* fflush(stdout); */
return (ft_solver(board, amap, lttmn, waste));
/* new_map = ft_lstmap(*amap, &ft_id); */
return (ft_solver(board, amap, lttmn, space));
}
size = ft_strlen(*board);
blob = *(t_list **)lblob->content;
@ -120,18 +124,21 @@ int ft_check_blobs(char **board, t_list **amap, t_list *lblob, t_list *lttmn, i
/* ft_lst_print2(lblob, &ft_putnbr); */
if (ft_lstsize(blob) / 4 == 1)
{
new_map = ft_lstmap(*amap, &ft_id);
if (ft_fit_blob(board, &new_map, lblob, lttmn, waste))
/* new_map = ft_lstmap(*amap, &ft_id); */
if (ft_fit_blob(board, amap, lblob, lttmn, space))
{
lblob = lblob->next;
return (1);
}
else
{
waste += 4;
if (waste > size * size - 4 * g_ttmn)
space -= 4;
/* printf("took off 4 from space : %i (fit)\n", space); */
/* fflush(stdout); */
if (space < 0)
{
/* ft_putendl("too much waste"); */
/* ft_show_board(board); */
/* ft_putendl("not enough space"); */
return (0);
}
}
@ -146,5 +153,5 @@ int ft_check_blobs(char **board, t_list **amap, t_list *lblob, t_list *lttmn, i
ft_lst_sorted_merge(amap, blob, &ft_diff);
}
new_map = ft_lstmap(*amap, &ft_id);
return(ft_check_blobs(board, &new_map, lblob->next, lttmn, waste));
return(ft_check_blobs(board, &new_map, lblob->next, lttmn, space));
}

View file

@ -40,7 +40,6 @@ t_list *ft_get_ttmn(char *filename)
size_t i;
size_t j;
size_t k;
size_t l;
int ref[2];
t_ttmn ttmn;
t_list *list;
@ -100,7 +99,6 @@ t_list *ft_get_ttmn(char *filename)
/* fflush(stdout); */
/* ft_show_ttmn(*(t_ttmn*)list->content); */
/* ft_show_ttmn(*(t_ttmn*)ft_lstlast(list)->content); */
l++;
j = -1;
k = 0;
}
@ -125,7 +123,5 @@ t_list *ft_get_ttmn(char *filename)
g_ttmn = ft_lstsize(list);
while ((g_ttmn * 4) > (g_target) * (g_target))
g_target++;
printf("n_ttmn=%zu, target=%zu\n", g_ttmn, g_target);
fflush(stdout);
return (list);
}

View file

@ -2,14 +2,14 @@
int ft_solved(char **board)
{
g_sol = ft_copy_board(board);
/* printf("new solution : size %i\n", ft_strlen(*board)); */
/* fflush(stdout); */
/* ft_show_board(g_sol); */
g_sol = ft_copy_board(board);
return (1);
}
int ft_solver(char **board, t_list **amap, t_list *lttmn, int waste)
int ft_solver(char **board, t_list **amap, t_list *lttmn, int space)
{
int size;
int i;
@ -21,11 +21,13 @@ int ft_solver(char **board, t_list **amap, t_list *lttmn, int waste)
return (ft_solved(board));
ttmn = (t_ttmn *)lttmn->content;
if (ttmn->placed)
return (ft_solver(board, amap, lttmn->next, waste));
return (ft_solver(board, amap, lttmn->next, space));
size = ft_strlen(*board);
map = *amap;
/* ft_putendl("before solver"); */
/* printf("space=%i, waste=%i\n", space, size*size - 4*g_ttmn - space); */
/* fflush(stdout); */
/* ft_show_board(board); */
/* ft_lst_print(map, &ft_putnbr); */
/* ft_show_ttmn(*ttmn); */
@ -46,14 +48,14 @@ int ft_solver(char **board, t_list **amap, t_list *lttmn, int waste)
ft_map_delttmn(amap, i, ttmn->pos, size);
valmap = ft_lstmap(*amap, &ft_id);
if (ft_get_blobs(board, &valmap, lttmn->next))
if (ft_get_blobs(board, &valmap, lttmn->next, space))
return (1);
/* printf("after blob fail\n"); */
/* fflush(stdout); */
ft_map_addttmn(amap, i, ttmn->pos, size);
ft_lst_remove_if(amap, i, &ft_diff);
ft_lst_remove_if(amap, &i, &ft_diff);
map = *amap;
ft_board_remove(board, ttmn->id);

View file

@ -1,6 +1,6 @@
#include "fillit.h"
t_list *ft_waste_here(char **board, t_list **amap, int size, int i)
t_list *ft_empty_here(char **board, t_list **amap, int size, int i)
{
t_list *blob;
@ -11,8 +11,8 @@ t_list *ft_waste_here(char **board, t_list **amap, int size, int i)
/* ft_lst_print(*amap, &ft_putnbr); */
/* printf("removing %i from map\n", i); */
/* ft_lst_print(*amap, &ft_putnbr); */
ft_lst_remove_if(amap, i, &ft_diff);
blob = ft_waste_around(board, amap, size, i);
ft_lst_remove_if(amap, &i, &ft_diff);
blob = ft_empty_around(board, amap, size, i);
ft_lst_sorted_insert(&blob, ft_lstnew(&i, sizeof(int)), &ft_diff);
}
/* printf("list at %i: ", i); */
@ -21,15 +21,15 @@ t_list *ft_waste_here(char **board, t_list **amap, int size, int i)
return (blob);
}
t_list *ft_waste_around(char **board, t_list **amap, int size, int i)
t_list *ft_empty_around(char **board, t_list **amap, int size, int i)
{
t_list *add;
add = NULL;
i % size < size - 1 ? ft_lst_sorted_merge(&add, ft_waste_here(board, amap, size, i + 1), &ft_diff) : 0;
i / size > 0 ? ft_lst_sorted_merge(&add, ft_waste_here(board, amap, size, i - size), &ft_diff) : 0;
i % size > 0 ? ft_lst_sorted_merge(&add, ft_waste_here(board, amap, size, i - 1), &ft_diff) : 0;
i / size < size - 1 ? ft_lst_sorted_merge(&add, ft_waste_here(board, amap, size, i + size), &ft_diff) : 0;
i % size < size - 1 ? ft_lst_sorted_merge(&add, ft_empty_here(board, amap, size, i + 1), &ft_diff) : 0;
i / size > 0 ? ft_lst_sorted_merge(&add, ft_empty_here(board, amap, size, i - size), &ft_diff) : 0;
i % size > 0 ? ft_lst_sorted_merge(&add, ft_empty_here(board, amap, size, i - 1), &ft_diff) : 0;
i / size < size - 1 ? ft_lst_sorted_merge(&add, ft_empty_here(board, amap, size, i + size), &ft_diff) : 0;
/* ft_lst_print(add, &ft_putnbr); */
return (add);
}

View file

@ -20,6 +20,8 @@ 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)
@ -27,7 +29,7 @@ int main(int ac, char **av)
map = ft_lstnew_range(0, size * size);
board = ft_empty_board(size);
ft_lstiter(lttmn, &ft_ttmn_reset);
ft_solver(board, &map, lttmn, 0);
ft_solver(board, &map, lttmn, size * size - 4 * g_ttmn);
ft_free_board(&board);
size--;
}

View file

@ -2,7 +2,7 @@
int cmp_ttmn(t_ttmn a, t_ttmn b)
{
return (ft_strcmp(a.id, b.id));
return (ft_strcmp(&a.id, &b.id));
}
void ft_ttmn_reset(t_list *ttmn)
@ -13,6 +13,7 @@ void ft_ttmn_reset(t_list *ttmn)
void ft_map_delttmn(t_list **amap, int anchor, int pos[4][2], int size)
{
int i;
int j;
i = -1;
fflush(stdout);
@ -20,7 +21,8 @@ void ft_map_delttmn(t_list **amap, int anchor, int pos[4][2], int size)
{
/* printf("deleting: %i\n", anchor + size * pos[i][0] + pos[i][1]); */
/* fflush(stdout); */
ft_lst_remove_if(amap, anchor + size * pos[i][0] + pos[i][1], &ft_diff);
j = anchor + size * pos[i][0] + pos[i][1];
ft_lst_remove_if(amap, &j, &ft_diff);
}
}