From dc0f3eb618c94133d19e6ca122fd657e81c60b1b Mon Sep 17 00:00:00 2001 From: Timo Schmidt Date: Wed, 5 Apr 2023 09:40:13 +0200 Subject: [PATCH] Working solutions, I guess --- Makefile | 3 ++ includes/parsing.h | 5 ++- includes/printing.h | 4 +- includes/solution.h | 6 ++- obj/io.o | Bin 0 -> 1960 bytes obj/main.o | Bin 0 -> 2552 bytes obj/parsing.o | Bin 0 -> 3480 bytes obj/parsing_simple.o | Bin 0 -> 1776 bytes obj/solution.o | Bin 0 -> 3040 bytes srcs/main.c | 48 ++++++++++++++++-------- srcs/map_helpers.c | 45 +++++++++++++++++++++++ srcs/printing.c | 40 ++++++++++---------- srcs/solution.c | 85 ++++++++++++++++++++++++++++++++++++++++++- 13 files changed, 195 insertions(+), 41 deletions(-) create mode 100644 obj/io.o create mode 100644 obj/main.o create mode 100644 obj/parsing.o create mode 100644 obj/parsing_simple.o create mode 100644 obj/solution.o create mode 100644 srcs/map_helpers.c 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 0000000000000000000000000000000000000000..c6099b46306dfc8991f3ff8329c5612b7aa4f846 GIT binary patch liter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KDU^;ISb>_~<#*wBo+lP~{#3h1J)EJm57x z?EW3vk5nnF0$D-67fSVch`SU`Jdoi;5Qrux3Gq@H9^ci&LVda>;QWjq1B;+*0lNaN zRQ*$B_#0*T*)qIRhHoyz-vl0k>+n#|dfZ|@?ob`H-|&q;^$(B) zIQhpGZI641#ZQ<;eHAo5F8?q%`B#FzM$o@yJYSa^9l!_6(D~GY&+C5@_|3_z!_W z>ngo9we=MZBxZ&UGZ~qgCsI2dGOak1kdC49CUJD)V&igpU~mdYF7S?Q~sV2*M(ek`T99M0Zys?li7jr zb>aTuuM^Mb`_~Vgr@x?sS%hX|j^*RKh`@Y=bN&y3bIu)ges-w_SppiIr*r(jY;cZS literal 0 HcmV?d00001 diff --git a/obj/parsing.o b/obj/parsing.o new file mode 100644 index 0000000000000000000000000000000000000000..d7a60e9b3e837905d2d6896fc3eff63df36bf892 GIT binary patch literal 3480 zcmbtWUuauZ7(chk+O%tXGf^2RYhH#7Txi`($Hq*NJFmLR5Q>71^zQby3#Lg)lhTR7 zVxn*fg+7+)LtoSfh4sOQ?WI^JtLvLkqz^#?Mr=^9?m>$b>i0YMoST!|#1}s}J>UI( z|IhcGlYU@ayV|8`Op?Z~u;!OU89RNl$tO6QV11-HVYbImZe(qdTCm-|U=%Q%E~<4p<*{fbd{H%ptEJ6<N7p6!y4% z>451UsTODd!@?{*)Y@D9LBTr3A*-(TR`UeeQ>cEto(En?cgl!YfvWR z&aHMq5r??(uS()%gmptr}hh6gEO-Hfjd6q|tVkv`NGep(7 z23>+3Uh__4P1L+JD#KSO8!D^eoprzyiltr;qu1^0<#$rvDSkpSHFbCvWJ0JNw>TsM zx#AYnN#Pb_I8?hUvV79_Z?5SXnXVaym04Ph2F!ZMj2;5KJKnJJ0+#6WSig|X$Y1MT zYv_$B73)W>IlKxlMeotU$@{$50X5wrQYDAGeyXlPtt{^H?obo_%R~9FiJOH?z7Vt1 zR%Rx);N<30*-UKYh4^rMFj~Sl2OlgAiQ9Af*Ushi5zBs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}Xg7_Q!yv(;;zM=lvmp zCM+oOc;vtM~HS;=13&qQWU zX3mWgIA$};XCmxK67y!kFeB+)s<0SY(#w@>u@H%#R;HBbspzyxwa-9UOc_PGe33}Y zW74ss!{KZ2fq4Wld{FM;>-+;_pM+kxe<-vK3|}{cILLAQAaVV`goFgNeFK#sAyFUt zw`2c-uO?f#a{JI7s%NUMaHj?57#dUD4bD z)8H9{Cnz=mz3`KL@FxMsxLjy&;ZTI9m!BoTkv}%zThLy?7V|{G%7)VseJgWv3f-|K@PXRX1wFlL>)o)++70iPFek#7jN$bXde z5&e${elh}%I^NGO8II|)E$j&SUf_?yne%@P@MX#-q}GT3n}8n?{6v@?mcdG)NGyOi z*m*91bE7K(oX^q7(BP5q@F-ErdM>3H`m#ZkbTOaT3kFFS;oFfc&R2|7I;U4i)+m-q zsjQdE#k5|jB=e~fS;*#c$xB(|?c{=)%aKLhNTzkzD21Y-D=)p0IEB{l5+#>g$|qBq z419qW!AZ7YERaeux1_VsI(R6j@;XLIqsBDgQ>CPdQZdR#Y97}2^K>m2Gbtm5a8!x1 zvi|q-1$bb8fRY{v>cRcU z$4cUiqn|lu|2ZB6;q`L}e?xkO^6=7w%+WU?*gu50J?~!%uz?Hv`1!p8Pe)#7MCOLt ZqmtLpiC@7cm^ +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); }