Compare commits

...

17 Commits

Author SHA1 Message Date
Timo Schmidt 56a46c0bb0 Makefile??? 2023-04-05 20:08:45 +02:00
Timo Schmidt ef28991e7c Remove unnecesary check in obstacle count 2023-04-05 20:02:49 +02:00
Andrei Pago dc8da0ae48 Simplify makefile 2023-04-05 19:46:12 +02:00
Andrei Pago 66faec7a0b Remove assets 2023-04-05 19:39:39 +02:00
Andrei Pago 6ef7b128c3 Print line breaks on appropriate output 2023-04-05 19:38:41 +02:00
Andrei Pago 23a404b106 Fix parsing 2023-04-05 19:29:57 +02:00
Andrei Pago c90731e654 Fix stuff 2023-04-05 19:15:25 +02:00
Andrei Pago 340761d565 Remove dead code 2023-04-05 19:12:12 +02:00
Andrei Pago dec52215e3 Unify headers 2023-04-05 19:08:30 +02:00
Andrei Pago 6809293e60 Test with valgrind 2023-04-05 19:02:29 +02:00
Andrei Pago 241abd135e Test full 2023-04-05 18:31:55 +02:00
Andrei Pago 854f581cff norminette 2023-04-05 18:24:30 +02:00
Andrei Pago e13c1fec1f Handle arguments 2023-04-05 18:20:08 +02:00
Andrei Pago d76384446b Make it work with map20x40 2023-04-05 18:00:46 +02:00
Andrei Pago 39c4fb3158 Replace copy with index 2023-04-05 17:42:21 +02:00
Andrei Pago ccf063f3aa Create map with copy 2023-04-05 17:18:28 +02:00
Andrei Pago cf95458e17 Fix parsing files 2023-04-05 17:07:52 +02:00
16 changed files with 170 additions and 268 deletions

View File

@ -7,12 +7,9 @@ SRC = \
printing.c \
map_helpers.c \
ft_string.c \
validation.c \
HEADERS = \
parsing.h \
printing.h \
solution.h \
ft_string.h \
HEADERS = ft_bsq.h
OBJDIR = obj
SRCDIR = srcs
@ -89,4 +86,4 @@ run:
$(YLW) ./$(NAME) $(CLR_RST) $(SUCCESS)
valgrind: re
$(CYN) $(VALGRIND) ./$(NAME) $(CLR_RST) $(SUCCESS_VALG)
$(CYN) $(VALGRIND) ./$(NAME) ./assets/subject.map ./assets/full.map $(CLR_RST) $(SUCCESS_VALG)

View File

@ -1,24 +0,0 @@
#!/usr/bin/perl
use warnings;
use strict;
die "program x y density" unless (scalar(@ARGV) == 3);
my ($x, $y, $density) = @ARGV;
my $empty = ".";
my $obstc = "X";
my $full = "#";
print "$y${empty}${obstc}${full}\n";
for (my $i = 0; $i < $y; $i++) {
for (my $j = 0; $j < $x; $j++) {
if (int(rand($y) * 2) < $density) {
print $obstc;
}
else {
print $empty;
}
}
print "\n";
}

View File

@ -1,21 +0,0 @@
20.X#
........................................
...................X....................
.........................X..........X...
..............X.........................
.........X...........................X..
............X..X........................
..................................X.....
....................................X...
........................................
X.X..........................X..........
....X.................................X.
..........X.............................
.............................X..........
....................X......X............
............................X...X......X
.......X........X....X....X..XX.........
................X........X............X.
................X......X................
....X................X.......X..........
........................................

View File

@ -1,10 +0,0 @@
9.ox
...........................
....o......................
............o..............
...........................
....o......................
...............o...........
...........................
......o..............o.....
..o.......o................

View File

@ -1,22 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.h :+: :+: :+: */
/* ft_bsq.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apago <apago@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/04 18:22:11 by apago #+# #+# */
/* Updated: 2023/04/05 09:31:04 by tischmid ### ########.fr */
/* Created: 2023/04/05 09:56:45 by tischmid #+# #+# */
/* Updated: 2023/04/05 19:27:20 by apago ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PARSING_H
# define PARSING_H
#ifndef FT_BSQ_H
# define FT_BSQ_H
# include "fcntl.h"
# include "stdio.h"
# include "stdlib.h"
# include "unistd.h"
# include <fcntl.h>
# include <limits.h>
# include <stdlib.h>
# include <unistd.h>
typedef struct Meta
{
@ -31,7 +31,7 @@ typedef struct Map
{
t_meta meta;
char *data;
char *copy;
int *index;
} t_map;
char *read_file(int file);
@ -40,6 +40,22 @@ int parse_valid_uint(char *str, size_t len);
int read_char(char *str, char *dst);
int printable(char c);
size_t count_first_line(char *line);
int read_fname(char *name, t_map *map);
int parse_file(int file, t_map *map);
void copy_bytes(char *dst, char *src, size_t bytes);
void ft_memset_int(int *dst, int set, size_t bytes);
void ft_putchar(char c, int fd);
void ft_putstr(char *str, int fd);
int ft_putnbr(int nb);
void print_map(t_map *map, int as_numbers);
int ft_err(char *str, int exit_code);
char *ft_strcpy(char *dest, char *src);
size_t ft_strlen(char const *str);
void solve(t_map *map);
int itoc(int idx, int width);
int itor(int idx, int width);
int get_index(t_map *map, int idx, int top, int left);
int validate_meta(t_meta *meta);
#endif

View File

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_string.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/05 09:56:45 by tischmid #+# #+# */
/* Updated: 2023/04/05 10:02:30 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_STRING_H
# define FT_STRING_H
# include <stdio.h>
char *ft_strcpy(char *dest, char *src);
size_t ft_strlen(char const *str);
#endif

View File

@ -1,29 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printing.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/05 04:11:08 by tischmid #+# #+# */
/* Updated: 2023/04/05 11:01:41 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PRINTING_H
# define PRINTING_H
# 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, int fd);
void ft_putstr(char *str, int fd);
void ft_putnbr(int nb);
void print_map(t_map *map, int as_numbers);
int ft_err(char *str, int exit_code);
#endif

View File

@ -3,14 +3,14 @@
/* ::: :::::::: */
/* ft_string.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* By: apago <apago@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/05 09:57:31 by tischmid #+# #+# */
/* Updated: 2023/04/05 09:57:39 by tischmid ### ########.fr */
/* Updated: 2023/04/05 19:06:23 by apago ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "ft_bsq.h"
char *ft_strcpy(char *dest, char *src)
{

View File

@ -6,11 +6,11 @@
/* 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 19:06:21 by apago ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
#include "ft_bsq.h"
void copy_bytes(char *dst, char *src, size_t bytes)
{
@ -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

@ -3,60 +3,56 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* By: apago <apago@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/04 21:50:50 by tischmid #+# #+# */
/* Updated: 2023/04/05 11:04:31 by tischmid ### ########.fr */
/* Updated: 2023/04/05 19:37:27 by apago ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include "solution.h"
#include "printing.h"
#include "ft_string.h"
#include "ft_bsq.h"
void debug_set_map(t_map *map)
{
char *str;
str = \
"........................................" \
"...................X...................." \
".........................X..........X..." \
"..............X........................." \
".........X...........................X.." \
"............X..X........................" \
"..................................X....." \
"....................................X..." \
"........................................" \
"X.X..........................X.........." \
"....X.................................X." \
"..........X............................." \
".............................X.........." \
"....................X......X............" \
"............................X...X......X" \
".......X........X....X....X..XX........." \
"................X........X............X." \
"................X......X................" \
"....X................X.......X.........." \
"........................................";
map->data = malloc(sizeof(char) * (ft_strlen(str) + 1));
ft_strcpy(map->data, str);
}
int main(void)
int handle_file(int file, int extra_newline)
{
t_map map;
if (!read_fname("./assets/map20x40.map", &map))
return (ft_err("map error\n", 1));
free(map.data);
debug_set_map(&map);
map.copy = malloc(sizeof(char) * (ft_strlen(map.data) + 1));
ft_strcpy(map.copy, map.data);
if (!parse_file(file, &map))
{
ft_err("map error\n", 1);
if (extra_newline)
ft_err("\n", 1);
return (1);
}
solve(&map);
print_map(&map, 0);
if (extra_newline)
ft_putchar('\n', 1);
free(map.data);
free(map.copy);
free(map.index);
return (0);
}
int main(int argc, char **argv)
{
int i;
int file;
int res;
if (argc < 2)
return (handle_file(0, 0));
i = 0;
while (++i < argc)
{
file = open(argv[i], O_RDONLY);
if (file < 0)
{
ft_err("map error\n", 1);
if (argc > 2)
ft_err("\n", 1);
continue ;
}
res = handle_file(file, argc > 2);
close(file);
}
return (0);
}

View File

@ -3,14 +3,14 @@
/* ::: :::::::: */
/* 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 19:06:18 by apago ### ########.fr */
/* */
/* ************************************************************************** */
#include "solution.h"
#include "ft_bsq.h"
// index to column
int itoc(int idx, int width)
@ -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,31 +6,32 @@
/* By: apago <apago@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/04 16:19:27 by apago #+# #+# */
/* Updated: 2023/04/05 10:04:09 by tischmid ### ########.fr */
/* Updated: 2023/04/05 19:28:14 by apago ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
#include "ft_bsq.h"
int parse_meta(char *str, t_meta *meta)
{
int read_bytes;
size_t read_bytes;
read_bytes = read_uint(str);
meta->height = parse_valid_uint(str, read_bytes);
read_bytes = 0;
while (str[read_bytes] && str[read_bytes] != '\n')
read_bytes++;
if (read_bytes < 4)
return (0);
if (!read_char(&str[read_bytes - 3], &meta->empty))
return (0);
if (!read_char(&str[read_bytes - 2], &meta->obstacle))
return (0);
if (!read_char(&str[read_bytes - 1], &meta->full))
return (0);
if (read_uint(str) < read_bytes - 3)
return (0);
meta->height = parse_valid_uint(str, read_bytes - 3);
if (meta->height < 1)
return (0);
if (!read_char(&str[read_bytes++], &meta->empty))
return (0);
if (!read_char(&str[read_bytes++], &meta->obstacle))
return (0);
if (!read_char(&str[read_bytes++], &meta->full))
return (0);
if (!printable(meta->empty) || !printable(meta->obstacle)
|| !printable(meta->full))
return (0);
if ((meta->empty == meta->obstacle) || (meta->obstacle == meta->full))
return (0);
if (str[read_bytes++] != '\n')
return (0);
return (read_bytes);
@ -45,10 +46,8 @@ size_t parse_line(char *line, char *dst, t_meta *meta)
{
if (!line[i])
return (0);
if (line[i] == meta->empty)
dst[i] = '.';
else if (line[i] == meta->obstacle)
dst[i] = 'o';
if (line[i] == meta->empty || line[i] == meta->obstacle)
dst[i] = line[i];
else
return (0);
i++;
@ -58,29 +57,30 @@ size_t parse_line(char *line, char *dst, t_meta *meta)
return (meta->width + 1);
}
// size_t offset; // unused
char *parse_data(char *data, t_meta *meta)
{
char *res;
size_t read_bytes;
size_t read_total;
int i;
i = 0;
read_total = 0;
res = malloc(meta->width * meta->height * sizeof(char));
if (!res)
return (0);
while (i < meta->height)
{
read_bytes = parse_line(data, res, meta);
read_bytes = parse_line(&data[read_total], &res[read_total - i], meta);
if (!read_bytes)
{
free(res);
return (0);
}
data += read_bytes;
read_total += read_bytes;
i++;
}
if (!*data)
if (!data[read_total])
return (res);
free(res);
return (0);
@ -92,7 +92,7 @@ int parse_input(char *str, t_map *map)
size_t read_bytes;
read_bytes = parse_meta(str, &map->meta);
if (!read_bytes)
if (!read_bytes || !validate_meta(&map->meta))
return (0);
offset = read_bytes;
map->meta.width = count_first_line(&str[offset]);
@ -101,20 +101,22 @@ int parse_input(char *str, t_map *map)
map->data = parse_data(&str[offset], &map->meta);
if (!map->data)
return (0);
map->index = malloc(sizeof(int) * (map->meta.width * map->meta.height));
if (!map->index)
{
free(map->data);
return (0);
}
ft_memset_int(map->index, 0, map->meta.width * map->meta.height);
return (1);
}
int read_fname(char *name, t_map *map)
int parse_file(int file, t_map *map)
{
int file;
char *text;
int res;
file = open(name, O_RDONLY);
if (file == -1)
return (0);
text = read_file(file);
close(file);
if (!text)
return (0);
res = parse_input(text, map);

View File

@ -6,11 +6,11 @@
/* By: apago <apago@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/04 18:25:47 by apago #+# #+# */
/* Updated: 2023/04/04 21:56:56 by tischmid ### ########.fr */
/* Updated: 2023/04/05 19:06:14 by apago ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
#include "ft_bsq.h"
size_t read_uint(char *str)
{

View File

@ -3,14 +3,14 @@
/* ::: :::::::: */
/* 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 19:06:03 by apago ### ########.fr */
/* */
/* ************************************************************************** */
#include "printing.h"
#include "ft_bsq.h"
void ft_putchar(char c, int fd)
{
@ -19,9 +19,7 @@ void ft_putchar(char c, int fd)
int ft_err(char *str, int exit_code)
{
ft_putstr(RED, 2);
ft_putstr(str, 2);
ft_putstr(CLR_RST, 2);
return (exit_code);
}
@ -31,25 +29,20 @@ void ft_putstr(char *str, int fd)
ft_putchar(*str++, fd);
}
void ft_putnbr(int nb)
int ft_putnbr(int nb)
{
if (nb > 9)
{
ft_putnbr(nb / 10);
ft_putchar(nb % 10 + '0', 1);
}
else if (nb == INT_MIN)
{
ft_putnbr(nb / 10);
ft_putnbr(-(nb % 10));
}
else if (nb < 0)
int n;
if (nb < 0)
{
ft_putchar('-', 1);
ft_putnbr(-nb);
return (ft_putnbr(-nb) + 1);
}
else
ft_putchar(nb % 10 + '0', 1);
n = 1;
if (nb > 9)
n += ft_putnbr(nb / 10);
ft_putchar(nb % 10 + '0', 1);
return (n);
}
void print_map(t_map *map, int as_numbers)

View File

@ -3,14 +3,14 @@
/* ::: :::::::: */
/* 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 19:59:57 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#include "solution.h"
#include "ft_bsq.h"
// Replace each cell by the number of obstacles a rectangle starting
// from the top left cell, filling the board until the cell in question,
@ -19,17 +19,15 @@
// 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++;
}
}
@ -40,15 +38,12 @@ 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)
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 +62,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++;
}
}

View File

@ -1,23 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* solution.h :+: :+: :+: */
/* validation.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* Created: 2023/04/05 19:27:00 by apago #+# #+# */
/* Updated: 2023/04/05 19:28:47 by apago ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SOLUTION_H
# define SOLUTION_H
# include "parsing.h"
# include "printing.h"
#include "ft_bsq.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
int validate_meta(t_meta *meta)
{
if (!printable(meta->empty) || !printable(meta->obstacle)
|| !printable(meta->full))
return (0);
if ((meta->empty == meta->obstacle) || (meta->obstacle == meta->full))
return (0);
return (1);
}