From bb28ece7b60940b2af13321188c5bc1b672b36c6 Mon Sep 17 00:00:00 2001 From: Yaroslav Kolomiets Date: Thu, 15 Dec 2016 22:20:46 +0200 Subject: [PATCH 1/3] Peace of shit --- .gitignore | 4 +++ Makefile | 6 ++-- src/fillit.c | 65 ++++++++++++++++++++++++++---------- src/glue_figure.c | 2 +- src/main.c | 42 +++++++++++------------ src/move_up_left.c | 2 +- src/read_file.c | 4 +-- src/string_to_table.c | 2 +- src/test!_check.c | 2 +- src/test!_print_one_string.c | 2 +- src/test!_print_table.c | 2 +- src/to_letters.c | 2 +- tests/sample.fillit | 23 ------------- tests/sample1.fillit | 19 +++++++++++ 14 files changed, 103 insertions(+), 74 deletions(-) create mode 100644 tests/sample1.fillit diff --git a/.gitignore b/.gitignore index e318427..29bf5b9 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,7 @@ # Vim temp *.swp + +CMakeLists.txt +.idea/ +**/cmake-build-debug/* diff --git a/Makefile b/Makefile index 54405ef..8605c7b 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: gtertysh +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/12/14 16:54:48 by gtertysh #+# #+# # -# Updated: 2016/12/14 18:41:33 by gtertysh ### ########.fr # +# Updated: 2016/12/15 22:08:42 by ykolomie ### ########.fr # # # # **************************************************************************** # @@ -43,12 +43,14 @@ LIBFOLDER = ./libft/ FLAGS = -Werror -Wextra -Wall +STACK = -Wl,-stack_size -Wl,0x10000000000 + CC = clang all: $(NAME) $(NAME): $(OBJ) $(LIBFOLDER)$(LIB) - $(CC) $(FLAGS) $(OBJ) $(LIBFLAGS) -o $(NAME) + $(CC) $(FLAGS) $(STACK) $(OBJ) $(LIBFLAGS) -o $(NAME) $(OBJDIR)%.o : $(SRCDIR)%.c $(CC) $(FLAGS) $(INC) -c $< -o $@ diff --git a/src/fillit.c b/src/fillit.c index ca680c3..01a3ca2 100644 --- a/src/fillit.c +++ b/src/fillit.c @@ -1,4 +1,4 @@ -#include "fillit.h" +#include "../inc/fillit.h" unsigned int ft_sqrt_ceil(unsigned int num) { @@ -26,7 +26,7 @@ void convert_tetramino(char *s, t_map *map, int index) int y; int sharp_count; - map->figure[index]->offset_y = 0; + map->figure[index].offset_y = 0; sharp_count = 0; i = 0; y = 1; @@ -41,10 +41,10 @@ void convert_tetramino(char *s, t_map *map, int index) } temp = temp << 12; - map->figure[index]->line[y - 1] = temp; + map->figure[index].line[y - 1] = temp; y++; } - map->figure[index]->height = y - 1; + map->figure[index].height = y - 1; } void map_initialization(char **tetramino_table, t_map *map) @@ -58,6 +58,7 @@ void map_initialization(char **tetramino_table, t_map *map) map->size = ft_sqrt_ceil(i * 4); map->mask = 1; map->mask <<= 16 - map->size; + map->ok = 0; clear_map(map); } @@ -111,32 +112,60 @@ int can_insert(t_map *map, int index) } return (1); } + int shift_tetramino(t_map *map, int index) { - int left_shift; - int i; - int k; + int right_shift; + unsigned int i; + unsigned int k; t_tetrominoes *temp; temp = &map->figure[index]; i = temp->offset_y; k = i + temp->height; - left_shift = 1; + right_shift = 1; while (i < k) if (temp->line[i++] & map->mask) - left_shift = 0; + right_shift = 0; i = temp->offset_y; - if (left_shift) + if (right_shift && ++(temp->offset_x)) while (i < k) temp->line[i++] >>= 1; - else if (k < map->size) - while(k-- > i) - { - while (!(temp->line[k] & 0x8000)) - temp->line[k] <<= 1; - temp->line[k + 1] = temp->line[k]; - } + else if (k < map->size && ++(temp->offset_y)) { + while (k-- > i) { + temp->line[k] <<= temp->offset_x; + temp->line[k + 1] = temp->line[k]; + } + temp->offset_x = 0; + } else return (0); return (1); -} +} + + +t_map algorithm(t_map map, unsigned int index) +{ + int ins = 0; + t_map copy; + + if (index == map.figure_amount) { + map.ok = 1; + return map; + } + while (!(ins = can_insert(&map, index))) + if (!shift_tetramino(&map, index)) + break; + if (ins) + { + copy = map; + insert_tetramino(&map, index); + map = algorithm(map, index + 1); + if (!map.ok) + { + shift_tetramino(©, index); + map = algorithm(copy, index); + } + } + return map; +} \ No newline at end of file diff --git a/src/glue_figure.c b/src/glue_figure.c index f3719ad..2cf8bdc 100644 --- a/src/glue_figure.c +++ b/src/glue_figure.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "fillit.h" +#include "../inc/fillit.h" // This function creates string table in which one line represent one figure // by "glueing" evry 4 strings from table diff --git a/src/main.c b/src/main.c index e9610f4..7f21301 100644 --- a/src/main.c +++ b/src/main.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "fillit.h" +#include "../inc/fillit.h" // bad global variable t_tetrominoes_templates templates[19] = @@ -113,42 +113,40 @@ t_tetrominoes_templates templates[19] = int main(int argc, char **argv) { - char *string; - char **table; - char **ttr; - int i; - + char *string; + char **table; + char **ttr; + int i; + t_map map; + t_map copy; + unsigned int size_map; + i = 0; if (argc != 2) { ft_putstr("error\n"); return (1); } - string = read_file(argv[1]); if (!(string = read_file(argv[1]))) { ft_putstr("error\n"); return (1); } - printf("string:\n\n\n%s\n", string); table = to_table(&string); - printf("print_table:\n\n\n"); - print_table(table); - ttr = glue_figure(table); - printf("glued figures:\n\n\n"); - print_one_string(ttr); - move_up_left(ttr); - printf("move to up left:\n\n\n"); - print_one_string(ttr); - - printf("test check:\n\n"); - test_check(ttr, templates); - - to_letters(ttr); - printf("to latters:\n\n"); print_one_string(ttr); + + map_initialization(ttr, &map); + size_map = map.size; + copy = map; + while (!map.ok) + { + map = copy; + map.size = size_map++; + map = algorithm(map, 0); + } + return (0); } diff --git a/src/move_up_left.c b/src/move_up_left.c index 3fa1129..7f6b5e3 100644 --- a/src/move_up_left.c +++ b/src/move_up_left.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "fillit.h" +#include "../inc/fillit.h" // Moves figures to top left corner. // table contains strings each represent one figure diff --git a/src/read_file.c b/src/read_file.c index 6dfb512..baacc41 100644 --- a/src/read_file.c +++ b/src/read_file.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "fillit.h" +#include "../inc/fillit.h" // read from file. Returns string. char *read_file(char *path) @@ -32,7 +32,7 @@ char *read_file(char *path) string = ft_memalloc(sizeof(char) * BUF_S); while ((readed = read(fd, buf, BUF_S))) { - buf[BUF_S] = '\0'; + buf[readed] = '\0'; old_size = ft_strlen(string); string = ft_realloc(string, old_size + readed, old_size); ft_strcat(string, buf); diff --git a/src/string_to_table.c b/src/string_to_table.c index 0ec55df..3cbc19f 100644 --- a/src/string_to_table.c +++ b/src/string_to_table.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "fillit.h" +#include "../inc/fillit.h" // Split string to table by newline character. // Each string contain one line of the file diff --git a/src/test!_check.c b/src/test!_check.c index 373200e..c339854 100644 --- a/src/test!_check.c +++ b/src/test!_check.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "fillit.h" +#include "../inc/fillit.h" // Test func. Compare figures from file // with templates diff --git a/src/test!_print_one_string.c b/src/test!_print_one_string.c index d7baab5..9ec7c9f 100644 --- a/src/test!_print_one_string.c +++ b/src/test!_print_one_string.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "fillit.h" +#include "../inc/fillit.h" // Test func. Print table but one string contain whole figure. void print_one_string(char **glued) diff --git a/src/test!_print_table.c b/src/test!_print_table.c index 624c77d..475b8e2 100644 --- a/src/test!_print_table.c +++ b/src/test!_print_table.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "fillit.h" +#include "../inc/fillit.h" // Test func. Print string table, // each line - line of tetromino figure. diff --git a/src/to_letters.c b/src/to_letters.c index 0789572..76a187d 100644 --- a/src/to_letters.c +++ b/src/to_letters.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "fillit.h" +#include "../inc/fillit.h" // convert "#" symbols to letters // rewrite for new method with structures diff --git a/tests/sample.fillit b/tests/sample.fillit index ffe4faf..84e79f4 100644 --- a/tests/sample.fillit +++ b/tests/sample.fillit @@ -18,27 +18,4 @@ .##. .... -.... -..#. -.##. -.#.. -.... -.... -.##. -##.. - -.... -.... -..## -.##. - -.... -...# -...# -..## - -.... -.... -...# -.### \ No newline at end of file diff --git a/tests/sample1.fillit b/tests/sample1.fillit new file mode 100644 index 0000000..5b65dda --- /dev/null +++ b/tests/sample1.fillit @@ -0,0 +1,19 @@ +#### +.... +.... +.... + +.... +##.. +.#.. +.#.. + +..#. +###. +.... +.... + +.... +##.. +##.. +.... From 76ced0d78d6bf8ee67c5e34441425df04e1c7e6e Mon Sep 17 00:00:00 2001 From: Yaroslav Kolomiets Date: Fri, 16 Dec 2016 17:26:35 +0200 Subject: [PATCH 2/3] read_file correction --- src/read_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/read_file.c b/src/read_file.c index baacc41..980d656 100644 --- a/src/read_file.c +++ b/src/read_file.c @@ -24,7 +24,7 @@ char *read_file(char *path) readed = 0; string = 0; old_size = 0; - if (!(fd = open(path, O_RDONLY))) + if ((fd = open(path, O_RDONLY)) == -1) { ft_putstr("error\n"); return (0); From 78320824fce283cb14c9adc74f253d6f8ad892fe Mon Sep 17 00:00:00 2001 From: Yaroslav Kolomiets Date: Tue, 20 Dec 2016 20:19:28 +0200 Subject: [PATCH 3/3] dancing links fixed --- src/dancing_links.c | 116 +++++++++++++++++++++++--------------------- src/glue_figure.c | 4 +- 2 files changed, 62 insertions(+), 58 deletions(-) diff --git a/src/dancing_links.c b/src/dancing_links.c index 6d333e7..045784e 100644 --- a/src/dancing_links.c +++ b/src/dancing_links.c @@ -12,67 +12,71 @@ #include "fillit.h" -// t_col *add_column(t_col *root) -// { -// t_col *new_col; +t_node *add_column(t_node *root) +{ + t_node *new_col; -// new_col = (t_col*)malloc(sizeof(t_col)); -// new_col->down = (void*)new_col; -// new_col->up = (void*)new_col; -// new_col->left = root->left; -// new_col->right = root; -// return (new_col); -// } + new_col = (t_node*)malloc(sizeof(t_node)); + new_col->down = new_col; + new_col->up = new_col; + new_col->left = root->left; + new_col->right = root; + new_col->row = -1; + new_col->column = NULL; + return (new_col); +} -// t_node *add_node(t_col *col, int row) -// { -// t_node *new_node; +t_node *add_node(t_node *col, int row) +{ + t_node *new_node; -// new_node = (t_node*)malloc(sizeof(t_node)); -// new_node->row = row; -// new_node->up = (t_node*)col->up; -// new_node->down = (t_node*)col; -// col->up->down = new_node; -// col->up = new_node; -// return (new_node); -// } + new_node = (t_node*)malloc(sizeof(t_node)); + new_node->row = row; + new_node->up = col->up; + new_node->down = col; + col->up->down = new_node; + col->up = new_node; + return (new_node); +} -// void cover(t_col *to_cover) -// { -// void *step_vert; -// t_node *step_horiz; +void cover(t_col *to_cover) +{ + void *step_vert; + t_node *step_horiz; -// to_cover->left->right = to_cover->right; -// to_cover->right->left = to_cover->left; -// step_vert = to_cover->down; -// while (step_vert != (void*)to_cover) -// { -// step_horiz = (t_node*)step_vert->right; -// while (step_horiz != (t_node*)step_vert) { -// step_horiz->down->up = step_horiz->up; -// step_horiz->up->down = step_horiz->down; -// step_horiz = step_horiz->right; -// } -// step_vert = (t_node*)step_vert->down; -// } -// } + to_cover->left->right = to_cover->right; + to_cover->right->left = to_cover->left; + step_vert = to_cover->down; + while (step_vert != to_cover) + { + step_horiz = step_vert->right; + while (step_horiz != step_vert) { + step_horiz->down->up = step_horiz->up; + step_horiz->up->down = step_horiz->down; + step_horiz = step_horiz->right; + } + step_vert = step_vert->down; + } +} -// void uncover(t_col *to_uncover) -// { -// void *step_vert; -// t_node *step_horiz; +void uncover(t_col *to_uncover) +{ + void *step_vert; + t_node *step_horiz; -// step_vert = to_uncover->up; -// while (step_vert != (void*)to_uncover) -// { -// step_horiz = (t_node*)step_vert->left; -// while (step_horiz != (t_node*)step_vert) -// { -// step_horiz->down->up = step_horiz; -// step_horiz->up->down = step_horiz; -// step_horiz = step_horiz->left; -// } -// step_vert = (t_node*)step_vert->up; -// } -// } + step_vert = to_uncover->up; + while (step_vert != to_uncover) + { + step_horiz = step_vert->left; + while (step_horiz != step_vert) + { + step_horiz->down->up = step_horiz; + step_horiz->up->down = step_horiz; + step_horiz = step_horiz->left; + } + step_vert = step_vert->up; + } + to_uncover->left->right = to_uncover; + to_uncover->right->left = to_uncover; +} diff --git a/src/glue_figure.c b/src/glue_figure.c index 07e71b0..e49dc51 100644 --- a/src/glue_figure.c +++ b/src/glue_figure.c @@ -6,7 +6,7 @@ /* By: gtertysh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/14 17:12:30 by gtertysh #+# #+# */ -/* Updated: 2016/12/14 17:12:32 by gtertysh ### ########.fr */ +/* Updated: 2016/12/20 20:06:22 by ykolomie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,4 +43,4 @@ char **glue_figure(char **table) free(table[i++]); free(table); return (ttr_table); -} \ No newline at end of file +}