From bc0d448f75ff48f56cf4f0d07daf472939aa1239 Mon Sep 17 00:00:00 2001 From: tomoron Date: Mon, 1 Dec 2025 10:45:59 +0100 Subject: [PATCH] day 1 done --- 2025/1/Makefile | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ 2025/1/main.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 2025/1/part1.c | 37 ++++++++++++++++++++++++++ 2025/1/part2.c | 55 +++++++++++++++++++++++++++++++++++++++ 2025/1/test | 10 +++++++ 2025/Makefile | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 307 insertions(+) create mode 100644 2025/1/Makefile create mode 100644 2025/1/main.c create mode 100644 2025/1/part1.c create mode 100644 2025/1/part2.c create mode 100644 2025/1/test create mode 100644 2025/Makefile diff --git a/2025/1/Makefile b/2025/1/Makefile new file mode 100644 index 0000000..9c9416a --- /dev/null +++ b/2025/1/Makefile @@ -0,0 +1,69 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: tomoron +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/07/17 15:48:36 by tomoron #+# #+# # +# Updated: 2025/12/01 09:42:26 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/1/main.c b/2025/1/main.c new file mode 100644 index 0000000..4f9b609 --- /dev/null +++ b/2025/1/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/1/part1.c b/2025/1/part1.c new file mode 100644 index 0000000..eae9849 --- /dev/null +++ b/2025/1/part1.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2025/12/01 10:45:18 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft/libft.h" + +long int resolve_part1(char *input, char **split) +{ + (void)input; + int move_val; + long int cur_val; + long int res; + + cur_val = 50; + res = 0; + while(*split) + { + move_val = ft_atoi((*split) + 1); + if(**split == 'L') + move_val *= -1; + cur_val += move_val; + if(cur_val % 100 == 0) + res++; + split++; + } + return(res); +} diff --git a/2025/1/part2.c b/2025/1/part2.c new file mode 100644 index 0000000..7cb036f --- /dev/null +++ b/2025/1/part2.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* part2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/17 23:03:36 by tomoron #+# #+# */ +/* Updated: 2025/12/01 10:43:09 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft/libft.h" + +int change_val(long int *val, int step) +{ + *val += step; + if(*val > 99) + *val = 0; + if(*val < 0) + *val = 99; + if(*val == 0) + return(1); + return(0); + +} + +long int resolve_part2(char *input, char **split) +{ + (void)input; + int move_val; + long int cur_val; + long int res; + + cur_val = 50; + res = 0; + while(*split) + { + move_val = ft_atoi((*split) + 1); + if(**split == 'L') + { + while (move_val--) + res += change_val(&cur_val, -1); + } + else + { + while (move_val--) + res += change_val(&cur_val, 1); + } + split++; + } + return(res); +} diff --git a/2025/1/test b/2025/1/test new file mode 100644 index 0000000..53287c7 --- /dev/null +++ b/2025/1/test @@ -0,0 +1,10 @@ +L68 +L30 +R48 +L5 +R60 +L55 +L1 +L99 +R14 +L82 diff --git a/2025/Makefile b/2025/Makefile new file mode 100644 index 0000000..808a501 --- /dev/null +++ b/2025/Makefile @@ -0,0 +1,69 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: tomoron +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/07/17 15:48:36 by tomoron #+# #+# # +# Updated: 2024/10/29 20:43:12 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 ~/Desktop/aoc/libft . + +main.c: + cp ~/Desktop/aoc/main.c . + +part1.c: + cp ~/Desktop/aoc/part1.c . + +part2.c: + cp ~/Desktop/aoc/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