diff --git a/Makefile b/Makefile index 290f150..2c4b87b 100644 --- a/Makefile +++ b/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 = : ; CYN = : ; CLR_RST = && :  run: @#clear + $(CYN) $(VALGRIND_SMALL) ./$(NAME) $(CLR_RST) $(SUCCESS_VALG) $(YLW) ./$(NAME) $(CLR_RST) $(SUCCESS) valgrind: re diff --git a/includes/parsing.h b/includes/parsing.h index 9a24dc0..98fb731 100644 --- a/includes/parsing.h +++ b/includes/parsing.h @@ -6,7 +6,7 @@ /* By: apago +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); @@ -41,4 +42,4 @@ int printable(char c); size_t count_first_line(char *line); int read_fname(char *name, t_map *map); -#endif \ No newline at end of file +#endif diff --git a/includes/printing.h b/includes/printing.h index 00aa934..8818c8a 100644 --- a/includes/printing.h +++ b/includes/printing.h @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/includes/solution.h b/includes/solution.h index 2c15d5b..9f7d904 100644 --- a/includes/solution.h +++ b/includes/solution.h @@ -6,14 +6,18 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/obj/io.o b/obj/io.o new file mode 100644 index 0000000..c6099b4 Binary files /dev/null and b/obj/io.o differ diff --git a/obj/main.o b/obj/main.o new file mode 100644 index 0000000..3724c81 Binary files /dev/null and b/obj/main.o differ diff --git a/obj/parsing.o b/obj/parsing.o new file mode 100644 index 0000000..d7a60e9 Binary files /dev/null and b/obj/parsing.o differ diff --git a/obj/parsing_simple.o b/obj/parsing_simple.o new file mode 100644 index 0000000..50ebe01 Binary files /dev/null and b/obj/parsing_simple.o differ diff --git a/obj/solution.o b/obj/solution.o new file mode 100644 index 0000000..799c09c Binary files /dev/null and b/obj/solution.o differ diff --git a/srcs/main.c b/srcs/main.c index 2dc2c82..27b522a 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/srcs/map_helpers.c b/srcs/map_helpers.c new file mode 100644 index 0000000..5511d5e --- /dev/null +++ b/srcs/map_helpers.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map_helpers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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]); +} diff --git a/srcs/printing.c b/srcs/printing.c index c0ba03d..456a885 100644 --- a/srcs/printing.c +++ b/srcs/printing.c @@ -6,7 +6,7 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) { - ft_putchar(map->data[i * map->meta.width + j]); - j++; + if (as_numbers) + { + ft_putnbr(map->data[i * map->meta.width + j]); + } + else + ft_putchar(map->data[i * map->meta.width + 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); diff --git a/srcs/solution.c b/srcs/solution.c index 73144c0..3257384 100644 --- a/srcs/solution.c +++ b/srcs/solution.c @@ -6,13 +6,94 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 + +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); }