From 61a0fee3b3b0beaff0162a7991bfdc0582ff7b8a Mon Sep 17 00:00:00 2001 From: tomoron Date: Thu, 4 Dec 2025 11:14:22 +0100 Subject: [PATCH] add day 04 --- 2025/4/Makefile | 69 ++++++++++++++++++++++++++++++++++++++++++++ 2025/4/main.c | 67 +++++++++++++++++++++++++++++++++++++++++++ 2025/4/part1.c | 71 +++++++++++++++++++++++++++++++++++++++++++++ 2025/4/part2.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 2025/4/test | 10 +++++++ 5 files changed, 293 insertions(+) create mode 100644 2025/4/Makefile create mode 100644 2025/4/main.c create mode 100644 2025/4/part1.c create mode 100644 2025/4/part2.c create mode 100644 2025/4/test diff --git a/2025/4/Makefile b/2025/4/Makefile new file mode 100644 index 0000000..5fad37f --- /dev/null +++ b/2025/4/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/4/main.c b/2025/4/main.c new file mode 100644 index 0000000..4f9b609 --- /dev/null +++ b/2025/4/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/4/part1.c b/2025/4/part1.c new file mode 100644 index 0000000..787c76c --- /dev/null +++ b/2025/4/part1.c @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2025/12/04 11:12:50 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft/libft.h" + +static long int is_box(int x, int y, int len_x, int len_y, char **map) +{ + if(x < 0 || y < 0 || x >= len_x || y >= len_y) + return(0); + return(map[y][x] == '@' || map[y][x] == 'x'); +} + +static long int count_neighbors(int x, int y, int len_x, int len_y, char **split) +{ + int res = 0; + + res += is_box(x - 1, y - 1, len_x, len_y, split); + res += is_box(x, y -1, len_x, len_y, split); + res += is_box(x + 1, y - 1, len_x, len_y, split); + res += is_box(x + 1, y, len_x, len_y, split); + res += is_box(x + 1, y + 1, len_x, len_y, split); + res += is_box(x, y + 1, len_x, len_y, split); + res += is_box(x - 1, y + 1, len_x, len_y, split); + res += is_box(x - 1, y, len_x, len_y, split); + return(res); +} + +long int resolve_part1(char *input, char **split) +{ + (void)input; + int x; + int y; + int len_x; + int len_y; + long int res; + + len_x = ft_strlen(split[0]); + len_y = 0; + res = 0; + while(split[len_y]) + len_y++; + + y = 0; + while(y < len_y) + { + x = 0; + while(x < len_x) + { + if(split[y][x] == '@') + if(count_neighbors(x, y, len_x, len_y, split) < 4) + { + split[y][x] = 'x'; + res++; + } + x++; + } + y++; + } + return(res); +} diff --git a/2025/4/part2.c b/2025/4/part2.c new file mode 100644 index 0000000..7f0ef38 --- /dev/null +++ b/2025/4/part2.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2025/12/04 11:13:32 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft/libft.h" + +static long int is_box(int x, int y, int len_x, int len_y, char **map) +{ + if(x < 0 || y < 0 || x >= len_x || y >= len_y) + return(0); + return(map[y][x] == '@' || map[y][x] == 'x'); +} + +static long int count_neighbors(int x, int y, int len_x, int len_y, char **split) +{ + int res = 0; + + res += is_box(x - 1, y - 1, len_x, len_y, split); + res += is_box(x, y -1, len_x, len_y, split); + res += is_box(x + 1, y - 1, len_x, len_y, split); + res += is_box(x + 1, y, len_x, len_y, split); + res += is_box(x + 1, y + 1, len_x, len_y, split); + res += is_box(x, y + 1, len_x, len_y, split); + res += is_box(x - 1, y + 1, len_x, len_y, split); + res += is_box(x - 1, y, len_x, len_y, split); + return(res); +} + +long int resolve_part2(char *input, char **split) +{ + (void)input; + int x; + int y; + int len_x; + int len_y; + long int res; + int i = 0; + + len_x = ft_strlen(split[0]); + len_y = 0; + res = 0; + while(split[len_y]) + len_y++; + + while(i < 500) + { + y = 0; + while(y < len_y) + { + x = 0; + while(x < len_x) + { + if(split[y][x] == '@') + if(count_neighbors(x, y, len_x, len_y, split) < 4) + { + split[y][x] = '.'; + res++; + } + x++; + } + y++; + } + i++; + } + return(res); +} diff --git a/2025/4/test b/2025/4/test new file mode 100644 index 0000000..8209399 --- /dev/null +++ b/2025/4/test @@ -0,0 +1,10 @@ +..@@.@@@@. +@@@.@.@.@@ +@@@@@.@.@@ +@.@@@@..@. +@@.@@@@.@@ +.@@@@@@@.@ +.@.@.@.@@@ +@.@@@.@@@@ +.@@@@@@@@. +@.@.@@@.@.