-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
103 lines (92 loc) · 2.41 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vcombey <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/06 23:40:42 by vcombey #+# #+# */
/* Updated: 2017/03/08 16:52:54 by vcombey ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
#include "libft/libft.h"
#include <stdio.h>
int is_a_number(char *line)
{
int i;
i = 0;
while (line[i])
{
if ((line[i] > 57) || (line[i] < 48))
return (0);
i++;
}
return (1);
}
void get_nb(char *line, t_anthill *a, t_file f)
{
int result;
result = 0;
if (!line || !(ft_atoi_safe(line, &result)))
ft_exit_err("bad number of ants", &f);
if ((a->nb_ant = result) == 0)
ft_exit_err("there is no ants you newbie", &f);
}
void realoc_file(t_file *f, int i)
{
char **new;
f->data_len = f->data_len + LINES;
if (!(new = ft_memalloc(sizeof(char *) * (f->data_len + LINES))))
ft_exit_err("malloc error", NULL);
ft_memcpy(new, f->data, i * sizeof(char *));
free(f->data);
f->data = new;
}
void read_file(t_file *f)
{
char *line;
int i;
int ret;
i = 0;
f->line = 1;
f->data_len = LINES + 1;
if (!(f->data = (char**)ft_memalloc(sizeof(char*) * (LINES + 1))))
ft_exit_err("malloc error", f);
line = NULL;
while ((ret = get_next_line(0, &line)) > 0)
{
if (i >= f->data_len)
realoc_file(f, i);
f->data[i] = line;
i++;
}
if (ret == -1)
ft_exit_err("bad file", f);
}
int main(void)
{
t_file f;
t_anthill a;
t_ways *ways;
read_file(&f);
a.room = NULL;
get_nb(f.data[0], &a, f);
f.line++;
get_rooms(&a, &f);
init_mat(&a);
while (f.data[f.line - 1] != NULL)
{
if (!(get_links(f.data[f.line - 1], &a)))
{
f.line--;
break ;
}
f.line++;
}
ways = find_best_ways(a);
ft_putnstrstr(f.data, f.line);
write(1, "\n", 1);
display_multi_solus(ways, a);
free_all_the_shit(ways, a, f);
}