diff --git a/f_bonus_opti.c b/f_bonus_opti.c index 41e132f..66520d7 100644 --- a/f_bonus_opti.c +++ b/f_bonus_opti.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/05/24 14:42:46 by hulamy #+# #+# */ -/* Updated: 2019/06/01 13:47:51 by hulamy ### ########.fr */ +/* Updated: 2019/06/01 14:11:32 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ ** Test optimisation for not testing wrong maps when tetri are identical */ -int check_tetri_memory(t_fillist *list, int pos) +int check_tetri_memory(t_fillist *list, int pos) { t_fillist *tetri; unsigned int mask; @@ -40,7 +40,7 @@ int check_tetri_memory(t_fillist *list, int pos) ** Test optimisation for not testing wrong maps when tetri are identical */ -int compare_tetri(t_fillist *tetri_a, t_fillist *tetri_b) +int compare_tetri(t_fillist *tetri_a, t_fillist *tetri_b) { if (tetri_a->tetribit != tetri_b->tetribit) return (0); @@ -70,14 +70,13 @@ t_fillist *clean_list_memory(t_fillist *list, t_fillist *tmp) ** Test optimisation for not testing wrong maps when tetri are identical */ -int check_same_tetri(t_fillist *list, int num) +int check_same_tetri(t_fillist *list, int num) { t_fillist *curr_tetri; t_fillist *next_tetri; int i; curr_tetri = clean_list_memory(list, list); -// curr_tetri = list; while (curr_tetri != NULL) { i = 0; diff --git a/f_bonus_print.c b/f_bonus_print.c index ad0b195..a221cec 100644 --- a/f_bonus_print.c +++ b/f_bonus_print.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/05/27 13:46:29 by hulamy #+# #+# */ -/* Updated: 2019/05/29 16:25:52 by hulamy ### ########.fr */ +/* Updated: 2019/06/01 14:17:43 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,6 +59,7 @@ int print_binary_map(unsigned int *map, int size, int *dope) print_sized_map(map, size, size, '#'); ft_putchar('\n'); } + free(map); return (size); } diff --git a/f_handle_errors.c b/f_handle_errors.c index dc27add..93241fc 100644 --- a/f_handle_errors.c +++ b/f_handle_errors.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/01 13:29:05 by vmanzoni #+# #+# */ -/* Updated: 2019/05/29 15:55:03 by hulamy ### ########.fr */ +/* Updated: 2019/06/01 14:49:01 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,16 +33,17 @@ void print_error_extended(int error, int *dope) if (error == 1) print_error("error: File contains char other than '.','#' and '\\n'\n"); if (error == 2) - print_error("error: File contains two tetriminos not" + print_error("error: File contains two tetriminos not" "separated by a '\\n'\n"); if (error == 3) print_error("error: File contains more than 2 '\\n' in a row\n"); if (error == 4) - print_error("error: File contains less than 1 tetrimino " - "or more than 26\n"); + print_error("error: File contains less than 1 tetrimino\n"); if (error == 5) - print_error("error: Tetrimino has more or less than 4 #\n"); + print_error("error: File contains more than 26 tetriminos\n"); if (error == 6) + print_error("error: Tetrimino has more or less than 4 #\n"); + if (error == 7) print_error("error: Tetrimino # are not all connected\n"); print_error("error\n"); } @@ -67,15 +68,17 @@ void check_file_errors(char *file, int *dope) print_error_extended(1, dope); else if (file[i] == '\n') line_nbr++; - if (file[i] == '\n' && line_nbr % 5 == 0 && file[i-1] != '\n') + if (file[i] == '\n' && line_nbr % 5 == 0 && file[i - 1] != '\n') print_error_extended(2, dope); - if (file[i] == '\n' && file[i+1] != '\0' && \ - file[i+2] != '.' && file[i+2] != '#') + if (file[i] == '\n' && file[i + 1] != '\0' && \ + file[i + 2] != '.' && file[i + 2] != '#') print_error_extended(3, dope); i++; } - if (line_nbr < 4 || line_nbr > 129) + if (line_nbr < 4) print_error_extended(4, dope); + if (!dope[3] && line_nbr > 129) + print_error_extended(5, dope); } /* @@ -102,7 +105,7 @@ int check_tetri_errors(char *tetri) i++; } if (htg != 4 || dot != 12 || check_tetri_errors_proxy(tetri)) - return (5 + check_tetri_errors_proxy(tetri)); + return (6 + check_tetri_errors_proxy(tetri)); return (0); } diff --git a/f_print_map_with_colors.c b/f_print_map_with_colors.c index 6a53edf..8ea47cc 100644 --- a/f_print_map_with_colors.c +++ b/f_print_map_with_colors.c @@ -6,18 +6,13 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/05/03 20:27:22 by vmanzoni #+# #+# */ -/* Updated: 2019/05/08 09:02:39 by vmanzoni ### ########.fr */ +/* Updated: 2019/06/01 14:27:30 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #include "fillit.h" -void ft_putchar(char c) -{ - write(1, &c, 1); -} - -void ft_putchar_color(char c, char color) +void ft_putchar_color(char c, char color) { if (color == 'R') write(1, RED, 5); diff --git a/f_search_map.c b/f_search_map.c index 00bb44e..13e5b94 100644 --- a/f_search_map.c +++ b/f_search_map.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/27 20:47:22 by hulamy #+# #+# */ -/* Updated: 2019/06/01 13:38:26 by hulamy ### ########.fr */ +/* Updated: 2019/06/01 14:19:22 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ ** function that look if a tretri fit in a place */ -unsigned int fit_in_place(unsigned int *map, t_fillist *lst, int size, int i) +unsigned int fit_in_place(unsigned int *map, t_fillist *lst, int sze, int i) { unsigned int tmp; unsigned int mask; @@ -40,7 +40,7 @@ unsigned int fit_in_place(unsigned int *map, t_fillist *lst, int size, int i) if (n + 1 < lst->total_num) tmp |= (mask & (map[n + 1] >> (32 - r))); tetri <<= lst->width; - r += size; + r += sze; } return (!(tmp & tetri)); } @@ -84,20 +84,20 @@ int find_place(unsigned int *map, t_fillist *list, int size) void add_remove(unsigned int *map, t_fillist *list, int size) { - unsigned int mask; + unsigned int msk; unsigned short tetri; int i; int j; tetri = list->tetribit; - mask = ~0u << (32 - list->width); + msk = ~0u << (32 - list->width); i = (list->height - 1) * list->width; j = (list->height - 1) * size + list->position; while (j >= list->position) { - map[(j - 1) / 32] ^= (mask & tetri << (16 + i)) >> (j - 1); + map[(j - 1) / 32] ^= (msk & tetri << (16 + i)) >> (j - 1); if ((j - 1) / 32 + 1 < list->total_num) - map[(j - 1) / 32 + 1] ^= (mask & tetri << (16 + i)) << (32 - j) << 1; + map[(j - 1) / 32 + 1] ^= (msk & tetri << (16 + i)) << (32 - j) << 1; j -= size; i -= list->width; } @@ -142,7 +142,7 @@ int fill_map(unsigned int *map, t_fillist *list, int size) int search_map(t_fillist *list) { t_fillist *tmp; - unsigned int *map = NULL; + unsigned int *map; int size; int num; int i; @@ -165,6 +165,5 @@ int search_map(t_fillist *list) map[num] = 0; i = fill_map(map, list, size++); } - free(map); return (print_binary_map(map, size, list->dope)); } diff --git a/fillit.dSYM/Contents/Info.plist b/fillit.dSYM/Contents/Info.plist deleted file mode 100644 index 96bfe2d..0000000 --- a/fillit.dSYM/Contents/Info.plist +++ /dev/null @@ -1,20 +0,0 @@ - - - - - CFBundleDevelopmentRegion - English - CFBundleIdentifier - com.apple.xcode.dsym.fillit - CFBundleInfoDictionaryVersion - 6.0 - CFBundlePackageType - dSYM - CFBundleSignature - ???? - CFBundleShortVersionString - 1.0 - CFBundleVersion - 1 - - diff --git a/fillit.h b/fillit.h index 984b987..56c9dbc 100644 --- a/fillit.h +++ b/fillit.h @@ -6,19 +6,19 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/01 13:34:46 by vmanzoni #+# #+# */ -/* Updated: 2019/06/01 13:44:51 by hulamy ### ########.fr */ +/* Updated: 2019/06/01 14:34:31 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FILLIT_H # define FILLIT_H -#include -#include -#include -#include +# include +# include +# include +# include -#include "libft/includes/libft.h" +# include "libft/includes/libft.h" /* ** DEFINE @@ -26,25 +26,13 @@ # define BUFF_SIZE 1024 -#define RED "\x1B[31m" -#define GRN "\x1B[32m" -#define YEL "\x1B[33m" -#define BLU "\x1B[34m" -#define MAG "\x1B[35m" -#define CYN "\x1B[36m" -#define RESET "\x1B[0m" - -/* -** Color for full block tetriminos when printing colored map -** (don't forget to comment all #define COLOR above) -*/ -//#define RED "\e[41m" -//#define GRN "\e[42m" -//#define YEL "\e[43m" -//#define BLU "\e[44m" -//#define MAG "\e[45m" -//#define CYN "\e[46m" -//#define RESET "\e[49m" +# define RED "\x1B[31m" +# define GRN "\x1B[32m" +# define YEL "\x1B[33m" +# define BLU "\x1B[34m" +# define MAG "\x1B[35m" +# define CYN "\x1B[36m" +# define RESET "\x1B[0m" /* ** STRUCTURE @@ -63,7 +51,7 @@ ** next : pointer to next tetrimino ** start : pointer to first tetrimino of input file */ -typedef struct s_fillist +typedef struct s_fillist { unsigned short tetribit; int width; @@ -80,7 +68,15 @@ typedef struct s_fillist struct s_fillist *same; struct s_fillist *next; struct s_fillist *start; -} t_fillist; +} t_fillist; + +/* +** bonus_opti.c +*/ +int check_tetri_memory(t_fillist *list, int pos); +int compare_tetri(t_fillist *tetri_a, t_fillist *tetri_b); +t_fillist *clean_list_memory(t_fillist *list, t_fillist *tmp); +int check_same_tetri(t_fillist *list, int num); /* ** bonus_print.c @@ -89,21 +85,13 @@ t_fillist *print_tetri(t_fillist *list); int print_binary_map(unsigned int *map, int size, int *dope); int print_flags_usage(void); -/* -** bonus_opti.c -*/ -int check_same_tetri(t_fillist *list, int num); -int compare_tetri(t_fillist *tetri_a, t_fillist *tetri_b); -t_fillist *clean_list_memory(t_fillist *list, t_fillist *tmp); -int check_tetri_memory(t_fillist *list, int pos); - /* ** main.c */ -int main(int argc, char **argv); int *create_dope(char *av, int mdp); -void clean_list(t_fillist *list, t_fillist *tmp); int is_mdp(int ac, char **av); +void clean_list(t_fillist *list, t_fillist *tmp); +int main(int argc, char **argv); /* ** read_file.c @@ -122,11 +110,11 @@ int check_tetri_errors_proxy(char *tetri); /* ** parse_input.c */ -int parse_input(char *input, t_fillist **list, int *dope); -int add_to_list(char *sqr, t_fillist **lst, char lett, int *dope); -void fill_list(char line[], t_fillist *list); -unsigned short reduce_tetri(unsigned short tetri, int width); unsigned short tab_to_bin(char line[]); +unsigned short reduce_tetri(unsigned short tetri, int width); +void fill_list(char line[], t_fillist *list); +int add_to_list(char *sqr, t_fillist **lst, char lett, int *dope); +int parse_input(char *input, t_fillist **list, int *dope); /* ** search_map.c @@ -140,11 +128,15 @@ int search_map(t_fillist *list); /* ** print.c */ -void print_sized_map(unsigned int *tab, int width, int height, char letter); +void print_sized_map(unsigned int *tab, int wdth, int hgt, char ltr); char *init_print_map(t_fillist *list, int size); void print_letter_map(t_fillist *list, int size, int flag); -void print_final_map(t_fillist *list, int size); +void print_final_map(t_fillist *list, int size); +/* +** print_map_with_colors.c +*/ +void ft_putchar_color(char c, char color); void ft_put_tetri_color(char c); #endif diff --git a/main.c b/main.c index 494c3cf..3161953 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/02/12 13:20:48 by vmanzoni #+# #+# */ -/* Updated: 2019/06/01 13:51:37 by hulamy ### ########.fr */ +/* Updated: 2019/06/01 14:40:58 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ ** d : debug print (print the map during the backtracking) ** o : optimisation ultra fast but with some errors still ** p : print extended (print the tetri and the map in different formats) -** e : error extended (error message more precise AND no error for too much tetri) +** e : error extended (message more precise AND no error if too much tetri) */ int *create_dope(char *av, int mdp) @@ -95,7 +95,8 @@ int main(int ac, char **av) int mdp; list = NULL; - dope = create_dope(av[3], (mdp = is_mdp(ac, av))); + mdp = is_mdp(ac, av); + dope = create_dope(av[3], mdp); if (ac == 2 || mdp) { if (!(input = read_file(av[1]))) diff --git a/test_files/test_add_to_list.c b/test_files/test_add_to_list.c deleted file mode 100644 index ab16e12..0000000 --- a/test_files/test_add_to_list.c +++ /dev/null @@ -1,95 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* add_to_list.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: hulamy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/04/14 15:20:53 by hulamy #+# #+# */ -/* Updated: 2019/04/30 14:14:32 by hulamy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "fillit.h" -//#include - -/* -** DELETE BEFORE EVAL - TEST FUNCTION -** Function that prints a 16 bites short -*/ -/* -void print_bits(short line) -{ - int mask; - - mask = 1 << 16; - while (mask >>= 1) - (line & mask) ? write(1, "1", 1) : write(1, "0", 1); - write(1, "\n", 1); -} -*/ - -/* -** Function that transforme a tetrminos char* into a short of 16 bites -** and then fills it and its reversed into the list -*/ - -void fill_list(char line[], t_fillist *list) -{ - int i; - - i = 0; - while (line[i]) - { - list->tetribit <<= 1; - if (line[i] == '\n') - i++; - if (line[i++] == '#') - list->tetribit |= 1; - print_bits(list->tetribit, 16); - } -// print_bits(list->tetribit); -} - -/* -** Function that create the linked list and add a new structure -** linked each time needed -*/ - -int add_to_list(char *line, t_fillist **list) -{ - t_fillist *tmp; - - if (!(tmp = (t_fillist*)malloc(sizeof(*tmp)))) - return (0); - if (!(*list)) - tmp->next = NULL; - else - tmp->next = *list; - *list = tmp; - fill_list(line, *list); - return (1); -} - -/* -** DELETE BEFORE EVAL - MAIN FOR TEST -*/ - -//int main(int ac, char **av) -//{ -// static t_fillist *list = NULL; // avant d'appeller add_to_list il faut declarer un pointeur static vers la structure -// int i; -// -// if (ac > 1) -// { -// add_to_list(*(++av), &list); -// while (list && (i = -1)) -// { -// printf("%d\n", list->tetribit); -// print_bits(list->tetribit); -// print_bits(list->tibirtet); -// list = list->next; -// } -// } -// return (0); -//} diff --git a/test_files/test_big_map.c b/test_files/test_big_map.c deleted file mode 100644 index 7e09536..0000000 --- a/test_files/test_big_map.c +++ /dev/null @@ -1,105 +0,0 @@ -#include - -void print_bits(int octet) -{ - unsigned int i; - - i = 1 << 31; - while (i) - { - (octet & i) ? printf("1") : printf("0"); - i >>= 1; - } -} - -void print_short(short octet) -{ - unsigned int i; - - i = 1 << 15; - while (i) - { - (octet & i) ? printf("1") : printf("0"); - i >>= 1; - } -} - -/* -** cette fonction affiche un mask qui se decale le long d'un tableau d'int -** lancer avec gcc test_big_map.c puis ./a.out pour voir -*/ - -void test(unsigned int map[]) -{ - int i; - int j; - int k; - int l; - unsigned int mask; - short tetri; - - tetri = (1 << 4) | (1 << 5) | (1 << 6); - mask = map[9]; - i = -1; - j = 9; - while (j >= 0) - { - // a chaque fois que mask s'est decale jusqu'au int precedant du tableau, le if suivant reaffiche le tableau en une ligne - if (!(++i % 32) && j-- && (k = -1)) - { - while (++k < 10) - print_bits(map[k]); - printf("\n"); - } - // imprime le bon nombre d'espaces pour que le mask s'imprime pile sous le tableau d'int a la position ou il en est - l = 0; - while (l++ < 320 - 32 - i) - printf(" "); - // imprime le mask - print_bits(mask); - printf("\n"); - // compare le mask (int) avec le short et l'imprime s'ils sont compatibles - if (!(tetri & mask)) - { - l = 0; - printf(">>>>>"); - while (l++ < 320 - 32 + 16 - i - 5) - printf(" "); - print_short(tetri); - printf("\n"); - } -/* - else - { - l = 0; - while (l++ < 320 - 32 + 16 - i) - printf(" "); - print_short(tetri); - printf("\n"); - } - l = 0; - while (l++ < 320 - 32 + 16 - i) - printf(" "); - print_short(tetri & mask); - printf("\n"); -*/ - // fait tourner le mask le long du tableau d'int - // (mask >> 1) decale le mask vers la droite (perd le bit le plus a droite et insere un bit a gauche) - // (1 << (i % 32)) creer un mask avec un 1 a la premiere place a droite, puis a la deuxieme, puis la troisieme, etc.. jusqu'a 32, et recommence - // (0000000000000010000000 & map[j]) recupere la valeur du bit de map[j] (qui est la map precedante sur le tableau d'int) a la position du 1 - // (0000000000000010000000 << (31 - (i % 32))) = 100000000000000000000 - // (mask >> 1) | 1000000000000000000000 = insere le 32eme bit dans mask - mask = (mask >> 1) | (((1 << (i % 32)) & map[j]) << (31 - (i % 32))); - } -} - -int main() -{ - unsigned int map[10] = {1568713153, 817645681, 654186132, 538171355, 1718453135, 551286515, 1631843343, 3413834313, 1155555555, 999999999}; - int i; - - //while (i < 10) - // printf("%d\n", map[i++]); - test(map); - return (0); -} diff --git a/test_files/test_big_tetri.c b/test_files/test_big_tetri.c deleted file mode 100644 index e2b5198..0000000 --- a/test_files/test_big_tetri.c +++ /dev/null @@ -1,125 +0,0 @@ -#include "./libft/includes/libft.h" - -void print_bits(unsigned int bits, int size) -{ - unsigned int mask; - - mask = 1 << (size - 1); - while (mask) - { - (bits & mask) ? write(1, "#", 1) : write(1, ".", 1); - write(1, " ", 1); - mask >>= 1; - } - write(1, "\n", 1); -} - -void print_map(unsigned int *tab, int width, int height) -{ - int i; - unsigned int mask; - - i = 0; - // creer un mask avec les size bits de gauche a 1 (ex: 11111110000000000000000000000000) - mask = ~0u << (32 - width); - while (i < width * height) - { - if (!(i % width)) - ft_putchar('\n'); - tab[i / 32] & (1 << (31 - i % 32)) ? ft_putchar('#') : ft_putchar('.'); - ft_putchar(' '); - i++; - } - write(1, "\n", 1); -} - -void find_place(unsigned int *tab, char *line, int size) -{ - unsigned short tetri; - int i; - int j; - unsigned int mask; - unsigned int tmp; - int width; - int height; - - /////////////// create tetri /////////////// - i = 0; - tmp = 0; - width = 3; - height = 2; - while (line[i]) - { - tetri <<= 1; - if (line[i] == '\n') - i++; - if (line[i++] == '#') - tetri |= 1; - } - while (!(tetri & (1 << 15))) - tetri <<= 1; - print_bits(tetri, 16); - tmp = (tetri | tmp) << 16; - print_map(&tmp, width, height); - /////////////// create tetri /////////////// - - mask = ~0; - mask <<= 32 - width; - ft_putendl("mask: "); - print_bits(mask, 32); - i = 0; - while (i < (size - height + 1) * size) - { - tmp = 0; - j = height * size + i; - while (j >= i) - { - tmp >>= width; - tmp |= (mask & (tab[j / 32] << j)); - tmp |= (mask & (tab[(j + size) / 32] >> (32 - j))); - j -= size; - } - print_map(&tmp, width, height); - print_bits(tmp >> 16, 32); - print_bits(tetri, 32); - print_bits((tmp >> 16) & tetri, 32); - if (i % size == size - width) - i += width - 1; - i++; - } -} - -int main(int ac, char **av) -{ - unsigned int tab[8]; - - // ce tableau permet de monter jusqu'a une map de 16*16 - tab[0] = 2656554334; - tab[1] = 1394456818; - tab[2] = 1494256918; - tab[3] = 2656554334; - tab[4] = 1592453883; - tab[5] = 1444352908; - tab[6] = 2154339230; - tab[7] = 1576493154; - print_bits(tab[0], 32); - print_bits(tab[1], 32); - print_bits(tab[2], 32); - print_bits(tab[3], 32); - print_bits(tab[4], 32); - print_bits(tab[5], 32); - print_bits(tab[6], 32); - print_bits(tab[7], 32); - if (ac > 1) - { - ++av; - // mettre l'option "b" pour afficher la troisieme argument en binaire - if (av[0][0] == 'b' && ac > 2) - ft_putendl(ft_convertbase(av[1], "01", "0123456789")); - print_map(tab, 10, 10); - write(1, "\n", 1); - if (av[0][0] == 't' && ac > 2) - find_place(tab, av[1], 10); - } - return (0); -} diff --git a/test_files/test_get_smallest_square.c b/test_files/test_get_smallest_square.c deleted file mode 100644 index 386d4f9..0000000 --- a/test_files/test_get_smallest_square.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* get_smallest_square.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: vmanzoni +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/04/12 22:29:45 by vmanzoni #+# #+# */ -/* Updated: 2019/04/27 15:04:46 by vmanzoni ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "fillit.h" - -/* -** Function to bruteforce with backtracking for smallest square -*/ - -int *get_smallest_square(t_fillist list, int size, unsigned int map[]) -{ - unsigned int mask; - int i; - int j; - - i = 0; - mask = map[size]; - while (list.tetribit != NULL) - { - mask = (mask >> 1) | (((1 << (i % 32)) & map[j]) << (31 - (i % 32))); - if (!(tetri & mask)) - return (1); - i++; - } - return (0); -} diff --git a/test_files/test_mask.c b/test_files/test_mask.c deleted file mode 100644 index 940fc6f..0000000 --- a/test_files/test_mask.c +++ /dev/null @@ -1,92 +0,0 @@ -#include "libft.h" - -void print_bits(int octet) -{ - int i; - int j; - - i = 1 << 12; -// i = 4096; - while (i >>= 1) - (octet & i) ? ft_putnbr(1) : ft_putnbr(0); - ft_putchar('\n'); -} - -void compare(int initial, int compare) -{ - compare ^= (1 << 3); - print_bits(compare); - ft_putchar('\n'); - - /* - // il faut utiliser le & car il transforme - // 0+0=0 1+0=0 0+1=0 mais 1+1=1 - // donc si rien ne se superpose on obtient 0 - // et si un seul bit se superpose on obtient 1 - // (penser a l'utiliser a l'envers donc) - */ - - while (initial & compare) - { - print_bits(initial); - print_bits(compare); - print_bits(initial & compare); - !(initial & compare) ? ft_putendl("&: fit") : ft_putendl("&: not fit"); - compare >>= 1; - } - print_bits(initial); - print_bits(compare); - print_bits(initial & compare); - !(initial & compare) ? ft_putendl("&: fit") : ft_putendl("&: not fit"); - -/* - j = 1 << 12; - while (j >>= 1) - (initial | compare) & j ? ft_putnbr(1) : ft_putnbr(0); - ft_putchar('\n'); - (initial | compare) ? ft_putendl("|: fit") : ft_putendl("|: not fit"); - j = 1 << 12; - while (j >>= 1) - (initial ^ compare) & j ? ft_putnbr(1) : ft_putnbr(0); - ft_putchar('\n'); - (initial ^ compare) ? ft_putendl("^: fit") : ft_putendl("^: not fit"); -*/ -} - -int main(int ac, char **av) -{ - int i; - int j; - -// i = 56173; -// j = 9362; - i = 9622; - j = 27; - if (ac > 0) - { - if (ac > 1) - ft_putendl(ft_convertbase(av[1], "0123456789", "01")); - print_bits(i); - print_bits(j); - compare(i, j); - } - return (0); -} - -/* -int main(int ac, char **av) -{ - int init; - int mask; - - mask = (1 << 4); - if (ac == 4) - { - ft_putendl(ft_convertbase(av[1], av[2], av[3])); - ft_putnbrendl(init); - init |= mask; - ft_putnbrendl(init); - } - return (0); -} -*/ diff --git a/test_files/test_othermethod_fillit.c b/test_files/test_othermethod_fillit.c deleted file mode 100644 index 22cc411..0000000 --- a/test_files/test_othermethod_fillit.c +++ /dev/null @@ -1,266 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* test_othermethod_fillit.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: vmanzoni +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/05/01 15:25:46 by vmanzoni #+# #+# */ -/* Updated: 2019/05/02 17:08:26 by vmanzoni ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft/includes/libft.h" -#include - -typedef struct s_fillist -{ - unsigned int tetribit; - int position; - struct s_fillist *next; -} t_fillist; - -/* ***************************************************************************** -** TEST FUNCTION -***************************************************************************** */ - -void print_bits(char *str, unsigned int bits, int size) -{ - unsigned int mask; - - ft_putstr(str); - mask = 1 << (size - 1); - while (mask) - { - (bits & mask) ? write(1, "1", 1) : write(1, "0", 1); - mask >>= 1; - } - write(1, "\n", 1); -} - -void print_tetri(unsigned int bits, int size) -{ - unsigned int mask; - short i; - - i = 0; - mask = 1 << (size - 1); - while (mask) - { - if (i % 4 == 0) - write(1, "\n", 1); - (bits & mask) ? write(1, "#", 1) : write(1, ".", 1); - mask >>= 1; - i++; - } - write(1, "\n", 1); -} - -/* ***************************************************************************** -** PROGRAM -***************************************************************************** */ - -void error(char *str) -{ - ft_putstr(str); - exit(0); -} - -char *read_file(char *file) -{ - char buf[1000]; - int fd; - int rv; - int i; - char *result; - - if (((fd = open(file, O_RDONLY)) < 0) \ - || ((rv = read(fd, &buf, 130)) < 0) \ - || !(result = malloc(sizeof(char) * rv))) - return (NULL); - buf[rv] = '\0'; - i = -1; - while (buf[++i]) - result[i] = buf[i]; - close(fd); - return (result); -} - -unsigned int tetribit_resize(unsigned int tetribit, - unsigned int from_size, - unsigned int to_size) -{ - unsigned int mask; - unsigned int i; - unsigned int j; - unsigned int ret; - unsigned int new_tet; - - i = from_size; - mask = 0; - while (i--) - mask |= (1 << i) + 1; //Cree un mask de from_size en bit -// print_bits("tet: ", tetribit, 32); -// print_bits("mak: ", mask, 32); - new_tet = tetribit & mask; //initialise new_tet avec la ligne du bas de tetribit -// print_bits("new: ", new_tet, 32); -// ft_putstr("while\n"); - j = 0; - while (j < from_size - 1) //pour chaque ligne du tetribit - { - i = 0; - mask = 0; - while (i != from_size) //on fait un mask sur chaque ligne - mask |= 1 << (from_size + (from_size * j) + i++); -// print_bits("tet: ", tetribit, 32); -// print_bits("mak: ", mask, 32); - ret = (tetribit & mask); -// print_bits("rbf: ",ret, 32); - ret = ret << (to_size - from_size + (j * (to_size - from_size))); -// print_bits("raf: ",ret, 32); - new_tet |= ret; -// print_bits("new: ",new_tet, 32); - j++; - } - return (new_tet); -} - -unsigned int tetri_to_binary(char line[]) -{ - unsigned int tmp; - int i; -// unsigned int mask; - - i = 0; - tmp = 0; - while (line[i]) - { - tmp <<= 1; - if (line[i] == '\n') - i++; - if (line[i++] == '#') - tmp |= 1; - } -// mask = (1 << 3 | 1 << 2 | 1 << 1) + 1; -// while ((tmp & mask) < 8) //place le tetri le plus en bas a droite possible sans le casser -// tmp /= 2; -// print_bits("before\n", tmp, 32); -// print_bits("after\n", tetribit_resize(tmp, 4, 5), 32); - return (tmp); -} - -int add_to_list(char *line, t_fillist **list) -{ - t_fillist *tmp; - t_fillist *test; - - if (!(tmp = (t_fillist*)malloc(sizeof(*tmp)))) - return (0); - tmp->next = NULL; - test = *list; - if (!test) - *list = tmp; - else - { - while(test->next) - test = test->next; - test->next = tmp; - } - tmp->tetribit = tetri_to_binary(line); - return (1); -} - -unsigned int *init_map(int i) -{ - unsigned int *new; - int size; - - size = (i * i) / 32 + 1; - new = (unsigned int *)malloc(sizeof(*new) * size); - while (size) - new[size--] = 0; - return (new); -} - -int find_place(unsigned int *map, t_fillist *list) -{ - ft_putstr(">>>\n"); - print_bits("map: ", *map, 32); - print_bits("lst: ", list->tetribit, 32); - print_bits("m&l: ", (*map & (short)(list->tetribit)), 32); - while ((*map & (int)(list->tetribit)) == 0 - && ((int)(list->tetribit) & (1 << 31))) - { - list->tetribit = list->tetribit << 1; - print_bits("lst: ", list->tetribit, 32); - ft_putnbr(list->tetribit); - } - return (0); -} - -int backtracking(t_fillist *list) -{ - unsigned int *map; - - map = init_map(1); - if (!list) - return (0); -// while (list) -// { -// print_bits("test: ", list->tetribit, 16); -// list = list->next; -// } - find_place(map, list); -// list = list->next; -// find_place(map, list); - /* - while (find_place(map, list)) - { - add_remove(map, list, size, list->position); - if (fill_map(map, list->next)) - return (1); - add_remove(map, list, size, list->position); - list->position++; - } - */ - return (0); -// mask = (mask >> 1) | (((1 << (i % 32)) & map[j]) << (31 - (i % 32))); -} - -void parse_file(char *file, t_fillist *list) -{ - char tetri[20]; - int i; - int j; - - i = 0; - while (file[i]) - { - if (file[i] != '.' && file[i] != '#' && file[i] != '\n') - error("error.\n"); - j = 0; - while (j < 19) - tetri[j++] = file[i++]; - tetri[19] = '\0'; - add_to_list(tetri, &list); - while (file[i] && file[i] != '.' && file[i] != '#') - i++; - } - backtracking(list); -} - -int solve(char *file) -{ - static t_fillist *list = NULL; - - parse_file(read_file(file), list); - return (1); -} - -int main(int ac, char **av) -{ - if (ac == 2) - solve(av[1]); - else - error("usage: ./fillit [file]\n"); -} diff --git a/test_files/test_print_fillit.c b/test_files/test_print_fillit.c deleted file mode 100644 index cf906a5..0000000 --- a/test_files/test_print_fillit.c +++ /dev/null @@ -1,20 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* print_fillit.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: vmanzoni +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/03/01 13:35:48 by vmanzoni #+# #+# */ -/* Updated: 2019/04/16 16:41:46 by vmanzoni ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "fillit.h" - -/* -void ft_print_fillit() -{ - -} -*/ diff --git a/test_files/test_search_map2.c b/test_files/test_search_map2.c deleted file mode 100644 index 65f21c8..0000000 --- a/test_files/test_search_map2.c +++ /dev/null @@ -1,382 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* search_map.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: hulamy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/04/27 20:47:22 by hulamy #+# #+# */ -/* Updated: 2019/05/24 14:41:08 by hulamy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "fillit.h" - -/* -** OLD VERSION of find_place and fit_in_place all in one but with a lot of divisions and modulo -** -** int find_place(unsigned int *tab, t_fillist *list, int size, int pos) -** { -** int i; -** int j; -** unsigned int mask; -** unsigned int tmp; -** -** mask = ~0u << (32 - list->width); -** tmp = mask; -** i = pos; -** while (i < (size - list->height + 1) * size) -** { -** if (i % size == size - list->width + 1) -** i += list->width - 1; -** else -** { -** tmp = 0; -** j = (list->height - 1) * size + i; -** while (j >= i) -** { -** tmp >>= list->width; -** tmp |= (mask & (tab[j / 32] << j)); -** tmp |= (mask & (tab[(j + list->width) / 32] >> (32 - j))); -** j -= size; -** } -** if (!((tmp >> 16) & list->tetribit)) -** return (i + 1); -** i++; -** } -** } -** return (0); -** } -*/ - -/* -** function that try to optimize the speed -** SECOND ATTEMPT -** by finding the isolated dots whille fillling the map -** untill they get more numerous than the max amount available -** TOTAL FAILURE :p -** -** int check_others(unsigned int *map, t_fillist *list, int size, int num) -** { -** t_fillist *tmp; -** int dots; -** int total; -** int i; -** int j; -** -** dots = 0; -** i = -1; -** num *= 4; -** total = size * size; -** while (++i < total && dots <= total - num) -** { -** tmp = list->next; -** j = 1; -** // saute les position pas vides -** while (1 << (i % 32) & map[i % 32]) -** i++; -** // pour chaque position vide regarde si chaque tetri non encode places peuvent y rentrer -** while (j && tmp) -** { -** // si le tetri est trop a droite ou trop en bas il ne rentre pas ce n'est pas la peine de chercher donc on passe au tetri suivant -** if (tmp->width > (size - i % size) || (total - i) <= (tmp->height * size)) -** tmp = tmp->next; -** // sinon verifier si on peut le mettre a cette position et si on ne peut pas passer au tetri suivant -** else if (!fit_in_place(map, list, size, i)) -** tmp = tmp->next; -** // si le tetri peut rentrer on arrete la boucle en mettant j = 0 -** else -** j = 0; -** } -** // si j existe c que le tetri ne pouvait pas etre place donc on rajoute 1 au compteur de point isoles -** if (j) -** dots++; -** } -** return (dots > total - num); -** } -*/ - -/* -** function that try to optimize the speed -** FIRST ATTEMPT -** by verifying if the place left is enough to place at least -** one of each tetri left -** TOTAL FAILURE :p -** -** int check_others(unsigned int *map, t_fillist *list, int size, int num) -** { -** t_fillist *tmp; -** -** // verifie que les tetri restant puissent un par un se placer sur la map -** // ca n'optimise qu'en fin de map donc ca ralentit les grosses map en fait -** while ((tmp = tmp->next)) -** if (!find_place(map, tmp, size, 0)) -** return (0); -** return (1); -** } -*/ - -/* -** function that look if a tretri fit in a place -*/ - -unsigned int fit_in_place(unsigned int *map, t_fillist *lst, int size, int i) -{ - unsigned int tmp; - unsigned int mask; - unsigned int tetri; - int n; - int r; - - n = lst->num; - r = lst->rank; - i = lst->height; - tetri = lst->tetribit << 16 >> lst->width; - tmp = 0; - mask = ~0u << (32 - lst->width); - while (i--) - { - if (tmp & tetri) - return (0); - if (r >= 32 && ++n) - r -= 32; - tmp = (mask & (map[n] << r)) | (mask & (map[n + 1] >> (32 - r))); - tetri <<= lst->width; - r += size; - } - return (!(tmp & tetri)); -} - -/* -** function that look for the first place in the map for a tetri -*/ - -int find_place(unsigned int *map, t_fillist *list, int size) -{ - int limit; - int pos; - - pos = list->position; - list->place = pos % size; - list->rank = pos % 32; - list->num = pos / 32; - limit = (size - list->height + 1) * size; - while (pos < limit) - { - if (list->rank >= 32 && ++list->num) - list->rank -= 32; - if (list->place > size - list->width) - { - list->place = -1; - pos += list->width - 2; - list->rank += list->width - 2; - } - else if (fit_in_place(map, list, size, 0)) - return ((list->position = pos + 1)); - pos++; - list->place++; - list->rank++; - } - return (0); -} - -/* -** function that add or remove a tetri on the map -*/ - -void add_remove(unsigned int *map, t_fillist *list, int size) -{ - unsigned int mask; - unsigned short tetri; - int i; - int j; - - tetri = list->tetribit; - mask = ~0u << (32 - list->width); - i = (list->height - 1) * list->width; - j = (list->height - 1) * size + list->position; - while (j >= list->position) - { - map[(j - 1) / 32] ^= (mask & tetri << (16 + i)) >> (j - 1); - map[(j - 1) / 32 + 1] ^= (mask & tetri << (16 + i)) << (32 - j) << 1; - j -= size; - i -= list->width; - } -} - -/* -** Test optimisation for not testing wrong maps when tetri are identical -*/ - -int check_tetri_memory(t_fillist *list, int pos) -{ - t_fillist *tetri; - unsigned int mask; - - tetri = list; - mask = 1 << ((pos % 32) - 1); - if (tetri->same) - { - if (!(tetri->same->memory[pos / 32] & mask)) - return (tetri->same->memory[pos / 32] |= mask); - } - else - { -// if (!(tetri->memory[pos / 32] & mask)) - return (tetri->memory[pos / 32] |= mask); - } - return (0); -} - -void remove_tetri_memory(t_fillist *list, int pos) -{ - t_fillist *tetri; - unsigned int mask; - - tetri = list; - mask = 1 << ((pos % 32) - 1); - if (tetri->same != NULL) - { - if ((tetri->same->memory[pos / 32] & mask)) - tetri->same->memory[pos / 32] ^= mask; - } - else - { - if ((tetri->memory[pos / 32] & mask)) - tetri->memory[pos / 32] ^= mask; - } -} - -/* -** Function that recursively try to fill the map with the tetris -*/ - -int fill_map(unsigned int *map, t_fillist *list, int size, t_fillist *link) -{ - if (!list) - return (1); - list->position = 0; - while (find_place(map, list, size)) - { - add_remove(map, list, size); - - list->test = 1; // DEBUG -// print_final_map(link, size, 1); // DEBUG -// ft_putchar('\n'); // DEBUG - -// if (check_tetri_memory(list, list->position)) - if (fill_map(map, list->next, size, link)) - return (1); - add_remove(map, list, size); -// remove_tetri_memory(list, list->position); - - list->test = 0; // DEBUG - - } - return (0); -} - -/* -** Test optimisation for not testing wrong maps when tetri are identical -*/ - -int compare_tetri(t_fillist *tetri_a, t_fillist *tetri_b) -{ - if (tetri_a->tetribit != tetri_b->tetribit) - return (0); - if (tetri_a->width != tetri_b->width) - return (0); - if (tetri_a->height != tetri_b->height) - return (0); - return (1); -} - -/* -** Test optimisation for not testing wrong maps when tetri are identical -*/ - -int check_same_tetri(t_fillist *list, int num) -{ - t_fillist *curr_tetri; - t_fillist *next_tetri; - int i; - - curr_tetri = list; - while (curr_tetri != NULL) - { - i = 0; - if (!(curr_tetri->memory = (unsigned int *)malloc(sizeof(*curr_tetri->memory) * num))) - return (0); - while (i < num) - curr_tetri->memory[i++] = 0; - next_tetri = curr_tetri->next; - while (next_tetri != NULL) - { - if (compare_tetri(curr_tetri, next_tetri)) - if (next_tetri->same == NULL) - next_tetri->same = curr_tetri; - next_tetri = next_tetri->next; - } - curr_tetri = curr_tetri->next; - } - return (0); -} - -/* -** function that send to "fill_map" a map of a certain size and increment its size untill it's solved -*/ - -int search_map(t_fillist *list) -{ - t_fillist *tmp; - unsigned int *map; - int size; - int num; - int i; - - /////////////////////////////////////////////////// TEST - unsigned int print; - tmp = list; - while (tmp) - { - // imression pour tests - check_same_tetri(list, 1); - print = tmp->tetribit; - print <<= 16; - print_map(&print, tmp->width, tmp->height, tmp->letter); - if (tmp->same) - { - print = tmp->same->tetribit; - print <<= 16; - ft_putstr("same --> "); - ft_putchar(tmp->same->letter); - ft_putchar('\n'); - } - ft_putchar('\n'); - tmp = tmp->next; - } - /////////////////////////////////////////////////// TEST - - size = 2; - num = 1; - tmp = list; - while ((tmp = tmp->next)) - num++; - while (size * size < num * 4) - size++; - i = 0; - while (!i) - { - ft_putnbrendl(size); - num = (size * size) / 32 + 1; - if (!(map = (unsigned int *)malloc(sizeof(*map) * num))) - return (0); - check_same_tetri(list, num); - while (num) - map[num--] = 0; - i = fill_map(map, list, size++, list); - } - ft_putendl("result in binary :"); // DEBUG (pas dans le main car besoin de map) - print_map(map, size - 1, size - 1, '#'); // DEBUG - return (--size); -}