Working solutions, I guess
This commit is contained in:
parent
a2e313a22a
commit
dc0f3eb618
3
Makefile
3
Makefile
|
@ -5,6 +5,7 @@ SRC = main.c \
|
|||
solution.c \
|
||||
printing.c \
|
||||
mem.c \
|
||||
map_helpers.c \
|
||||
|
||||
HEADERS = parsing.h \
|
||||
printing.h \
|
||||
|
@ -72,12 +73,14 @@ SUCCESS = && $(SUCCESS_MSG) || $(FAIL_MSG)
|
|||
SUCCESS_VALG = && $(SUCCESS_MSG_VALG) || $(FAIL_MSG_VALG)
|
||||
FAIL = && $(FAIL_MSG) || $(SUCCESS_MSG)
|
||||
VALGRIND = valgrind --leak-check=full --error-exitcode=1
|
||||
VALGRIND_SMALL = 2>/dev/null 1>&2 valgrind --leak-check=full --error-exitcode=1
|
||||
YLW = : [33m;
|
||||
CYN = : [36m;
|
||||
CLR_RST = && : [m
|
||||
|
||||
run:
|
||||
@#clear
|
||||
$(CYN) $(VALGRIND_SMALL) ./$(NAME) $(CLR_RST) $(SUCCESS_VALG)
|
||||
$(YLW) ./$(NAME) $(CLR_RST) $(SUCCESS)
|
||||
|
||||
valgrind: re
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: apago <apago@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/04 18:22:11 by apago #+# #+# */
|
||||
/* Updated: 2023/04/04 18:28:44 by apago ### ########.fr */
|
||||
/* Updated: 2023/04/05 09:31:04 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -31,6 +31,7 @@ typedef struct Map
|
|||
{
|
||||
t_meta meta;
|
||||
char *data;
|
||||
char *copy;
|
||||
} t_map;
|
||||
|
||||
char *read_file(int file);
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/05 04:11:08 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/05 04:16:35 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/05 06:28:23 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -19,6 +19,6 @@
|
|||
void ft_putchar(char c);
|
||||
void ft_putnbr(int nb);
|
||||
size_t ft_strlen(char const *str);
|
||||
void print_map(t_map *map);
|
||||
void print_map(t_map *map, int as_numbers);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,14 +6,18 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/05 04:02:35 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/05 04:03:37 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/05 09:05:08 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef SOLUTION_H
|
||||
# define SOLUTION_H
|
||||
# include "parsing.h"
|
||||
# include "printing.h"
|
||||
|
||||
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);
|
||||
|
||||
#endif
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
48
srcs/main.c
48
srcs/main.c
|
@ -6,7 +6,7 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/04 21:50:50 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/05 04:27:43 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/05 09:33:15 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -14,31 +14,49 @@
|
|||
#include "solution.h"
|
||||
#include "printing.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
char *ft_strcpy(char *dest, char *src)
|
||||
{
|
||||
char *o_dest;
|
||||
|
||||
o_dest = dest;
|
||||
while (*src)
|
||||
*dest++ = *src++;
|
||||
*dest = 0;
|
||||
return (o_dest);
|
||||
}
|
||||
|
||||
// int argc, char **argv
|
||||
// (void)argc;
|
||||
// (void)argv;
|
||||
int main(void)
|
||||
{
|
||||
t_map map;
|
||||
int ret;
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
ret = read_fname("./assets/map2", &map);
|
||||
map.data = \
|
||||
free(map.data);
|
||||
map.data = malloc(sizeof(char) * 244);
|
||||
map.copy = malloc(sizeof(char) * 244);
|
||||
ft_strcpy(map.data, \
|
||||
"..........................." "....o......................"
|
||||
"............o.............." "..........................."
|
||||
"....o......................" "...............o..........."
|
||||
"..........................." "......o..............o....."
|
||||
"..o.......o................";
|
||||
printf("Reading file ret val: %d\n", ret);
|
||||
printf("Height: %d\n", map.meta.height);
|
||||
printf("Width: %d\n", map.meta.width);
|
||||
printf("Map len: %zu\n", ft_strlen(map.data));
|
||||
printf("Map 1d:\n\"%s\"\n", map.data);
|
||||
fflush(stdout);
|
||||
print_map(&map);
|
||||
"..o.......o................");
|
||||
ft_strcpy(map.copy, map.data);
|
||||
print_map(&map, 0);
|
||||
printf("######## STARTED SOLVE ########\n");
|
||||
solve(&map);
|
||||
printf("######## FINISHED SOLVE ########\n");
|
||||
fflush(stdout);
|
||||
print_map(&map);
|
||||
return (0);
|
||||
print_map(&map, 0);
|
||||
free(map.data);
|
||||
free(map.copy);
|
||||
}
|
||||
// printf("Reading file ret val: %d\n", ret);
|
||||
// printf("Height: %d\n", map.meta.height);
|
||||
// printf("Width: %d\n", map.meta.width);
|
||||
// printf("Map len: %zu\n", ft_strlen(map.data));
|
||||
// printf("Map 1d:\n\"%s\"\n", map.data);
|
||||
// fflush(stdout);
|
||||
// return (0);
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* map_helpers.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/05 06:50:28 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/05 09:05:18 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solution.h"
|
||||
|
||||
// index to column
|
||||
int itoc(int idx, int width)
|
||||
{
|
||||
return (idx % width);
|
||||
}
|
||||
|
||||
// index to row
|
||||
int itor(int idx, int width)
|
||||
{
|
||||
return (idx / width);
|
||||
}
|
||||
|
||||
char get_cell(t_map *map, int idx, int bottom, int right)
|
||||
{
|
||||
int row;
|
||||
int col;
|
||||
|
||||
if ((bottom | right) == 0)
|
||||
return (map->data[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)
|
||||
return (0);
|
||||
idx = col + row * map->meta.width;
|
||||
return (map->data[idx]);
|
||||
}
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/05 04:09:39 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/05 04:23:45 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/05 06:54:58 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -54,31 +54,33 @@ size_t ft_strlen(char const *str)
|
|||
return (size);
|
||||
}
|
||||
|
||||
void print_map(t_map *map)
|
||||
void print_map(t_map *map, int as_numbers)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
ft_putstr("Map Meta:\n\tEmpty char: ");
|
||||
ft_putchar(map->meta.empty);
|
||||
ft_putchar('\n');
|
||||
ft_putstr("\tObstacle char: ");
|
||||
ft_putchar(map->meta.obstacle);
|
||||
ft_putchar('\n');
|
||||
ft_putstr("\tFull char: ");
|
||||
ft_putchar(map->meta.full);
|
||||
ft_putstr("\nMap:\n");
|
||||
i = 0;
|
||||
while (i < map->meta.height)
|
||||
ft_putstr("\nMap 2d:\n");
|
||||
i = -1;
|
||||
while (++i < map->meta.height)
|
||||
{
|
||||
j = 0;
|
||||
while (j < map->meta.width)
|
||||
j = -1;
|
||||
while (++j < map->meta.width)
|
||||
{
|
||||
if (as_numbers)
|
||||
{
|
||||
ft_putnbr(map->data[i * map->meta.width + j]);
|
||||
}
|
||||
else
|
||||
ft_putchar(map->data[i * map->meta.width + j]);
|
||||
j++;
|
||||
}
|
||||
ft_putchar('\n');
|
||||
i++;
|
||||
}
|
||||
ft_putchar('\n');
|
||||
}
|
||||
}
|
||||
// ft_putstr("Map Meta:\n\tEmpty char: ");
|
||||
// ft_putchar(map->meta.empty);
|
||||
// ft_putchar('\n');
|
||||
// ft_putstr("\tObstacle char: ");
|
||||
// ft_putchar(map->meta.obstacle);
|
||||
// ft_putchar('\n');
|
||||
// ft_putstr("\tFull char: ");
|
||||
// ft_putchar(map->meta.full);
|
||||
|
|
|
@ -6,13 +6,94 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/04 21:46:57 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/05 04:04:28 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/05 09:39:53 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solution.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void preprocess_map(t_map *map)
|
||||
{
|
||||
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);
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
|
||||
// if (idx - 1 - map->meta.width < 0
|
||||
int count_obstacles(t_map *map, int idx, int sq_size)
|
||||
{
|
||||
int alternat_corner_sum;
|
||||
|
||||
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 (-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);
|
||||
return (alternat_corner_sum);
|
||||
}
|
||||
|
||||
void fill_with_full(t_map *map, int sq_idx, int sq_size)
|
||||
{
|
||||
int idx;
|
||||
int idx_row;
|
||||
int idx_col;
|
||||
int sq_row;
|
||||
int sq_col;
|
||||
|
||||
sq_row = itor(sq_idx, map->meta.width);
|
||||
sq_col = itoc(sq_idx, map->meta.width);
|
||||
idx = 0;
|
||||
while (idx < map->meta.width * map->meta.height)
|
||||
{
|
||||
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)
|
||||
map->data[idx] = map->meta.full;
|
||||
else
|
||||
map->data[idx] = map->copy[idx];
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
|
||||
void solve(t_map *map)
|
||||
{
|
||||
(void)map;
|
||||
int idx;
|
||||
int sq_idx;
|
||||
int sq_size;
|
||||
|
||||
preprocess_map(map);
|
||||
idx = 0;
|
||||
sq_size = 0;
|
||||
sq_idx = 0;
|
||||
while (idx < map->meta.width * map->meta.height)
|
||||
{
|
||||
while (count_obstacles(map, idx, sq_size) == 0)
|
||||
{
|
||||
sq_idx = idx;
|
||||
sq_size++;
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
sq_size--;
|
||||
printf("sq_size: <%d>\n", sq_size);
|
||||
printf("sq_idx: <%d>\n", sq_idx);
|
||||
fill_with_full(map, sq_idx, sq_size);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue