Replace copy with index

This commit is contained in:
Andrei Pago 2023-04-05 17:42:21 +02:00
parent ccf063f3aa
commit 39c4fb3158
8 changed files with 51 additions and 43 deletions

View File

@ -6,7 +6,7 @@
/* By: apago <apago@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/04 18:22:11 by apago #+# #+# */
/* Updated: 2023/04/05 17:17:26 by apago ### ########.fr */
/* Updated: 2023/04/05 17:39:31 by apago ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,7 +31,7 @@ typedef struct Map
{
t_meta meta;
char *data;
char *copy;
int *index;
} t_map;
char *read_file(int file);
@ -42,5 +42,6 @@ int printable(char c);
size_t count_first_line(char *line);
int read_fname(char *name, t_map *map);
void copy_bytes(char *dst, char *src, size_t bytes);
void ft_memset_int(int *dst, int set, size_t bytes);
#endif

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* solution.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* By: apago <apago@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/05 04:02:35 by tischmid #+# #+# */
/* Updated: 2023/04/05 09:05:08 by tischmid ### ########.fr */
/* Updated: 2023/04/05 17:40:16 by apago ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,6 +18,6 @@
void solve(t_map *map);
int itoc(int idx, int width);
int itor(int idx, int width);
char get_cell(t_map *map, int idx, int top, int left);
int get_index(t_map *map, int idx, int top, int left);
#endif

View File

@ -6,7 +6,7 @@
/* By: apago <apago@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/04 18:23:57 by apago #+# #+# */
/* Updated: 2023/04/04 18:24:50 by apago ### ########.fr */
/* Updated: 2023/04/05 17:32:11 by apago ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,6 +24,18 @@ void copy_bytes(char *dst, char *src, size_t bytes)
}
}
void ft_memset_int(int *dst, int set, size_t bytes)
{
size_t i;
i = 0;
while (i < bytes)
{
dst[i] = set;
i++;
}
}
int push_bytes(size_t n_read, char **res, size_t *offset, size_t *size)
{
char *new_res;

View File

@ -6,7 +6,7 @@
/* By: apago <apago@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/04 21:50:50 by tischmid #+# #+# */
/* Updated: 2023/04/05 17:14:36 by apago ### ########.fr */
/* Updated: 2023/04/05 17:41:33 by apago ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,6 +24,6 @@ int main(void)
solve(&map);
print_map(&map, 0);
free(map.data);
free(map.copy);
free(map.index);
return (0);
}

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* map_helpers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* By: apago <apago@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/05 06:50:28 by tischmid #+# #+# */
/* Updated: 2023/04/05 09:05:18 by tischmid ### ########.fr */
/* Updated: 2023/04/05 17:35:58 by apago ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,22 +24,19 @@ int itor(int idx, int width)
return (idx / width);
}
char get_cell(t_map *map, int idx, int bottom, int right)
int get_index(t_map *map, int idx, int bottom, int right)
{
int row;
int col;
if ((bottom | right) == 0)
return (map->data[idx]);
return (map->index[idx]);
row = itor(idx, map->meta.width);
col = itoc(idx, map->meta.width);
row += bottom;
col += right;
if (row >= map->meta.height
|| col >= map->meta.width
|| col < 0
|| row < 0)
if (row >= map->meta.height || col >= map->meta.width || col < 0 || row < 0)
return (0);
idx = col + row * map->meta.width;
return (map->data[idx]);
return (map->index[idx]);
}

View File

@ -6,7 +6,7 @@
/* By: apago <apago@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/04 16:19:27 by apago #+# #+# */
/* Updated: 2023/04/05 17:16:41 by apago ### ########.fr */
/* Updated: 2023/04/05 17:39:56 by apago ### ########.fr */
/* */
/* ************************************************************************** */
@ -103,13 +103,13 @@ int parse_input(char *str, t_map *map)
map->data = parse_data(&str[offset], &map->meta);
if (!map->data)
return (0);
map->copy = malloc(sizeof(char) * (map->meta.width * map->meta.height));
if (!map->copy)
map->index = malloc(sizeof(int) * (map->meta.width * map->meta.height));
if (!map->index)
{
free(map->data);
return (0);
}
copy_bytes(map->copy, map->data, map->meta.width * map->meta.height);
ft_memset_int(map->index, 0, map->meta.width * map->meta.height);
return (1);
}

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* printing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* By: apago <apago@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/05 04:09:39 by tischmid #+# #+# */
/* Updated: 2023/04/05 11:01:16 by tischmid ### ########.fr */
/* Updated: 2023/04/05 17:28:54 by apago ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* solution.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* By: apago <apago@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/04 21:46:57 by tischmid #+# #+# */
/* Updated: 2023/04/05 11:18:21 by tischmid ### ########.fr */
/* Updated: 2023/04/05 17:38:37 by apago ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,17 +19,17 @@
// namely by summing up the corner cells alternatingly (+, -, +, -, clockwise).
void preprocess_map(t_map *map)
{
int is_obstacle;
int idx;
int is_obstacle;
int idx;
idx = 0;
while (idx < map->meta.width * map->meta.height)
{
is_obstacle = get_cell(map, idx, 0, 0) == map->meta.obstacle;
map->data[idx] = is_obstacle + \
get_cell(map, idx, 0, -1) + \
get_cell(map, idx, -1, 0) - \
get_cell(map, idx, -1, -1);
is_obstacle = map->data[idx] == map->meta.obstacle;
map->index[idx] = is_obstacle +
get_index(map, idx, 0, -1) +
get_index(map, idx, -1, 0) -
get_index(map, idx, -1, -1);
idx++;
}
}
@ -41,14 +41,14 @@ int count_obstacles(t_map *map, int idx, int sq_size)
if (sq_size == 0)
return (0);
if (sq_size == 1)
return (get_cell(map, idx, 0, 0));
if (idx + (sq_size - 1) * (map->meta.width + 1)
> map->meta.width * map->meta.height)
return (get_index(map, idx, 0, 0));
if (idx + (sq_size - 1) * (map->meta.width + 1) > map->meta.width
* map->meta.height)
return (-1);
alternat_corner_sum = get_cell(map, idx, -1, -1) - \
get_cell(map, idx, -1, sq_size - 1) + \
get_cell(map, idx, sq_size - 1, sq_size - 1) - \
get_cell(map, idx, sq_size - 1, -1);
alternat_corner_sum = get_index(map, idx, -1, -1) -
get_index(map, idx, -1, sq_size - 1) +
get_index(map, idx, sq_size - 1, sq_size - 1) -
get_index(map, idx, sq_size - 1, -1);
return (alternat_corner_sum);
}
@ -67,11 +67,9 @@ void fill_with_full(t_map *map, int sq_idx, int sq_size)
{
idx_row = itor(idx, map->meta.width);
idx_col = itoc(idx, map->meta.width);
if (idx_row >= sq_row && idx_row < sq_row + sq_size
&& idx_col >= sq_col && idx_col < sq_col + sq_size)
if (idx_row >= sq_row && idx_row < sq_row + sq_size && idx_col >= sq_col
&& idx_col < sq_col + sq_size)
map->data[idx] = map->meta.full;
else
map->data[idx] = map->copy[idx];
idx++;
}
}