diff --git a/.gitignore b/.gitignore index e525c97..9b97ae7 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,11 @@ objs/ a\.out\.dSYM/ a\.out + +*.swo + +*.swp + +fillit + +test_fillit\.c diff --git a/.parse_input.c.swo b/.parse_input.c.swo deleted file mode 100644 index 90e58e0..0000000 Binary files a/.parse_input.c.swo and /dev/null differ diff --git a/.parse_input.c.swp b/.parse_input.c.swp deleted file mode 100644 index b44adaa..0000000 Binary files a/.parse_input.c.swp and /dev/null differ diff --git a/.test_mask.c.swp b/.test_mask.c.swp deleted file mode 100644 index 6204ff1..0000000 Binary files a/.test_mask.c.swp and /dev/null differ diff --git a/Makefile b/Makefile index 876fac0..6a9955a 100644 --- a/Makefile +++ b/Makefile @@ -6,22 +6,24 @@ # By: vmanzoni +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/03/01 13:24:35 by vmanzoni #+# #+# # -# Updated: 2019/04/18 12:27:12 by vmanzoni ### ########.fr # +# Updated: 2019/04/23 15:16:05 by vmanzoni ### ########.fr # # # # **************************************************************************** # NAME = fillit -OBJ_DIR = objs./ +OBJ_DIR = ./objs HEADER = fillit.h SRCS = main.c \ read_file.c \ handle_errors.c \ parse_input.c \ - get_smallest_square.c \ + add_to_list.c \ +# get_smallest_square.c \ print_fillit.c + OBJS = $(SRCS:.c=.o) LIB = libft/ @@ -33,7 +35,7 @@ RM = rm -rf all: $(NAME) $(NAME): - make -C libft/ + make -C $(LIB) $(CC) $(CFLAGS) -I$(HEADER) -c $(SRCS) $(CC) -o $(NAME) $(OBJS) -L $(LIB) -lft mkdir $(OBJ_DIR) diff --git a/add_to_list.c b/add_to_list.c index 65bd325..cbb5c98 100644 --- a/add_to_list.c +++ b/add_to_list.c @@ -6,16 +6,17 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/14 15:20:53 by hulamy #+# #+# */ -/* Updated: 2019/04/22 15:10:14 by hulamy ### ########.fr */ +/* Updated: 2019/04/24 12:15:47 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #include "fillit.h" #include -#include "libft/includes/libft.h" +//#include "libft/includes/libft.h" /* -** this function prints a 16 bites short +** DELETE BEFORE EVAL - TEST FUNCTION +** Function that prints a 16 bites short */ void print_bits(short line) @@ -24,45 +25,44 @@ void print_bits(short line) mask = 1 << 27; while (mask >>= 1) - (line & mask) ? ft_putnbr(1) : ft_putnbr(0); - ft_putchar('\n'); + (line & mask) ? write(1, "1", 1) : write(1, "0", 1); + write(1, "\n", 1); } /* -** this function transforme a tetrminos char* into a short of 16 bites +** Function that transforme a tetrminos char* into a short of 16 bites ** then it fills it and its reverse into the list */ -int fill_list(char *line, t_fillist *list) +int fill_list(char line[], t_fillist *list) { - short tmp; - int test; +// short tmp; + int i; - while (*line) + i = 0; + while (line[i]) { list->tetribit <<= 1; - if (*line == '\n') - line++; - if (*(line++) == '#') + if (line[i] == '\n') + i++; + if (line[i++] == '#') list->tetribit |= 1; } - tmp = list->tetribit; - while (tmp) - { - if (tmp & 1) - list->tibirtet |= 1; - list->tibirtet <<= 1; - tmp >>= 1; - } - test = list->tibirtet; - print_bits(test); - while (test <<= 1) - print_bits(test); +// tmp = list->tetribit; +// while (tmp) +// { +// if (tmp & 1) +// list->tibirtet |= 1; +// list->tibirtet <<= 1; +// tmp >>= 1; +// } + print_bits(list->tetribit); return (0); } /* -** this function create the linked list and add a new structure linked each time needed +** Function that create the linked list and add a new structure +** linked each time needed */ int add_to_list(char *line, t_fillist **list) @@ -80,6 +80,11 @@ int add_to_list(char *line, t_fillist **list) return (1); } +/* +** DELETE BEFORE EVAL - TEST FUNCTION +** Print octet +*/ + void print_test(int octet) { unsigned int i; @@ -92,6 +97,11 @@ void print_test(int octet) } } +/* +** DELETE BEFORE EVAL - TEST FUNCTION +** Test for big map +*/ + void test(unsigned int map[]) { int i; @@ -121,6 +131,11 @@ void test(unsigned int map[]) } } +/* +** 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 @@ -142,4 +157,4 @@ int main(int ac, char **av) return (0); } - +*/ diff --git a/fillit b/fillit deleted file mode 100755 index bd83e46..0000000 Binary files a/fillit and /dev/null differ diff --git a/fillit.h b/fillit.h index 94895c2..0fbd67c 100644 --- a/fillit.h +++ b/fillit.h @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/01 13:34:46 by vmanzoni #+# #+# */ -/* Updated: 2019/04/19 14:52:10 by hulamy ### ########.fr */ +/* Updated: 2019/04/23 16:19:19 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,11 +16,9 @@ #include #include // for system call write #include // for system call open -#include // for memmove and strlen -#include // for debug printf -#include // to use bool type +#include // for printf (DELETE BEFORE EVAL) -# define BUFF_SIZE 1024 +#include "libft/includes/libft.h" /* ** STRUCTURE @@ -43,7 +41,8 @@ void print_error(char *s); void parse_input(char *input); int check_file_errors(char *file); int check_tetri_errors(char *tetri); -int check_tetri_errors2(char *tetri); +int check_tetri_errors_proxy(char *tetri); int add_to_list(char *square, t_fillist **list); +int fill_list(char *line, t_fillist *list); #endif diff --git a/get_smallest_square.c b/get_smallest_square.c index ad03c58..4a4c8d2 100644 --- a/get_smallest_square.c +++ b/get_smallest_square.c @@ -6,15 +6,43 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/12 22:29:45 by vmanzoni #+# #+# */ -/* Updated: 2019/04/14 21:37:19 by vmanzoni ### ########.fr */ +/* Updated: 2019/04/23 21:42:35 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ #include "fillit.h" /* -char *get_smallest_square() -{ - -} +** Function that initialize a int map equivalent to a square size*size */ + +unsigned int *initialize_map(int size) +{ + unsigned int *map[0]; + + while (size--) + map[size] = 0; + return (map); +} + +/* +** Function that 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/handle_errors.c b/handle_errors.c index 2f5a907..94d5932 100644 --- a/handle_errors.c +++ b/handle_errors.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/01 13:29:05 by vmanzoni #+# #+# */ -/* Updated: 2019/04/15 14:41:19 by vmanzoni ### ########.fr */ +/* Updated: 2019/04/23 14:05:16 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -75,7 +75,7 @@ int check_tetri_errors(char *tetri) dot++; i++; } - if (htg != 4 || dot != 12 || check_tetri_errors2(tetri)) + if (htg != 4 || dot != 12 || check_tetri_errors_proxy(tetri)) return (1); return (0); } @@ -84,7 +84,7 @@ int check_tetri_errors(char *tetri) ** Function that check if tetrimino parts are linked */ -int check_tetri_errors2(char *tetri) +int check_tetri_errors_proxy(char *tetri) { int i; diff --git a/main.c b/main.c index d7dfc4b..2448ba8 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/02/12 13:20:48 by vmanzoni #+# #+# */ -/* Updated: 2019/04/16 16:41:28 by vmanzoni ### ########.fr */ +/* Updated: 2019/04/23 21:39:42 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/parse_input.c b/parse_input.c index 9dbda39..49ce03e 100644 --- a/parse_input.c +++ b/parse_input.c @@ -6,29 +6,49 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/15 14:48:14 by vmanzoni #+# #+# */ -/* Updated: 2019/04/18 16:09:24 by vmanzoni ### ########.fr */ -/* Updated: 2019/04/19 12:53:40 by hulamy ### ########.fr */ +/* Updated: 2019/04/23 21:23:47 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ #include "fillit.h" /* -** !!! DELETE PRINT_BITS BEFORE EVAL !!! +** Function that parse a file and put each tetrimino in a linked list */ -void print_bits(short octet) +void parse_input(char *input) { - int i; + static t_fillist *list = NULL; + char tetri[20]; + int i; + int j; - i = 1 << 16; - while (i >>= 1) - (octet & i) ? write(1, "1", 1) : write(1, "0", 1); - printf("\n"); + i = 0; + printf("%s", input); + while (input[i]) + { + j = 0; + while (j < 19) + tetri[j++] = input[i++]; + tetri[19] = '\0'; + if (check_tetri_errors(tetri)) + print_error("Error: Tetrimino not valid."); + add_to_list(tetri, &list); + printf("added to list !!\n"); + while (input[i] && input[i] != '.' && input[i] != '#') + i++; + } +/*DEBUG PART - Print each tetribit*/ +// while (list != NULL) +// { +// printf("%i\n", list->tetribit); +// printf("\n"); +// list = list->next; +// } } - /* +** DELETE BEFORE EVAL - NOT USED ANYMORE ** Function that parse a file and put each tetrimino in a linked list */ @@ -55,63 +75,3 @@ char **create_square(char *tetri) } return (square); } - -short create_tetribit(char *tetri) -{ - short tetribit; - int i; - - i = 0; - tetribit = 0; - while (tetri[i]) - { - if (tetri[i] != '\n') // Pour comparer avec le tetri en char - printf("%c", tetri[i]); - if (tetri[i] == '#') - tetribit = tetribit | 1; - else - tetribit <<= 1; - i++; - } - return (tetribit); -} - -void parse_input(char *input) -{ - static t_fillist *list = NULL; - char tetri[20]; - int i; - int j; - short test; //DELETE BEFORE EVAL - - i = 0; - printf("input: %s\n", input); - printf("end\n"); - while (input[i]) - { - j = 0; - while (j < 19) - tetri[j++] = input[i++]; - tetri[19] = '\0'; - if (check_tetri_errors(tetri)) - print_error("Error: Tetrimino not valid."); - test = create_tetribit(tetri); - printf("\n"); - print_bits(test); - //test = create_square(tetri); - //add_to_list(test, &list); - while (input[i] == '\n') - i++; - } - /* - ** ce qui suit sert juste a afficher le contenu de tous les tetraminos de la liste chainee - ** pour debug, a effacer au rendu - */ - while (list && (i = -1)) - { - while (++i < list->size[1]) - printf("%s\n", list->tetraminos[i]); - printf("\n"); - list = list->next; - } -} diff --git a/read_file.c b/read_file.c index 0afb78c..72f1e58 100644 --- a/read_file.c +++ b/read_file.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/13 12:09:46 by vmanzoni #+# #+# */ -/* Updated: 2019/04/19 12:50:32 by hulamy ### ########.fr */ +/* Updated: 2019/04/22 15:16:18 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,12 +29,9 @@ char *read_file(char *file) || !(result = malloc(sizeof(char) * rv))) return (NULL); buf[rv] = '\0'; - i = 0; - while (buf[i]) - { + i = -1; + while (buf[++i]) result[i] = buf[i]; - i++; - } result[i] = '\0'; close(fd); return (result); diff --git a/samples/1tetri b/samples/1tetri index 3f1734a..caa819e 100644 --- a/samples/1tetri +++ b/samples/1tetri @@ -1,4 +1,4 @@ -...# -...# -...# -...# +#... +#... +#... +#... diff --git a/samples/2tetri b/samples/2tetri new file mode 100644 index 0000000..26e4931 --- /dev/null +++ b/samples/2tetri @@ -0,0 +1,9 @@ +...# +...# +...# +...# + +.... +##.. +.##. +.... diff --git a/samples/3tetri b/samples/3tetri new file mode 100644 index 0000000..84fea70 --- /dev/null +++ b/samples/3tetri @@ -0,0 +1,14 @@ +...# +...# +...# +...# + +.... +##.. +.##. +.... + +.... +###. +.#.. +.... diff --git a/samples/4tetri b/samples/4tetri new file mode 100644 index 0000000..98a0c27 --- /dev/null +++ b/samples/4tetri @@ -0,0 +1,19 @@ +...# +...# +...# +...# + +...# +...# +...# +...# + +...# +...# +...# +...# + +...# +...# +...# +...# diff --git a/samples/difficile_ou_pas b/samples/difficile_ou_pas index aac9474..d0c08a8 100644 --- a/samples/difficile_ou_pas +++ b/samples/difficile_ou_pas @@ -111,4 +111,4 @@ .... .... #### -....iskjdslkfsljklfdjk +....i diff --git a/samples/one b/samples/test_bug similarity index 50% rename from samples/one rename to samples/test_bug index 5013175..0e8a3bd 100644 --- a/samples/one +++ b/samples/test_bug @@ -1,4 +1,4 @@ +#... +###. .... .... -.... -#### diff --git a/test_big_map.c b/test_big_map.c index 58f4b09..7e09536 100644 --- a/test_big_map.c +++ b/test_big_map.c @@ -98,13 +98,8 @@ int main() unsigned int map[10] = {1568713153, 817645681, 654186132, 538171355, 1718453135, 551286515, 1631843343, 3413834313, 1155555555, 999999999}; int i; - i = -1; - while (++i < 10) - { - print_bits(map[i]); - printf(" "); - } - printf("\n"); + //while (i < 10) + // printf("%d\n", map[i++]); test(map); return (0); } diff --git a/test_fillit.c b/test_fillit.c new file mode 100644 index 0000000..3704f2e --- /dev/null +++ b/test_fillit.c @@ -0,0 +1,272 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_fillit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vmanzoni +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/04/22 16:05:59 by vmanzoni #+# #+# */ +/* Updated: 2019/04/23 13:57:22 by vmanzoni ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include // for system call write +#include // for system call open +#include // for memmove and strlen +#include // for debug printf +#include // to use bool type + +#include "libft/includes/libft.h" + +/* +** STRUCTURE +*/ + +typedef struct s_fillist +{ + int tetribit; + int position; + struct s_fillist *next; +} t_fillist; + +/* +** Function that display error message *s on fd and exit program +*/ + +void print_error(char *s) +{ + write(2, s, strlen(s)); + exit(1); +} + +/* +** Function that read file +*/ + +char *read_file(char *file) +{ + char buf[BUFF_SIZE]; + int fd; + int rv; + int i; + char *result; + + if (((fd = open(file, O_RDONLY)) < 0) \ + || ((rv = read(fd, &buf, BUFF_SIZE)) < 0) \ + || !(result = malloc(sizeof(char) * rv))) + return (NULL); + buf[rv] = '\0'; + i = -1; + while (buf[++i]) + result[i] = buf[i]; + result[i] = '\0'; + close(fd); + return (result); +} + +/* +** Function to see if there if an error if the file +** - less than 4 lines +** - more than 104 (26 tetri) + 25 (\n) = 129 lines +** - two \n in a row +*/ + +int check_file_errors(char *file) +{ + int i; + int line_nbr; + + i = 0; + line_nbr = 0; + while (file[i]) + { + if (file[i] != '.' && file[i] != '#' && file[i] != '\n') + return (1); + if (file[i] == '\n') + line_nbr++; + if (file[i] == '\n' && file[i+1] != '\0' && \ + file[i+2] != '.' && file[i+2] != '#') + return (1); + i++; + } + if (line_nbr < 4 || line_nbr > 129) + return (1); + return (0); +} + +/* +** Function that check if tetrimino parts are linked +*/ + +int check_tetri_errors2(char *tetri) +{ + int i; + + i = 0; + while (tetri[i]) + { + if (tetri[i] == '.' || tetri[i] == '\n') + i++; + else if (tetri[i] == '#' && (tetri[i + 1] == '#' || tetri[i - 1] == '#' + || tetri[i + 5] == '#' || tetri[i - 5] == '#')) + i++; + else + return (1); + } + return (0); +} + +/* +** Function that check if tetrimino square contains: +** - 4 '#' +** - 12 '.' +*/ + +int check_tetri_errors(char *tetri) +{ + int i; + int htg; + int dot; + + i = 0; + htg = 0; + dot = 0; + while (tetri[i]) + { + if (tetri[i] == '#') + htg++; + else if (tetri[i] == '.') + dot++; + i++; + } + if (htg != 4 || dot != 12 || check_tetri_errors2(tetri)) + return (1); + return (0); +} + +/* +** Function that take char * tetri and convert to short and put it in a list +*/ + +int fill_list(char *line, t_fillist *list) +{ + short tmp; + short tet; + int i; + + i = -1; + tet = 0; + while (line[++i]) + { + tet <<= 1; + if (line[i] == '\n') + i++; + if (line[i] == '#') + tet |= 1; + } + return (0); +} + +/* +** this function 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); +} + +/* +** Function that parse a file and put each tetrimino in a linked list +*/ + +int parse_input(char *input, t_fillist list) +{ + //static t_fillist *list = NULL; + static int size; + char tetri[20]; + int i; + int j; + + i = 0; + size = 0; + while (input[i]) + { + j = 0; + while (j < 19) + tetri[j++] = input[i++]; + tetri[19] = '\0'; + if (check_tetri_errors(tetri)) + print_error("Error: Tetrimino not valid."); + add_to_list(tetri, &list); + while (input[i] == '\n') + i++; + size++; + } + printf("%i\n", size); + return (size); +} + +/* ***************************************************************************** +** Function that brute force with backtracking for smallest square with tetris +***************************************************************************** */ + +int get_smallest_square(t_fillist list, int size, unsigned int map[]) +{ + unsigned int mask; + int i; + int j; + + i = 0; + j = 0; + mask = map[size]; + while (list.next != NULL) + { + mask = (mask >> 1) | (((1 << (i % 32)) & map[j]) << (31 - (i % 32))); + if (!(list.tetribit & mask)) + return (1); + i++; + } + return (0); +} + +/* ***************************************************************************** +** MAIN +***************************************************************************** */ + +int main(int argc, char **argv) +{ + char *input; + int map; + int size; + t_fillist list; + + if (argc == 2) + { + if (!(input = read_file(argv[1]))) + print_error("Error: Could not read file.\n"); + if (check_file_errors(input)) + print_error("Error: Invalid file.\n"); + size = parse_input(input, list); + get_smallest_square(list, size, map); + /* + Backtracking for smallest square + Transform tetriminos with letters + Print result + */ + } + else + print_error("Usage: Please submit a file.\n"); + return (0); +} diff --git a/test_mask.c b/test_mask.c index c8d0d42..940fc6f 100644 --- a/test_mask.c +++ b/test_mask.c @@ -40,16 +40,16 @@ void compare(int initial, int 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"); + 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"); */ } @@ -74,19 +74,19 @@ int main(int ac, char **av) } /* -**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); -**} +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); +} */