norm and no more submodule

This commit is contained in:
Jack Halford 2016-11-03 17:45:01 +01:00
parent a7bb8aa364
commit 7edaabc3a1
11 changed files with 131 additions and 98 deletions

3
fillit/.gitmodules vendored
View file

@ -1,3 +0,0 @@
[submodule "libft"]
path = libft
url = https://github.com/jzck/libft.git

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fillit.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 17:38:16 by jhalford #+# #+# */
/* Updated: 2016/11/03 17:39:41 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FILLIT_H #ifndef FILLIT_H
# define FILLIT_H # define FILLIT_H
# define BUF_SIZE 32 # define BUF_SIZE 32
@ -9,18 +21,21 @@ extern int g_target;
extern int g_ttmn; extern int g_ttmn;
extern char **g_sol; extern char **g_sol;
typedef struct s_stack struct s_stack
{ {
char id; char id;
int num; int num;
} t_stack; };
typedef struct s_ttmn struct s_ttmn
{ {
char id; char id;
int placed; int placed;
int pos[4][2]; int pos[4][2];
} t_ttmn; };
typedef struct s_stack t_stack;
typedef struct s_ttmn t_ttmn;
t_list *ft_parse(char *filename); t_list *ft_parse(char *filename);
int ft_parse_line(char *line, int linenumber, t_list **list); int ft_parse_line(char *line, int linenumber, t_list **list);
@ -31,7 +46,8 @@ int ft_solver(char **board, t_list *lttmn, int space, int size);
int ft_solved(char **board); int ft_solved(char **board);
int ft_check_waste(char **board, t_list *lttmn, int space, int size); int ft_check_waste(char **board, t_list *lttmn, int space, int size);
int ft_fit_blob(char **board, t_list *lttmn, int space, int size, int blob_size, int i); int ft_fit_blob(
char **board, t_list *lttmn, int space, int size, int blob_size, int i);
int ft_floodfill_recursive(char **board, int size, int i, char c); int ft_floodfill_recursive(char **board, int size, int i, char c);
int ft_floodfill_stack(char **board, int size, int i, char c); int ft_floodfill_stack(char **board, int size, int i, char c);

@ -1 +0,0 @@
Subproject commit 70af533db4c5a53646e2ea2e371d611cc9959f48

View file

@ -1,6 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fillit_fit_blob.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 17:39:49 by jhalford #+# #+# */
/* Updated: 2016/11/03 17:42:03 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h" #include "fillit.h"
int ft_fit_blob(char **board, t_list *lttmn, int space, int size, int blob_size, int y) int ft_fit_blob(
char **board, t_list *lttmn, int space, int size, int blob_size, int y)
{ {
t_ttmn *ttmn; t_ttmn *ttmn;
t_list *list; t_list *list;
@ -22,7 +35,6 @@ int ft_fit_blob(char **board, t_list *lttmn, int space, int size, int blob_size
if (ttmn->placed || ft_board_add(board, *ttmn, i, size)) if (ttmn->placed || ft_board_add(board, *ttmn, i, size))
continue ; continue ;
ttmn->placed = 1; ttmn->placed = 1;
/* ft_board_print(board); */
if (ft_solver(board, lttmn, space, size)) if (ft_solver(board, lttmn, space, size))
return (1); return (1);
ft_board_replace(board, ttmn->id, '*'); ft_board_replace(board, ttmn->id, '*');

View file

@ -1,4 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fillit_parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 17:29:51 by jhalford #+# #+# */
/* Updated: 2016/11/03 17:31:00 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h" #include "fillit.h"
int g_target = 0; int g_target = 0;
int g_ttmn = 0; int g_ttmn = 0;
@ -22,7 +35,6 @@ int ft_parse_line(char *line, int linenumber, t_list **list)
i = -1; i = -1;
while (line[++i]) while (line[++i])
{ {
/* ft_printf("%i,%i,%i: %c\n", i, j, k, line[i]); */
if (!ft_strchr(".#", line[i])) if (!ft_strchr(".#", line[i]))
return (1); return (1);
else if (line[i] == '#' && ft_parse_sharp(&j, &k, &ttmn)) else if (line[i] == '#' && ft_parse_sharp(&j, &k, &ttmn))
@ -40,7 +52,6 @@ int ft_parse_sharp(int *j, int *k, t_ttmn *ttmn)
{ {
ref[0] = *j / 4; ref[0] = *j / 4;
ref[1] = *j % 4; ref[1] = *j % 4;
/* ft_printf("refs: %i,%i\n", ref[0], ref[1]); */
ttmn->pos[0][0] = 0; ttmn->pos[0][0] = 0;
ttmn->pos[0][1] = 0; ttmn->pos[0][1] = 0;
} }
@ -48,7 +59,6 @@ int ft_parse_sharp(int *j, int *k, t_ttmn *ttmn)
{ {
ttmn->pos[*k][0] = (*j) / 4 - ref[0]; ttmn->pos[*k][0] = (*j) / 4 - ref[0];
ttmn->pos[*k][1] = (*j) % 4 - ref[1]; ttmn->pos[*k][1] = (*j) % 4 - ref[1];
/* ft_printf("pos: %i,%i\n", ttmn->pos[*k][0], ttmn->pos[*k][1]); */
} }
else else
return (1); return (1);
@ -62,12 +72,11 @@ int ft_parse_addttmn(int *j, int *k, t_ttmn ttmn, t_list **list)
t_list *tmp; t_list *tmp;
ttmn.id = id++; ttmn.id = id++;
/* ft_printf("%i,%i\n", *j, *k); */
if (*j != 16 || *k != 4) if (*j != 16 || *k != 4)
return (1); return (1);
if (ft_ttmn_validate(ttmn)) if (ft_ttmn_validate(ttmn))
return (1); return (1);
tmp = ft_lstnew(&ttmn , sizeof(t_ttmn)); tmp = ft_lstnew(&ttmn, sizeof(t_ttmn));
ft_lsteadd(list, tmp); ft_lsteadd(list, tmp);
*j = 0; *j = 0;
*k = 0; *k = 0;
@ -88,7 +97,6 @@ t_list *ft_parse(char *filename)
linenumber = 0; linenumber = 0;
while ((ret = get_next_line(fd, &line))) while ((ret = get_next_line(fd, &line)))
{ {
/* ft_printf("parsing line:%s\n", line); */
if (ft_parse_line(line, linenumber++, &list)) if (ft_parse_line(line, linenumber++, &list))
return (0); return (0);
} }

View file

@ -1,13 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fillit_solver.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 17:36:27 by jhalford #+# #+# */
/* Updated: 2016/11/03 17:38:05 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h" #include "fillit.h"
int ft_solved(char **board) int ft_solved(char **board)
{ {
ft_putendl("found solution:");
ft_board_remove(board, '^'); ft_board_remove(board, '^');
ft_board_remove(board, '*'); ft_board_remove(board, '*');
g_sol = ft_board_copy(board); g_sol = ft_board_copy(board);
ft_board_print(board);
return (1); return (1);
} }
@ -21,17 +30,13 @@ int ft_solver(char **board, t_list *lttmn, int space, int size)
ttmn = (t_ttmn *)lttmn->content; ttmn = (t_ttmn *)lttmn->content;
if (ttmn->placed) if (ttmn->placed)
return (ft_solver(board, lttmn->next, space, size)); return (ft_solver(board, lttmn->next, space, size));
/* ft_board_print(board); */
i = -1; i = -1;
while (++i < size * size) while (++i < size * size)
{ {
if (ft_board_add(board, *ttmn, i, size)) if (ft_board_add(board, *ttmn, i, size))
continue ; continue ;
/* if (ft_solver(board, lttmn->next, space, size)) */
/* return (1); */
if (ft_check_waste(board, lttmn->next, space, size)) if (ft_check_waste(board, lttmn->next, space, size))
return (1); return (1);
ft_board_remove(board, ttmn->id); ft_board_remove(board, ttmn->id);
} }
return (0); return (0);

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* floodfill.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 17:26:42 by jhalford #+# #+# */
/* Updated: 2016/11/03 17:29:36 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h" #include "fillit.h"
int ft_floodfill_recursive(char **board, int size, int i, char c) int ft_floodfill_recursive(char **board, int size, int i, char c)
@ -9,58 +21,14 @@ int ft_floodfill_recursive(char **board, int size, int i, char c)
{ {
board[i / size][i % size] = c; board[i / size][i % size] = c;
n++; n++;
n += i % size < size - 1 ? ft_floodfill_recursive(board, size, i + 1, c) : 0; n += i % size < size - 1 ?
n += i / size > 0 ? ft_floodfill_recursive(board, size, i - size, c) : 0; ft_floodfill_recursive(board, size, i + 1, c) : 0;
n += i % size > 0 ? ft_floodfill_recursive(board, size, i - 1, c) : 0; n += i / size > 0 ?
n += i / size < size - 1 ? ft_floodfill_recursive(board, size, i + size, c) : 0; ft_floodfill_recursive(board, size, i - size, c) : 0;
} n += i % size > 0 ?
/* printf("list at %i: ", i); */ ft_floodfill_recursive(board, size, i - 1, c) : 0;
/* fflush(stdout); */ n += i / size < size - 1 ?
/* ft_lst_print(blob, &ft_putnbr); */ ft_floodfill_recursive(board, size, i + size, c) : 0;
return (n);
}
int ft_floodfill_stack(char **board, int size, int i, char c)
{
static const int dx[4] = {1, -1, 0, 0};
static const int dy[4] = {0, 0, 1, -1};
int x;
int y;
int nx;
int ny;
int n;
t_list *top;
t_list *stack;
t_list *stack2;
stack = NULL;
stack2 = NULL;
n = 0;
ft_lstadd(&stack, ft_lstnew(&i, sizeof(int)));
while (stack || stack2)
{
if (stack2)
stack = stack2;
stack2 = NULL;
/* ft_board_print(board); */
while((top = ft_lstpop(&stack)))
{
i = *(int *)top->content;
x = i % size;
y = i / size;
board[y][x] = c;
for(int j = 0; j < 4; j++) {
nx = x + dx[j];
ny = y + dy[j];
i = nx + size * ny;
if(nx >= 0 && nx < size && ny >= 0 && ny < size
&& board[ny][nx] == '.')
{
ft_lstadd(&stack2, ft_lstnew(&i, sizeof(int)));
n++;
}
}
}
} }
return (n); return (n);
} }

View file

@ -1,8 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lib_board.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 17:34:39 by jhalford #+# #+# */
/* Updated: 2016/11/03 17:35:39 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h" #include "fillit.h"
void ft_board_print(char **board) void ft_board_print(char **board)
{ {
/* ft_putendl("-s-"); */
while (*board) while (*board)
ft_putendl(*board++); ft_putendl(*board++);
ft_putendl(""); ft_putendl("");
@ -55,4 +66,3 @@ void ft_board_fill(char **dst, char **src)
i++; i++;
} }
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lib_board2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 17:31:46 by jhalford #+# #+# */
/* Updated: 2016/11/03 17:31:51 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h" #include "fillit.h"
int ft_board_add(char **board, t_ttmn ttmn, int i, int size) int ft_board_add(char **board, t_ttmn ttmn, int i, int size)
@ -11,8 +23,6 @@ int ft_board_add(char **board, t_ttmn ttmn, int i, int size)
{ {
x = i % size + ttmn.pos[k][1]; x = i % size + ttmn.pos[k][1];
y = i / size + ttmn.pos[k][0]; y = i / size + ttmn.pos[k][0];
/* printf("adding %c to %i,%i\n", block.id, y, x); */
/* fflush(stdout); */
if (x > size - 1 || y > size - 1 || x < 0 || y < 0) if (x > size - 1 || y > size - 1 || x < 0 || y < 0)
return (1); return (1);
if (board[y][x] != '.' && board[y][x] != '*') if (board[y][x] != '.' && board[y][x] != '*')
@ -25,7 +35,6 @@ int ft_board_add(char **board, t_ttmn ttmn, int i, int size)
x = i % size + ttmn.pos[k][1]; x = i % size + ttmn.pos[k][1];
y = i / size + ttmn.pos[k][0]; y = i / size + ttmn.pos[k][0];
board[y][x] = ttmn.id; board[y][x] = ttmn.id;
/* ft_show_board(board); */
k++; k++;
} }
return (0); return (0);

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lib_ttmn.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 17:32:01 by jhalford #+# #+# */
/* Updated: 2016/11/03 17:34:16 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h" #include "fillit.h"
int ft_ttmn_validate(t_ttmn ttmn) int ft_ttmn_validate(t_ttmn ttmn)
@ -7,7 +19,7 @@ int ft_ttmn_validate(t_ttmn ttmn)
int touch; int touch;
if (!(ttmn.id >= 'A' && ttmn.id <= 'Z')) if (!(ttmn.id >= 'A' && ttmn.id <= 'Z'))
return(1); return (1);
i = -1; i = -1;
touch = 0; touch = 0;
while (++i < 4) while (++i < 4)
@ -28,22 +40,6 @@ int ft_ttmn_validate(t_ttmn ttmn)
return (1); return (1);
} }
void ft_ttmn_print(t_ttmn ttmn)
{
int i;
if (!ttmn.id)
{
ft_putendl("end of ttmn\n");
return ;
}
printf("%c,%i", ttmn.id, ttmn.placed);
for (i=1; i < 4; i++)
printf("\t%d,%d\n", ttmn.pos[i][0], ttmn.pos[i][1]);
fflush(stdout);
ft_putendl("");
}
void ft_ttmn_reset(t_list *ttmn) void ft_ttmn_reset(t_list *ttmn)
{ {
((t_ttmn *)ttmn->content)->placed = 0; ((t_ttmn *)ttmn->content)->placed = 0;

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 17:31:13 by jhalford #+# #+# */
/* Updated: 2016/11/03 17:37:59 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h" #include "fillit.h"
char **g_sol = NULL; char **g_sol = NULL;
@ -26,5 +38,6 @@ int main(int ac, char **av)
ft_solver(board, lttmn, size * size - 4 * g_ttmn, size); ft_solver(board, lttmn, size * size - 4 * g_ttmn, size);
size--; size--;
} }
ft_board_print(g_sol);
return (0); return (0);
} }