Alg seems to be working. Didn't do enough testing tho, since parsing is not working yet
This commit is contained in:
parent
dc0f3eb618
commit
37784434d0
3
Makefile
3
Makefile
|
@ -4,12 +4,13 @@ SRC = main.c \
|
|||
parsing_simple.c \
|
||||
solution.c \
|
||||
printing.c \
|
||||
mem.c \
|
||||
map_helpers.c \
|
||||
ft_string.c \
|
||||
|
||||
HEADERS = parsing.h \
|
||||
printing.h \
|
||||
solution.h \
|
||||
ft_string.h \
|
||||
|
||||
OBJDIR = obj
|
||||
SRCDIR = srcs
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
...............................o..................................o.............................................o.....................................o............................................o..............o.......o.......o................
|
165
assets/solve.py
165
assets/solve.py
|
@ -1,165 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
|
||||
def to_i(idx, x):
|
||||
return idx // x
|
||||
|
||||
def to_j(idx, x):
|
||||
return idx % x
|
||||
|
||||
def plus_j(idx, n, max):
|
||||
if idx + n >= max:
|
||||
return -1
|
||||
return idx + n
|
||||
|
||||
def plus_i(idx, x, n, max):
|
||||
if idx + n * x >= max:
|
||||
return -1
|
||||
return idx + n * x
|
||||
|
||||
def is_obst(board, idx, obst):
|
||||
if board[idx] == obst:
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
def to_char(n):
|
||||
alph = '0123456789abcdefghijklmnopqrstuvwxyz'
|
||||
if n < len(alph):
|
||||
return alph[n]
|
||||
return "<OOB>"
|
||||
|
||||
def count_obst(prep_board, size, idx, x):
|
||||
max_idx = len(prep_board)
|
||||
if size == 0:
|
||||
return 0
|
||||
if plus_j(idx, size - 1, max_idx) == -1 or plus_i(idx, x, size - 1, max_idx) == -1:
|
||||
return -1
|
||||
idx = plus_j(plus_i(idx, x, -1, max_idx), -1, max_idx)
|
||||
top_left = 0
|
||||
top_right = 0
|
||||
bottom_left = 0
|
||||
bottom_right = 0
|
||||
if to_i(idx, x) != 0 and to_j(idx, x) != 0:
|
||||
top_left = prep_board[idx]
|
||||
if size == 1:
|
||||
return top_left
|
||||
if to_i(idx, x) != 0:
|
||||
top_right = prep_board[plus_j(idx, size, max_idx)]
|
||||
if to_j(idx, x) != 0:
|
||||
bottom_left = prep_board[plus_i(idx, x, size, max_idx)]
|
||||
bottom_right = prep_board[plus_j(plus_i(idx, x, size, max_idx), size, max_idx)]
|
||||
total = top_left + bottom_right - top_right - bottom_left
|
||||
print(f"{size=}")
|
||||
print(f"{top_right=}")
|
||||
print(f"{top_left=}")
|
||||
print(f"{bottom_right=}")
|
||||
print(f"{bottom_left=}")
|
||||
print(f"{total=}")
|
||||
return total
|
||||
|
||||
def print_prep_board_idx(prep_board, i, x, full):
|
||||
for idx in range(len(prep_board)):
|
||||
if i == idx:
|
||||
print(end=full)
|
||||
else:
|
||||
print(to_char(prep_board[idx]), end='')
|
||||
if to_j(idx, x) == x - 1:
|
||||
print()
|
||||
|
||||
def print_board_idx(board, i, x, full):
|
||||
for idx in range(len(board)):
|
||||
if i == idx:
|
||||
print(end=full)
|
||||
else:
|
||||
print(board[idx], end='')
|
||||
if to_j(idx, x) == x - 1:
|
||||
print()
|
||||
|
||||
from time import sleep
|
||||
def find_largest_square(board, prep_board, x, full):
|
||||
idx = 0
|
||||
idx_of_square = 0
|
||||
size = 0
|
||||
while idx < len(prep_board):
|
||||
print()
|
||||
print(f"{idx=}")
|
||||
print_prep_board_idx(prep_board, idx, x, full)
|
||||
print_board_idx(board, idx, x, full)
|
||||
while count_obst(prep_board, size, idx, x) == 0:
|
||||
print("YES")
|
||||
idx_of_square = idx
|
||||
size += 1
|
||||
sleep(.1)
|
||||
print()
|
||||
print(f"{idx=}")
|
||||
print_prep_board_idx(prep_board, idx, x, full)
|
||||
print_board_idx(board, idx, x, full)
|
||||
idx += 1
|
||||
sleep(.1)
|
||||
size -= 1
|
||||
return idx_of_square, size
|
||||
|
||||
def is_in_square(idx, sol_idx, sq_size, x):
|
||||
i = to_i(idx, x)
|
||||
j = to_j(idx, x)
|
||||
sol_i = to_i(sol_idx, x)
|
||||
sol_j = to_j(sol_idx, x)
|
||||
if sol_i < i <= sol_i + sq_size - 1:
|
||||
if sol_j < j <= sol_j + sq_size - 1:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def print_sol(board, sol_idx, sq_size, x, full):
|
||||
for idx in range(len(board)):
|
||||
if is_in_square(idx, sol_idx, sq_size, x):
|
||||
print(board[idx], end='')
|
||||
else:
|
||||
print(end=full)
|
||||
if to_j(idx, x) == x - 1:
|
||||
print()
|
||||
|
||||
def main():
|
||||
with open('map2') as m:
|
||||
inp = m.read().splitlines()
|
||||
first_line = inp[0]
|
||||
board = ''
|
||||
empty = first_line[-3]
|
||||
obst = first_line[-2]
|
||||
full = first_line[-1]
|
||||
y = int(first_line[:-3])
|
||||
x = len(inp[1])
|
||||
for line in inp[1:]:
|
||||
print(line)
|
||||
board += line
|
||||
board = list(board)
|
||||
prep_board = board.copy()
|
||||
for idx in range(x * y):
|
||||
i = to_i(idx, x)
|
||||
j = to_j(idx, x)
|
||||
if i > 0 and j > 0:
|
||||
prep_board[idx] = is_obst(board, idx, obst) + \
|
||||
prep_board[plus_j(idx, -1, len(prep_board))] + \
|
||||
prep_board[plus_i(idx, x, -1, len(prep_board))] - \
|
||||
prep_board[plus_i(plus_j(idx, -1, len(prep_board)), x, -1, len(prep_board))]
|
||||
elif i > 0:
|
||||
prep_board[idx] = is_obst(board, idx, obst) + \
|
||||
prep_board[plus_i(idx, x, -1, len(prep_board))]
|
||||
elif j > 0:
|
||||
prep_board[idx] = is_obst(board, idx, obst) + \
|
||||
prep_board[plus_j(idx, -1, len(prep_board))]
|
||||
else:
|
||||
prep_board[idx] = is_obst(board, idx, obst)
|
||||
# for i in range(y):
|
||||
# for j in range(x):
|
||||
# print(to_char(prep_board[j + i * x]), end=' ')
|
||||
# print()
|
||||
print()
|
||||
idx, size = find_largest_square(board, prep_board, x, full)
|
||||
print_sol(board, idx, size, x, full)
|
||||
print(idx, size)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
|
@ -1,25 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* mem.c :+: :+: :+: */
|
||||
/* ft_string.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/05 00:03:33 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/05 00:05:22 by tischmid ### ########.fr */
|
||||
/* Created: 2023/04/05 09:56:45 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/05 10:02:30 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdlib.h>
|
||||
#ifndef FT_STRING_H
|
||||
# define FT_STRING_H
|
||||
# include <stdio.h>
|
||||
|
||||
char *char_calloc(size_t size)
|
||||
{
|
||||
char *ptr;
|
||||
char *ft_strcpy(char *dest, char *src);
|
||||
size_t ft_strlen(char const *str);
|
||||
|
||||
ptr = malloc(size);
|
||||
if (!ptr)
|
||||
return (ptr);
|
||||
while (size--)
|
||||
ptr[size] = 0;
|
||||
return (ptr);
|
||||
}
|
||||
#endif
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/05 04:11:08 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/05 06:28:23 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/05 10:01:56 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -15,10 +15,15 @@
|
|||
# include "parsing.h"
|
||||
# include <limits.h>
|
||||
# include <unistd.h>
|
||||
# define RED "\x1b[31m"
|
||||
# define GRN "\x1b[32m"
|
||||
# define YLW "\x1b[33m"
|
||||
# define CLR_RST "\x1b[m"
|
||||
|
||||
void ft_putchar(char c);
|
||||
void ft_putstr(char *str);
|
||||
void ft_putnbr(int nb);
|
||||
size_t ft_strlen(char const *str);
|
||||
void print_map(t_map *map, int as_numbers);
|
||||
int ft_err(char *str, int exit_code);
|
||||
|
||||
#endif
|
||||
|
|
BIN
obj/main.o
BIN
obj/main.o
Binary file not shown.
BIN
obj/parsing.o
BIN
obj/parsing.o
Binary file not shown.
Binary file not shown.
BIN
obj/solution.o
BIN
obj/solution.o
Binary file not shown.
|
@ -0,0 +1,34 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_string.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/05 09:57:31 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/05 09:57:39 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_string.h"
|
||||
|
||||
char *ft_strcpy(char *dest, char *src)
|
||||
{
|
||||
char *o_dest;
|
||||
|
||||
o_dest = dest;
|
||||
while (*src)
|
||||
*dest++ = *src++;
|
||||
*dest = 0;
|
||||
return (o_dest);
|
||||
}
|
||||
|
||||
size_t ft_strlen(char const *str)
|
||||
{
|
||||
size_t size;
|
||||
|
||||
size = 0;
|
||||
while (*str++)
|
||||
++size;
|
||||
return (size);
|
||||
}
|
43
srcs/main.c
43
srcs/main.c
|
@ -6,57 +6,26 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/04 21:50:50 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/05 09:33:15 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/05 10:05:40 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include "solution.h"
|
||||
#include "printing.h"
|
||||
#include "ft_string.h"
|
||||
|
||||
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;
|
||||
|
||||
ret = read_fname("./assets/map2", &map);
|
||||
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................");
|
||||
if (!read_fname("./assets/subject.map", &map))
|
||||
return (ft_err("map error\n", 1));
|
||||
map.copy = malloc(sizeof(char) * (ft_strlen(map.data) + 1));
|
||||
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, 0);
|
||||
free(map.data);
|
||||
free(map.copy);
|
||||
return (0);
|
||||
}
|
||||
// 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);
|
||||
|
|
|
@ -6,14 +6,12 @@
|
|||
/* By: apago <apago@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/04 16:19:27 by apago #+# #+# */
|
||||
/* Updated: 2023/04/05 01:49:15 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/05 10:04:09 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parsing.h"
|
||||
|
||||
void *char_calloc(size_t size);
|
||||
|
||||
int parse_meta(char *str, t_meta *meta)
|
||||
{
|
||||
int read_bytes;
|
||||
|
@ -68,7 +66,7 @@ char *parse_data(char *data, t_meta *meta)
|
|||
int i;
|
||||
|
||||
i = 0;
|
||||
res = char_calloc(meta->width * meta->height * sizeof(char));
|
||||
res = malloc(meta->width * meta->height * sizeof(char));
|
||||
if (!res)
|
||||
return (0);
|
||||
while (i < meta->height)
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/05 04:09:39 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/05 06:54:58 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/05 10:00:23 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -17,6 +17,14 @@ void ft_putchar(char c)
|
|||
write(1, &c, 1);
|
||||
}
|
||||
|
||||
int ft_err(char *str, int exit_code)
|
||||
{
|
||||
ft_putstr(RED);
|
||||
ft_putstr(str);
|
||||
ft_putstr(CLR_RST);
|
||||
return (exit_code);
|
||||
}
|
||||
|
||||
void ft_putstr(char *str)
|
||||
{
|
||||
while (*str)
|
||||
|
@ -44,16 +52,6 @@ void ft_putnbr(int nb)
|
|||
ft_putchar(nb % 10 + '0');
|
||||
}
|
||||
|
||||
size_t ft_strlen(char const *str)
|
||||
{
|
||||
size_t size;
|
||||
|
||||
size = 0;
|
||||
while (*str++)
|
||||
++size;
|
||||
return (size);
|
||||
}
|
||||
|
||||
void print_map(t_map *map, int as_numbers)
|
||||
{
|
||||
int i;
|
||||
|
@ -76,11 +74,3 @@ void print_map(t_map *map, int as_numbers)
|
|||
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,12 +6,11 @@
|
|||
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/04 21:46:57 by tischmid #+# #+# */
|
||||
/* Updated: 2023/04/05 09:39:53 by tischmid ### ########.fr */
|
||||
/* Updated: 2023/04/05 10:03:19 by tischmid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "solution.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void preprocess_map(t_map *map)
|
||||
{
|
||||
|
@ -93,7 +92,5 @@ void solve(t_map *map)
|
|||
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