add day 9 (a bit slow)
This commit is contained in:
69
2025/9/Makefile
Normal file
69
2025/9/Makefile
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2024/07/17 15:48:36 by tomoron #+# #+# #
|
||||||
|
# Updated: 2025/12/02 14:14:04 by tomoron ### ########.fr #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
SRCS = main.c\
|
||||||
|
part1.c\
|
||||||
|
part2.c
|
||||||
|
|
||||||
|
OBJS = $(SRCS:.c=.o)
|
||||||
|
|
||||||
|
FLAGS = -Wextra -Werror -Wall -g
|
||||||
|
|
||||||
|
LIB = libft/libft.a
|
||||||
|
|
||||||
|
all: $(LIB) a.out test
|
||||||
|
cp test cur_input
|
||||||
|
./a.out
|
||||||
|
|
||||||
|
f: $(LIB) a.out input
|
||||||
|
cp input cur_input
|
||||||
|
./a.out
|
||||||
|
|
||||||
|
test:
|
||||||
|
touch test
|
||||||
|
|
||||||
|
input:
|
||||||
|
touch input
|
||||||
|
|
||||||
|
cp:
|
||||||
|
cp part1.c part2.c
|
||||||
|
sed -i 's/resolve_part1/resolve_part2/g' part2.c
|
||||||
|
|
||||||
|
$(LIB): libft
|
||||||
|
make -C libft
|
||||||
|
|
||||||
|
libft:
|
||||||
|
cp -r ../../libft .
|
||||||
|
|
||||||
|
main.c:
|
||||||
|
cp ../../main.c .
|
||||||
|
|
||||||
|
part1.c:
|
||||||
|
cp ../../part1.c .
|
||||||
|
|
||||||
|
part2.c:
|
||||||
|
cp ../../part2.c .
|
||||||
|
|
||||||
|
a.out: main.c $(OBJS) $(LIB)
|
||||||
|
clang $(FLAGS) $(OBJS) $(LIB)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(OBJS)
|
||||||
|
rm -rf libft
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
rm -rf a.out
|
||||||
|
|
||||||
|
.c.o:
|
||||||
|
clang $(FLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
.PHONY: t all clean fclean cp
|
||||||
67
2025/9/main.c
Normal file
67
2025/9/main.c
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "libft/libft.h"
|
||||||
|
|
||||||
|
long int resolve_part1(char *input, char **split);
|
||||||
|
long int resolve_part2(char *input, char **split);
|
||||||
|
|
||||||
|
char *get_input(char *file)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
int rd_len;
|
||||||
|
char buffer[1000];
|
||||||
|
int len;
|
||||||
|
char *res;
|
||||||
|
|
||||||
|
fd = open(file, O_RDONLY);
|
||||||
|
len = 0;
|
||||||
|
if(fd < 0)
|
||||||
|
return(0);
|
||||||
|
rd_len = -1;
|
||||||
|
while(rd_len)
|
||||||
|
{
|
||||||
|
rd_len = read(fd, buffer, 1000);
|
||||||
|
len+= rd_len;
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
res = malloc(len + 1);
|
||||||
|
if(!res)
|
||||||
|
return(0);
|
||||||
|
fd = open(file, O_RDONLY);
|
||||||
|
if(fd < 0)
|
||||||
|
return(0);
|
||||||
|
rd_len = read(fd,res, len);
|
||||||
|
res[len] = 0;
|
||||||
|
close(fd);
|
||||||
|
return(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char *input;
|
||||||
|
char *input_cpy;
|
||||||
|
char **split;
|
||||||
|
|
||||||
|
input = get_input("cur_input");
|
||||||
|
input_cpy = ft_strdup(input);
|
||||||
|
if(!input || !input_cpy)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "file read error\n");
|
||||||
|
return(1);
|
||||||
|
}
|
||||||
|
split = ft_split(input, '\n');
|
||||||
|
if(!split)
|
||||||
|
return(1);
|
||||||
|
printf("result part 1: %ld\n", resolve_part1(input, split));
|
||||||
|
ft_free_str_arr(split);
|
||||||
|
split = ft_split(input_cpy, '\n');
|
||||||
|
if(!split)
|
||||||
|
return(1);
|
||||||
|
printf("result part 2: %ld\n", resolve_part2(input, split));
|
||||||
|
ft_free_str_arr(split);
|
||||||
|
free(input);
|
||||||
|
free(input_cpy);
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
0
2025/9/make
Normal file
0
2025/9/make
Normal file
79
2025/9/part1.c
Normal file
79
2025/9/part1.c
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* part1.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2025/12/09 21:37:15 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "libft/libft.h"
|
||||||
|
|
||||||
|
typedef struct s_cords
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
} t_coords;
|
||||||
|
|
||||||
|
static t_coords parse_line(char *line)
|
||||||
|
{
|
||||||
|
t_coords res;
|
||||||
|
|
||||||
|
res.x = atoi(line);
|
||||||
|
while(ft_isdigit(*line))
|
||||||
|
line++;
|
||||||
|
line++;
|
||||||
|
res.y = atoi(line);
|
||||||
|
return(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
static long int get_biggest_area(t_coords *co, int len)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
long int max;
|
||||||
|
long int tmp;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
max = 0;
|
||||||
|
while(i < len)
|
||||||
|
{
|
||||||
|
j = -1;
|
||||||
|
while(++j < len)
|
||||||
|
{
|
||||||
|
if(i == j)
|
||||||
|
continue;
|
||||||
|
tmp = ((long)abs(co[i].x - co[j].x) + 1) * ((long)abs(co[i].y - co[j].y) + 1);
|
||||||
|
if(tmp > max)
|
||||||
|
max = tmp;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return(max);
|
||||||
|
}
|
||||||
|
|
||||||
|
long int resolve_part1(char *input, char **split)
|
||||||
|
{
|
||||||
|
(void)input;
|
||||||
|
int len;
|
||||||
|
t_coords *co;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
while(split[len])
|
||||||
|
len++;
|
||||||
|
co = malloc(len * sizeof(t_coords));
|
||||||
|
i = 0;
|
||||||
|
while(split[i])
|
||||||
|
{
|
||||||
|
co[i] = parse_line(split[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return(get_biggest_area(co, len));
|
||||||
|
}
|
||||||
|
// 2147308956 <
|
||||||
166
2025/9/part2.c
Normal file
166
2025/9/part2.c
Normal file
@ -0,0 +1,166 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* part2.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <tomoron@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2025/12/10 15:08:17 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include "libft/libft.h"
|
||||||
|
|
||||||
|
typedef struct s_cords
|
||||||
|
{
|
||||||
|
double x;
|
||||||
|
double y;
|
||||||
|
} t_coords;
|
||||||
|
|
||||||
|
static t_coords parse_line(char *line)
|
||||||
|
{
|
||||||
|
t_coords res;
|
||||||
|
|
||||||
|
res.x = atoi(line);
|
||||||
|
while(ft_isdigit(*line))
|
||||||
|
line++;
|
||||||
|
line++;
|
||||||
|
res.y = atoi(line);
|
||||||
|
return(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int is_point_on_line(t_coords pos, t_coords a, t_coords b)
|
||||||
|
{
|
||||||
|
double cross = (pos.x - a.x) * (b.y - a.y) - (pos.y - a.y) * (b.x - a.x);
|
||||||
|
if(cross != 0)
|
||||||
|
return(0);
|
||||||
|
return(pos.x >= fmin(a.x, b.x) && pos.x <= fmax(a.x, b.x) &&
|
||||||
|
pos.y >= fmin(a.y, b.y) && pos.y <= fmax(a.y, b.y));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int is_point_in_poly(t_coords pos, t_coords *poly, int len)
|
||||||
|
{
|
||||||
|
int inside;
|
||||||
|
int i;
|
||||||
|
t_coords cur[2];
|
||||||
|
double intersect;
|
||||||
|
|
||||||
|
inside = 0;
|
||||||
|
i = 0;
|
||||||
|
while(i < len)
|
||||||
|
{
|
||||||
|
cur[0] = poly[i];
|
||||||
|
cur[1] = poly[(i + 1) % len];
|
||||||
|
|
||||||
|
if(is_point_on_line(pos, cur[0], cur[1]))
|
||||||
|
{
|
||||||
|
inside = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if((cur[0].y > pos.y) != (cur[1].y > pos.y))
|
||||||
|
{
|
||||||
|
intersect = ((cur[1].x - cur[0].x) * (pos.y - cur[0].y)) / (cur[1].y - cur[0].y) + cur[0].x;
|
||||||
|
if(pos.x < intersect)
|
||||||
|
inside = !inside;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return(inside);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int is_line_in_poly(t_coords p1, t_coords p2, t_coords *poly, int len)
|
||||||
|
{
|
||||||
|
t_coords from;
|
||||||
|
t_coords to;
|
||||||
|
|
||||||
|
from.x = fmin(p1.x, p2.x);
|
||||||
|
from.y = fmin(p1.y, p2.y);
|
||||||
|
to.x = fmax(p1.x, p2.x);
|
||||||
|
to.y = fmax(p1.y, p2.y);
|
||||||
|
|
||||||
|
while(from.x <= to.x && from.y <= to.y)
|
||||||
|
{
|
||||||
|
if(!is_point_in_poly(from, poly, len))
|
||||||
|
{
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
if(from.x != to.x)
|
||||||
|
from.x++;
|
||||||
|
else
|
||||||
|
from.y++;
|
||||||
|
}
|
||||||
|
return(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int is_all_in_poly(t_coords p1, t_coords p2, t_coords *poly, int len)
|
||||||
|
{
|
||||||
|
|
||||||
|
if(!is_line_in_poly(p1,(t_coords){p2.x, p1.y}, poly, len) ||
|
||||||
|
!is_line_in_poly((t_coords){p2.x, p1.y}, p2, poly, len) ||
|
||||||
|
!is_line_in_poly(p2, (t_coords){p1.x, p2.y}, poly, len) ||
|
||||||
|
!is_line_in_poly((t_coords){p1.x, p2.y},p1, poly, len))
|
||||||
|
return(0);
|
||||||
|
return(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static long int get_biggest_area(t_coords *co, int len)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
long int max;
|
||||||
|
long int tmp;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
max = 0;
|
||||||
|
while(i < len)
|
||||||
|
{
|
||||||
|
j = i;
|
||||||
|
while(++j < len)
|
||||||
|
{
|
||||||
|
if(i == j)
|
||||||
|
continue;
|
||||||
|
tmp = ((long)fabs(co[i].x - co[j].x) + 1) * ((long)fabs(co[i].y - co[j].y) + 1);
|
||||||
|
if(tmp <= max)
|
||||||
|
continue;
|
||||||
|
if(!is_point_in_poly((t_coords){co[i].x,co[j].y}, co, len) || !is_point_in_poly((t_coords){co[j].x,co[i].y}, co, len))
|
||||||
|
continue;
|
||||||
|
if(!is_all_in_poly(co[i], co[j], co, len))
|
||||||
|
continue;
|
||||||
|
printf("got %ld for %.0f, %.0f to %.0f, %.0f checking points \n", tmp, co[i].x, co[i].y, co[j].x, co[j].y);
|
||||||
|
max = tmp;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return(max);
|
||||||
|
}
|
||||||
|
|
||||||
|
long int resolve_part2(char *input, char **split)
|
||||||
|
{
|
||||||
|
(void)input;
|
||||||
|
int len;
|
||||||
|
t_coords *co;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
while(split[len])
|
||||||
|
len++;
|
||||||
|
co = malloc(len * sizeof(t_coords));
|
||||||
|
i = 0;
|
||||||
|
while(split[i])
|
||||||
|
{
|
||||||
|
co[i] = parse_line(split[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return(get_biggest_area(co, len));
|
||||||
|
}
|
||||||
|
// 4604141472 >
|
||||||
|
// 4604141472 :/
|
||||||
|
// 4501110590 >
|
||||||
|
// 1588990708
|
||||||
8
2025/9/test
Normal file
8
2025/9/test
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
7,1
|
||||||
|
11,1
|
||||||
|
11,7
|
||||||
|
9,7
|
||||||
|
9,5
|
||||||
|
2,5
|
||||||
|
2,3
|
||||||
|
7,3
|
||||||
Reference in New Issue
Block a user