From 3dc5e7f8d972f607805d649893ba3c0e4cd64be1 Mon Sep 17 00:00:00 2001 From: tomoron Date: Wed, 10 Dec 2025 15:08:47 +0100 Subject: [PATCH] add day 9 (a bit slow) --- 2025/9/Makefile | 69 ++++++++++++++++++++ 2025/9/main.c | 67 +++++++++++++++++++ 2025/9/make | 0 2025/9/part1.c | 79 +++++++++++++++++++++++ 2025/9/part2.c | 166 ++++++++++++++++++++++++++++++++++++++++++++++++ 2025/9/test | 8 +++ 6 files changed, 389 insertions(+) create mode 100644 2025/9/Makefile create mode 100644 2025/9/main.c create mode 100644 2025/9/make create mode 100644 2025/9/part1.c create mode 100644 2025/9/part2.c create mode 100644 2025/9/test diff --git a/2025/9/Makefile b/2025/9/Makefile new file mode 100644 index 0000000..5fad37f --- /dev/null +++ b/2025/9/Makefile @@ -0,0 +1,69 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: tomoron +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# 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 diff --git a/2025/9/main.c b/2025/9/main.c new file mode 100644 index 0000000..4f9b609 --- /dev/null +++ b/2025/9/main.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#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); +} diff --git a/2025/9/make b/2025/9/make new file mode 100644 index 0000000..e69de29 diff --git a/2025/9/part1.c b/2025/9/part1.c new file mode 100644 index 0000000..cae63af --- /dev/null +++ b/2025/9/part1.c @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2025/12/09 21:37:15 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#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 < diff --git a/2025/9/part2.c b/2025/9/part2.c new file mode 100644 index 0000000..8d197cf --- /dev/null +++ b/2025/9/part2.c @@ -0,0 +1,166 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2025/12/10 15:08:17 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#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 diff --git a/2025/9/test b/2025/9/test new file mode 100644 index 0000000..c8563ea --- /dev/null +++ b/2025/9/test @@ -0,0 +1,8 @@ +7,1 +11,1 +11,7 +9,7 +9,5 +2,5 +2,3 +7,3